-1

i have written a jsp code which contains mutiple select and when i click on submit i get the null pointer exception . i am using the request.getParametervalues() to fetch the data from multiple select:

jsp code is as follows

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page language="java" import="java.lang.* ,javax.servlet.*,javax.servlet.http.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>

</head>
<body>

<form id="form1" method="get" name="form">   

<select name="color1" id="dd1" multiple>
<option value="empty">Select Color</option>
<option value="RED">red</option>
<option value="BLUE">blue</option>
<option value="GREEN">green</option>
<option value="YELLOW">yellow</option>
<option value="PINK">pink</option>
<option value="BLACK">black</option>
<option value="BROWN">brown</option>
<option value="PURPLE">purple</option>
</select>

<input type="submit" value="Submit">
</form>
</body>  
</html>
<%
  String [] x = request.getParameterValues("color1");
  if(!x.equals("")) 
  {
   for (int i = 0; i < x.length; i++)
    out.println (x[i]);

  }                      
%>

i am getting null pointer exception in out.println (x[i]); part i have also tried if(x ! = null) which is giving error

i have also tried this :

 if(request.getParameterValues("color1").equals(null)) 
  {
      out.println("abcd");
  }          

its giving the same exception

amol
  • 89
  • 1
  • 9
  • Well, `out` could be `null`, or `x` could be `null`. Note that `if(!x.equals(""))` is meaningless, since an array will never be equal to a String. – Hot Licks Jun 28 '14 at 04:22
  • `out` is not null @HotLicks Its default object of `JSP` – Pratik Butani Jun 28 '14 at 04:24
  • @PratikButani - Then the whole question is bogus, since the if statement would have gotten an exception if x were null. Most likely the exception is not occurring where the OP says, or the setup for the code is corrupted. – Hot Licks Jun 28 '14 at 04:26
  • This Q&A explains how to go about debugging an NPE: http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it/24347569#24347569 – Stephen C Jun 28 '14 at 04:34
  • @HotLicks is there any way i can check if i am getting any value into `x` ? – amol Jun 28 '14 at 04:34
  • thanks all ... `if(x != null)` condition is woring – amol Jun 28 '14 at 04:43
  • Not a lot one can do until you start telling us where the exception actually occurred. – Hot Licks Jun 28 '14 at 11:36

1 Answers1

0

Check before doing this in your scriplet

if(request.getParameterValues("color1")!=null){
//Go On
}

As your select is in same .jsp and so on start up load it will give you null for sure.

<select name="color1" id="dd1" multiple>

Moreover you are comparing array with ""

if(!x.equals("")) //No use Check for Not Null
if(x!=null)//Cahange this

If you are using form why not pass request and perform checks and other stuff in Servlet.

ΔȺȾΔ
  • 21,571
  • 10
  • 52
  • 80
  • i have tried what you said but its giving the same exception – amol Jun 28 '14 at 04:30
  • 1
    thnks man ..this condition worked ..earlier it was not not working ..maybe there is some issue in Netbeans IDE or a bug – amol Jun 28 '14 at 04:40