Solved

Error for request.getParameter() ???

Posted on 2002-04-17
32
218 Views
Last Modified: 2010-04-01
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 ?
0
Comment
Question by:tanc02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 8
  • 5
  • +4
32 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 50 total points
ID: 6948473
String sField;

if ( request.getParameter("theField") == null )
{
   sField = "";
}
else
{
   sField = request.getParameter("theField");
}
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6948478
or:


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

if ( sField == null )
{
   sField = "";
}
0
 
LVL 1

Author Comment

by:tanc02
ID: 6948487
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Expert Comment

by:knightEknight
ID: 6948504
String sNumber = request.getParameter("number");

if ( sNumber == null )
{
  sNumber = "";
}
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6948516
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
 
LVL 1

Author Comment

by:tanc02
ID: 6948522
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
 
LVL 33

Expert Comment

by:knightEknight
ID: 6948526
same as my above code, just change "sNumber" to "num"
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6948529
Integer.parseInt(sNumber);
is better for parsing ints from strings.

CJ
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6948531
if "number" is not passed on the query string, then request.getParameter("number") will return null.
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6948534
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
 
LVL 33

Expert Comment

by:knightEknight
ID: 6948538
CY == CJ
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6948539
You would also have to catch NumberFormatException in case some one screws with URL.

CJ
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6948541
:-)
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6948557
good point ... does the static method also throw that exception?
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6948567
yes.

First a null check should be done.. which you are doing.. but if you don't just catch a NullPointerException :-)
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6948593
String sNumber = request.getParameter("number");
int iNumber;

try
{
   iNumber = Integer.parseInt(sNumber);
}
catch( Exception e )
{
   iNumber = 0;
}
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6948594
... that will handle nulls and bad data.
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6948623
yup.  but as a ROT (rule of thumb) .. not to get picky or anything ;-) its not good practice to catch Exception e

CJ
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6948647
yes, but this is JSP :)
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6948656
true.. seceding point. <grin>
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6948812
tanc02, did this help?
0
 
LVL 1

Author Comment

by:tanc02
ID: 6948948
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
 
LVL 19

Expert Comment

by:cheekycj
ID: 6948964
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
 
LVL 1

Author Comment

by:tanc02
ID: 6948984
cheekyci,

Same thing, I still got the error.
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6949001
can you paste your entire code?
0
 
LVL 6

Expert Comment

by:kotan
ID: 6949669
NUllPointerExeption  --> NullPointerException
0
 
LVL 27

Expert Comment

by:rrz
ID: 6949703
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
 

Expert Comment

by:jodear
ID: 6949969
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
 
LVL 1

Expert Comment

by:Moondancer
ID: 6955939
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
 
LVL 1

Author Comment

by:tanc02
ID: 6956292
Now it is working. Maybe I type something wrong before.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6956869
Thanks for returning and finalizing this.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's recap what we learned from yesterday's Skyport Systems webinar.
This article outlines the struggles that Macs encounter in Windows-dominated workplace environments – and what Mac users can do to improve their network connectivity and remain productive.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question