Error for request.getParameter() ???

I would like to get form's fields info.
What if the field I ask for is not there. How do I handle
such error ?
LVL 1
tanc02Asked:
Who is Participating?
 
knightEknightConnect With a Mentor Commented:
String sField;

if ( request.getParameter("theField") == null )
{
   sField = "";
}
else
{
   sField = request.getParameter("theField");
}
0
 
knightEknightCommented:
or:


String sField = request.getParameter("theField");

if ( sField == null )
{
   sField = "";
}
0
 
tanc02Author Commented:
What I mean is if I want to get a field name called number.

I can have below situation :

htpp://127.0.0.1/Test/t.jsp?name=mike

or

htpp://127.0.0.1/Test/t.jsp?name=mike&number=0

0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
knightEknightCommented:
String sNumber = request.getParameter("number");

if ( sNumber == null )
{
  sNumber = "";
}
0
 
knightEknightCommented:
and if you want to convert it to an int:


String sNumber = request.getParameter("number");

if ( sNumber == null )
{
   sNumber = "0";  // default value == 0
}


int iNumber = new Integer(sNumber).intValue();
0
 
tanc02Author Commented:
What if I want to get it from this :

htpp://127.0.0.1/Test/t.jsp?name=mike

String num = request.getParameter("number") ??
0
 
knightEknightCommented:
same as my above code, just change "sNumber" to "num"
0
 
cheekycjCommented:
Integer.parseInt(sNumber);
is better for parsing ints from strings.

CJ
0
 
knightEknightCommented:
if "number" is not passed on the query string, then request.getParameter("number") will return null.
0
 
knightEknightCommented:
CY ... yes, I knew there was a static method, but I wasn't sure if it was parseInt or intValue ... so I went with what I knew.
0
 
knightEknightCommented:
CY == CJ
0
 
cheekycjCommented:
You would also have to catch NumberFormatException in case some one screws with URL.

CJ
0
 
cheekycjCommented:
:-)
0
 
knightEknightCommented:
good point ... does the static method also throw that exception?
0
 
cheekycjCommented:
yes.

First a null check should be done.. which you are doing.. but if you don't just catch a NullPointerException :-)
0
 
knightEknightCommented:
String sNumber = request.getParameter("number");
int iNumber;

try
{
   iNumber = Integer.parseInt(sNumber);
}
catch( Exception e )
{
   iNumber = 0;
}
0
 
knightEknightCommented:
... that will handle nulls and bad data.
0
 
cheekycjCommented:
yup.  but as a ROT (rule of thumb) .. not to get picky or anything ;-) its not good practice to catch Exception e

CJ
0
 
knightEknightCommented:
yes, but this is JSP :)
0
 
cheekycjCommented:
true.. seceding point. <grin>
0
 
knightEknightCommented:
tanc02, did this help?
0
 
tanc02Author Commented:
This is what I did :

<%
...
...
String test = "";
try{
  test = request.getParameter("number");
}
catch(NUllPointerExeption e){
  test = "0";
}
%>

on this url http://127.0.0.1/Test/t.jsp?name=mike

I still got NullPointerExeption error.
0
 
cheekycjCommented:
try this:
<%
...
...
String test = request.getParameter("number");
if (test == null)
   test = "0";
%>

the null pointer exception occurs when you use test not when you retrieve/set it.

CJ
0
 
tanc02Author Commented:
cheekyci,

Same thing, I still got the error.
0
 
cheekycjCommented:
can you paste your entire code?
0
 
kotanCommented:
NUllPointerExeption  --> NullPointerException
0
 
rrzCommented:
If you are talking about more than a few parameters, then maybe something like the following might be useful.

 
<%@ page import="java.util.*" %>
<%
Hashtable ht = new Hashtable();
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()){
                                    Object param = paramNames.nextElement();
                                    ht.put(param,request.getParameter((String)param));
}
if(ht.containsKey("name")){out.print("name was sent.");}
if(ht.containsKey("number")){out.print("number was sent.");}
%>
0
 
jodearCommented:
I agree with CJ.  Your error is not due to the code CJ gave.  It has to be in your other code.  If you want, post it so that we can check it out.
0
 
MoondancerCommented:
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20248411.html
http://www.experts-exchange.com/questions/Q.20249733.html
http://www.experts-exchange.com/questions/Q.20250244.html
http://www.experts-exchange.com/questions/Q.20253285.html
http://www.experts-exchange.com/questions/Q.20253658.html
http://www.experts-exchange.com/questions/Q.20253724.html
http://www.experts-exchange.com/questions/Q.20290579.html
http://www.experts-exchange.com/questions/Q.20291425.html
http://www.experts-exchange.com/questions/Q.20291476.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20248017.html

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations if this item remains inactive another seven (7) days.  If you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed here -> http://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange
0
 
tanc02Author Commented:
Now it is working. Maybe I type something wrong before.
0
 
MoondancerCommented:
Thanks for returning and finalizing this.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.