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!

java.lang.NullPointerException?? 2

Status
Not open for further replies.

pdotd

Programmer
Jun 15, 2004
29
CA
i get a java.lang.NullPointerException exception error when i try to run my servlet:

the exact error is:
java.lang.NullPointerException
OnlineAdmin.doGet(OnlineAdmin.java:38)
javax.servlet.http.HttpServlet.service (HttpServlet.java:697)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)

***********(if this helps)
on line 38 in my serlvet is:

if (strAction != null || strAction.equals("Students"))

i also have in the line before that another if statement that is coded liek this


if (request.getParameter("action") == null || request.getParameter("action").equals(""))

**************************

now if this is problem...any help with my code would be awesome
or if it is something else...that i might be doing wrong in my servlet...any sugguest would be appreciated


pdotd
 
perhaps strAction is null?

The construct doesn't look logical, cause if strAction.equals ("Student") it cannot be null.

If it isn't null, the short-circuit OR will not test for .equals("Student").

Did you mean:

if (strAction != null && strAction.equals("Students"))


seeking a job as java-programmer in Berlin:
 
The thing about this line:

if (strAction != null || strAction.equals("Students"))

...is that whenever you attempt to run a function (like .equals(...)) on a null object, you will get an error.

When strAction IS null, it fails the first condition, so it goes on to check the second condition.

stefanwagner is right about using the '&&'. That way, if the first condition fails (due to strAction being null), then the second condition won't even be checked (as there is no way for the overall condition to succeed when the first condition has failed).

Another way to have written it would be:

Code:
if(strAction != null)
{
 if(strAction.equals("Students"))
 {
  ...
 }//end if
}//end if

'hope that helps.

--Dave
 
Hey
thanks guys, my code works now , you were right, my logic was wrong using the short-circuit OR operator.

instead i used && and everything works now. Now understand the error checking for nulls.

thanks

pdotd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top