This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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
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