req.getParameter("...") return NullPointerException

Hello all I have a trouble to reference to hidden field on HTML page from servlet:
It returns java.lang.NullPointerException.

Please advise what is wrong.

Thanks in advance.




//HTML page:

<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>	
<script type="text/javascript" language="JavaScript">
<!--
 function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
 function validate(bClicked){
    document.test.buttonClicked.value = bClicked;
    if(bClicked == 'save'){
    alert(document.test.buttonClicked.value + 'Save ...');
    if (document.test.datafile.value == ""){
      alert("Please select file attachment!");
          return (false);
    }
                       
    return (true);            
    }
    else{
    alert(document.test.buttonClicked.value + 'Delete ...');
    return (false);
   }
   }

//-->
</script>
</head>
<body>	    
<form method="post" enctype="multipart/form-data" action="../servlet/TestServl" name="test">  
  <table width="700" border="0" cellspacing="0" cellpadding="0">
   <tr>
    <td style="width:3"><img src="../images/grayspace.gif"   style="width:5; height:1" alt=""/></td>
    <td></td>
   </tr>
   <tr>
    <td style="width:3"><img src="../images/grayspace.gif" width="5" height="1"/></td>
    <td nowrap="nowrap"><br/>
	<b>Please specify a file to attach to ticket:</b><br/><br/>
	<input type="file" name="datafile"size="50"/>
	<input type="submit" name="submit" onclick="return  validate('save');" value="Save Attachment"/>
        <input type="submit" name="submit" onclick="return validate('delete');" value="Delete"/>
    </td>
</tr>	
<tr>
  <td colspan="2">					    
    <input type="hidden" name="buttonClicked" />
  </td>
 </tr>
</table>
</form>	        
</body>
</html>
   
Servlet's doPost:
public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
 
  String userOption = "";

// following line generates java.lang.NullPointerException:
  userOption = req.getParameter("buttonClicked").trim();
  
  if(userOption.equalsIgnoreCase("save")){
    res.sendRedirect("/servlet/message?msg="+ "Save");
  }
  else{
    res.sendRedirect("/servlet/message?msg="+ "Delete");
  }

    makePage (req, res);
}

Open in new window

Vadim2004Asked:
Who is Participating?
 
cmalakarConnect With a Mentor Commented:
ok.. I overlooked your first html.

You are using multipart/form-data. and uploading a file.

In that case, the data is coming to servlet in a different format. You need to use some library (Ex.. Apache Commons upload) to read the file, as well the parameters that are coming to the server. Check the below link.

http://commons.apache.org/fileupload/using.html
0
 
SordSordCommented:
The problem is that the parameter "buttonClicked" is not getting passed to the servlet. So req.getParameter("buttonClicked") is returing null, and trying to call .trim() on a null object is throwing the exception.

As to why buttonClicked isn't being sent over, I'd assume that there is a problem with your javascript. It may help to try dumping all the parameters being passed using req.getParameterNames() and sending each of the names returned to your debug output, but most likely you'll just find that buttonClicked doesn't exist and you should ask under javascript why it isn't being sent.

You can also do a quick fix by giving your hidden input a default value
<input type="hidden" name="buttonClicked" value="none" />
0
 
cmalakarCommented:
When you have buttonClicked defined as hidden, you should never get "null" value.

Are you referencing the same parameter name in servlet and jsp ?

Also.. you can try to print request.getParameter("buttonClicked"), before you trim it.. and see what is the value being coming to servlet.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Vadim2004Author Commented:
thank you for your reply,
I was follow your recommendations:
assign default value to hidden input
<input type="hidden" name="buttonClicked" value="none" />
and then printed request.getParameter("buttonClicked") - result "null"
As you can see in my JavaScript two 'alerts' - they return expected values from hidden field.
Why my servlet does not get hidden's value?
0
 
cmalakarCommented:
Can you check the output of following in your servlet ?

System.out.println("Map:" + request.getParameterMap());
0
 
Vadim2004Author Commented:
here is result:

Map: {}
0
 
cmalakarCommented:
which means.. you are not getting any parameters from your servlet.

Is it possible to attach the jsp as it is ?
0
 
Vadim2004Author Commented:
here is html code included what is used with servlet
0
 
cmalakarCommented:
where ?
0
 
Vadim2004Author Commented:
/HTML page:

<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>      
<script type="text/javascript" language="JavaScript">
<!--
 function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
 function validate(bClicked){
    document.test.buttonClicked.value = bClicked;
    if(bClicked == 'save'){
    alert(document.test.buttonClicked.value + 'Save ...');
    if (document.test.datafile.value == ""){
      alert("Please select file attachment!");
          return (false);
    }
                       
    return (true);            
    }
    else{
    alert(document.test.buttonClicked.value + 'Delete ...');
    return (false);
   }
   }

//-->
</script>
</head>
<body>      
<form method="post" enctype="multipart/form-data" action="../servlet/TestServl" name="test">  
  <table width="700" border="0" cellspacing="0" cellpadding="0">
   <tr>
    <td style="width:3"><img src="../images/grayspace.gif"   style="width:5; height:1" alt=""/></td>
    <td></td>
   </tr>
   <tr>
    <td style="width:3"><img src="../images/grayspace.gif" width="5" height="1"/></td>
    <td nowrap="nowrap"><br/>
        <b>Please specify a file to attach to ticket:</b><br/><br/>
        <input type="file" name="datafile"size="50"/>
        <input type="submit" name="submit" onclick="return  validate('save');" value="Save Attachment"/>
        <input type="submit" name="submit" onclick="return validate('delete');" value="Delete"/>
    </td>
</tr>  
<tr>
  <td colspan="2">                                          
    <input type="hidden" name="buttonClicked" />
  </td>
 </tr>
</table>
</form>        
</body>
</html>
0
 
Vadim2004Author Commented:
Thank you very much, it solve my problem
0
All Courses

From novice to tech pro — start learning today.