File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes NullPointer Exception in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "NullPointer Exception in JSP" Watch "NullPointer Exception in JSP" New topic
Author

NullPointer Exception in JSP

milan doshi
Ranch Hand

Joined: Nov 03, 2000
Posts: 43
Hello,
I am trying to develop a simple shopping cart using JSP(using Tomcat 3.1).
I have generated CheckBoxes , which when checked , the corresponding items are selected.
I am retrieving those values through the following commands.
:
String basket[]=request.getParameterValues("c1");
out.println("<hr>Items selected "+basket.length);
:
This works fine only when 1 or more items are selected. If however , NO items are selected (i.e no checkboxes are checked) than I get a NullPointer exception.
The reason for this is basket.length!!!
But why ? Why cant I simply get an output as follows?
Items selected null
Thanx in Advance

maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Milan,
This is a very common and famous experience which I think all java people encounter. The best practice of java coding is, before you invoke any method on a reference, check if the reference is NOT NULL and then do the other checks. This is where the '&& CONDITIONAL CHECK' comes for help. Just change your code slightly like this. Now you won't get NullPointerException.
regds
maha anna
milan doshi
Ranch Hand

Joined: Nov 03, 2000
Posts: 43
Thanx for the help,
regards,
Milan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointer Exception in JSP
 
Similar Threads
why am i getting null pointer exception?
Maintain checked checkbox status
multibox disabled but checked
Multiple checkboxes
Maintain checked and unchecked in checkbox in pagination in display tag in struts