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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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
cmalakarCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vadim2004Author Commented:
Thank you very much, it solve my problem
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.