• Post Reply Bookmark Topic Watch Topic
  • New Topic

Return value from checkbox throwing an NPE

 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Im writing a servlet that takes input from a jsp . I return the value from a checkbox as a string and then store it into an integer for database operations.
JSP fragment

Servlet Code

Here when the checkbox is checked the active flag is set to 1 and the corr output is obtained .However when it is unchecked and the form is submitted there is an NPE thrown on the if statement. Could anyone please point out where im going wrong?
Many Thanks.
 
Stefan Evans
Bartender
Posts: 1822
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just noted this on your other post, and then saw this one.

You should note that standard HTML checkboxes will submit
- their value if they are checked (or ON if there is no value defined)
- no request parameter if they are unchecked.

so request.getParameter("statusActive") will be null if the checkbox is not checked, and thus fail with null pointer exception on the .equals() test.




 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stefan!

That worked, thanks for helping out.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using an int to represent a Boolean value?
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,
Good one!
Thanks for replying! I did modify the code to Boolean before using it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!