Skip to content

Update an image to product #7

Pinned Answered by bartsli
tamanhtp asked this question in Q&A
Discussion options

You must be logged in to vote

Re: Update an image to product

Hi @tamanhtp! I can see the issues in your code. Let me help you fix them.

Problems in your current code:

  1. Null pointer issue: product.getImages() returns null when no images exist.
  2. Redundant method calls: You're using both pdImage.setSrc() and pdImage.src() - the second one is a fluent method that returns the object, not a setter.
  3. Incorrect method calls: You're using product.images() instead of product.setImages().
  4. Manual list management: You're manually creating and managing the images list.

Correct solution:

// Get the product
Product product = getProductbyID(products, ID);

// Create new product image
ProductImage pdImage = new ProductImage();
pdImage.s…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@tamanhtp
Comment options

@bartsli
Comment options

bartsli Jul 8, 2025
Collaborator

@tamanhtp
Comment options

@bartsli
Comment options

bartsli Jul 8, 2025
Collaborator

Answer selected by bartsli
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants