Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Null Pointer Exception

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to make a call on my Controller in order to retrieve some data. For example I want to get the Data until the date,which user will give and by id of product(barcode).

 


in line : findByProductBarcodeAndDate  on debug it returns me nullpointer exception.
Cannot find local variable findByProductBarcodeAndDate  .

How I could handle it ?

I also have have on my Service layer:



Thank you in advance!!
 
Master Rancher
Posts: 4663
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the full exception text including stack trace, and point out which line of your code it is thrown from.
 
Eleni Pap
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
























Postman:

"status": 500,
   "error": "Internal Server Error",
   "trace": "java.lang.NullPointerException\r\n\tat com.demo.warehouse.controller.RetrievalController.searchDoctorAppointments(RetrievalController.java:59)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n\tat java.lang.reflect.Method.invoke(Method.java:498)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHan
.....
Console

java.lang.NullPointerException: null
at com.demo.warehouse.controller.RetrievalController.searchRetrievals(RetrievalController.java:59) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_251]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_251]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_251]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_251]



findByProductBarcodeAndDate is null
 
Eleni Pap
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i forgot @Autowired at my Controller

@Autowired
   private RetrievalServiceImpl retrievalService;

But still have problems,

java.lang.NullPointerException\r\n\tat com.demo.warehouse.controller.RetrievalController.findByRetrieval(RetrievalController.java:77)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(
 
Dave Tolls
Master Rancher
Posts: 4663
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you missed the bit where I said to point out where in your code it is thrown from.

Your Controller has no line 77.
 
Saloon Keeper
Posts: 22479
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, what Dave was asking for was a full stack trace, not the full application listing. Too much code is as bad as too little. Worse, really, because too little and we'll ask for more, but too much and it's better just to skip over it. Well, actually, a full stack trace is usually pretty long, but we're used to being able to skip quickly to the important parts.

However, your lastest exception is on line 77 of something that's posted with only about 40-something lines in it. If you could let us know which line in RetrievalController is supposed to be line 77, we can say more.
 
Bartender
Posts: 1944
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Eleni,
In your Retrieval class, there is no attribute called productbarcode. Instead, there is an attribute called product.
So, if your repository, you may want to define a method called findByProductAndDate() instead of findByProductbarcodeAndDate.
 
Rancher
Posts: 3625
40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, your original problem was with the retrievalService, which was not autowired, and so was still null when you tried to use it.  It looks like you're now having the same problem with retrievalRepository.  Which probably has the same solution...
 
Eleni Pap
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all!!

Yes the problem was Autowired. Thank for your time.
 
Himai Minh
Bartender
Posts: 1944
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am wondering why findByProductBarcodeAndDate works.
In the Retrieval class, there is no attribute called productBarcode.
 
Himai Minh
Bartender
Posts: 1944
13
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can answer my own question. The product has a join column called product_barcode. That is why findByProductBarcode still works.
 
    Bookmark Topic Watch Topic
  • New Topic