Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

equals 1

Status
Not open for further replies.

scohan

Programmer
Dec 29, 2000
283
US
How come I get a nullpointerexception with the equals method here?

String cmd"";
.
.
.

cmd = request.getParameter("paste");
if (cmd.equals("True")) {
.
.
.

Thanks.
 
Hi,

The nullpointerexception happened because the variable cmd has a null value. When you compare Strings, you cannot compare null value or the nullpointerexception would be thrown.

So what you can do is:-

cmd = request.getParameter("paste");
if (cmd == null)
cmd="";
if (cmd.equals("True")) {

Of course this isn't a good way of coding. So normally what we do is to check for all input parameters, whether they are null or not, before carrying on with the processing of other things.


String abc = request.getParameter("alpha");
String bcd = request.getParameter("alpha2");
String cmd = request.getParameter("paste");

if (abc == null || bcd == null || cmd == null)
// return a response page to the user saying error in page
else
// carry on processing


Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
cmd EQUALS null because it is

String cmd ;


check it if you did it like

String cmd = "True" ;


 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top