• 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

java.lang.NullPointerException

 
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My following code works if one or more checkboxes selected. If the selection is empty then it throws:
java.lang.NullPointerException

>
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start by looking at the line of code where the exception is thrown. That should tell you which variable is null, and then you should modify your code to test whether the variable is null before using it.

(Hint: we don't know which line of code it is.)
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Defensive programming:
this isn't directly related to your problem, but rather than writing variable.equals("string") try to get into the practice of writing "string".equals(variable)
They are nearly the same but the latter returns false for null rather than throwing an NPE.
You can do the same with many Java classes eg new Integer(0).equals(var)
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David O'Meara wrote:Defensive programming:
this isn't directly related to your problem, but rather than writing variable.equals("string") try to get into the practice of writing "string".equals(variable)
They are nearly the same but the latter returns false for null rather than throwing an NPE.
You can do the same with many Java classes eg new Integer(0).equals(var)



Thanks the problem fixed


Best Regards
 
reply
    Bookmark Topic Watch Topic
  • New Topic