Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in OrderController.java #3

Open
AlpNek opened this issue Nov 18, 2019 · 4 comments
Open

Error in OrderController.java #3

AlpNek opened this issue Nov 18, 2019 · 4 comments

Comments

@AlpNek
Copy link

AlpNek commented Nov 18, 2019

Hi,
Trying to learn some Java + Spring + stuff and your example is awesome.

However, I see an error when using the OrderController.java code. On line 60, it tells me:
Cannot resolve method 'getProductPrice()

Due to this, not able to continue with build.

@shakeelosmani
Copy link
Owner

@AlpNek are you sure you have all the model code correctly available when you are building. The getProductPrice() is a simple getter in the Product class. Once the repository method returns an instance of a product by id we are simply calling the getter so it should work. Also you said OrderController I think that's a typo it is OrdersController.

@AlpNek
Copy link
Author

AlpNek commented Nov 18, 2019

Hi @shakeelosmani , thanks for replying promptly.

I found this comment on your blog and realised the error is because of newer version of springframework:
https://shakeelosmani.wordpress.com/2017/02/13/step-by-step-spring-boot-hibernate-crud-web-application-tutorial/#comment-653

So changed the code to:
for(Long productId:productIds) { if (!(productRepository.findById(productId).orElse(null).getProductPrice().isNaN())) { total = total + (productRepository.findById(productId).orElse(null).getProductPrice()); }
to get it working.
Not sure if this is the best way to do it though, since I am still learning :)

Cheers.

@shakeelosmani
Copy link
Owner

I see yeah that makes sense. Usually I think people would use the pom file and download dependencies that way it will work but downloading later versions may or may not work as I have not kept up with the updates in this example. And the updated code looks fine but it would be better to wrap it in a try catch as it could return null and then a possible exception

@AlpNek
Copy link
Author

AlpNek commented Nov 18, 2019

Thanks @shakeelosmani ! I'll test with try ... catch instead of checking for isNaN () which I'm sure will fall at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants