• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Null Pointer Exception

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm trying to learn about REST services so have been working through a tutorialspoint tutorial. When I greate and run the test class found at https://www.tutorialspoint.com/restful/restful_methods.htm I get a null pointer exception.

Test case name: testGetAllUsers, Result: pass
Exception in thread "main" java.lang.NullPointerException
at com.tutorialspoint.WebServiceTester.testGetUser(WebServiceTester.java:71)
at com.tutorialspoint.WebServiceTester.main(WebServiceTester.java:34)



The issue seems to be with this code:


I assume no value is getting passed. Can anybody advise how I might fix it?

Cheers
Paul

 
paul a carron
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I just realised I posted to the wrong forum. Can I move a post?
 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you know that user is not null?
Why are you using the == operator? That will only work if the ID fields are primitives. Indent the } to the same level as if.
 
paul a carron
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick reply Campbell. User actually appears to be null. How should I deal with this?

Also, the ID fields are int(I think).

Sorry about the }. It's fine in the code but the copy and paste must ahve knocked it out.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

paul a carron wrote:User actually appears to be null. How should I deal with this?


The question should be, "What does it mean when I get to this statement and user is null?"  It may be a simple as
but you can't tell without answer the question first.
 
paul a carron
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah forgive me. I had made changes to some of the other classes but hadn't deployed them to Tomcat. All sorted now though.

Thanks for the help.
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

paul a carron wrote:Ah forgive me. I had made changes to some of the other classes but hadn't deployed them to Tomcat. All sorted now though.

Thanks for the help.

This hardly solves the problem. It eliminates the symptom, but not the problem probably. As been said, you should be questioning yourself why the user is null in the first place? Does your program (not code) behaves in an expected way?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Liutauras means is that there may be a serious error if user is null. You may have corrected that in your recent deployment, but I think you shou‍ld check carefully for whereever the user field is declared, and all constructors, and any setXXX methods, to make sure user won't be null again.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic