It's not a secret anymore!
The moose likes JSP and the fly likes null pointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "null pointer exception" Watch "null pointer exception" New topic

null pointer exception

nene batungbakal

Joined: Apr 24, 2003
Posts: 16
gud pm guys... it's me again... sori to ask you another of silly questions.. hope you can help...
well i hav dis code written in jsp...
String InputString = request.getParameter("InputString");
long IDtransfer = Long.parseLong(request.getParameter("IDtransfer"));
int index;
int start,finish;
long ParsedArtID = 0;
int IDcounter = 0;
int i,carrier;
long[] ArrayOfID = null;

System.out.println("e dito?");
//the Parse Function
if (InputString.length() > 0)
System.out.println("entered 'if InputString.length'"+InputString.length());
for (index=1;index<InputString.length();index++)
{System.out.println("entered 'for (index=1;index<InputString.length();index++)'"+index+" "+InputString.length());
if (InputString.charAt(index) == '@')
{ finish = index;
System.out.println("entered 'if (InputString.charAt(index) == '@')'"+InputString.charAt(index));
ParsedArtID = Long.parseLong(InputString.substring(start,finish));
System.out.println("entered 'ParsedArtID = Long.parseLong(InputString.substring(start,finish));'"+ParsedArtID);
System.out.println("doing 'while (Show.GetArtID(i) != ParsedArtID && i<Show.GetLength())'"+i+" "+Show.GetArtID(i)+" "+Show.GetLength());
ArrayOfID[IDcounter] = ParsedArtID;
System.out.println("entered 'ArrayOfID[IDcounter] = ParsedArtID;'"+ArrayOfID[IDcounter]);
System.out.println("entered 'IDcounter++;'"+IDcounter);
start = finish+1;
System.out.println("entered 'if InputString.length'"+start+" "+finish);
the error shows me that i have a null pointer exception,, when i check the java code generated i found out that the error is in my for loop declaration for (index=1;index<InputString.length();index++)
the browser showed dis...
500 Servlet Exception
at _displayartifact__jsp._jspService(/DisplayArtifact.jsp:108)
at com.caucho.jsp.JavaPage.service(
at com.caucho.jsp.JavaPage.subservice(
at com.caucho.jsp.Page.service(
at com.caucho.server.http.FilterChainPage.doFilter(
at com.caucho.server.http.Invocation.service(
at com.caucho.server.http.CacheInvocation.service(
at com.caucho.server.http.HttpRequest.handleRequest(
at com.caucho.server.http.HttpRequest.handleConnection(

InputString is a string variable and i used its length to check whether the for loop will end or not..
the other suspect i think of the error is with the ArrayOfID error, cause it is a long array i just don't know whether or not i made the correct declaration and intiation of this variable and i just don't know if the line ArrayOfID[IDcounter] = ParsedArtID; is correct or not.. can anyone help me um figure this out?

if you have helped me in my questions, whether dumb or hard.. all i could say is...<br />YOU'RE THE MAN/WOMAN!!!
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
You aren't checking whether InputString is null before trying to call its length() method.

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
nene batungbakal

Joined: Apr 24, 2003
Posts: 16
oh ok, um can i ask how will i check if the variable InputString is null or not? because i already made an if condition checking whether or not InputString.length() is equal to 0, so i made another if condition whether InputString != null... but somehow it still show the same error... null pointer exception..
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5382
Originally posted by nene batungbakal:
InputString.length() is equal to 0, so i made another if condition whether InputString != null...

If InputString is null then you will be calling length() method on a null object and thats why its giving error. To call a method on object you need ref to that object, not a null.
So when InputString is null, you are calling method on null and hence you are getting famous NPE. So first check for null then use any method on that object.
As you have not used [code] tag, I cant read your code ... its too difficult to read code with put formatting.

"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
I agree. Here's the link:
subject: null pointer exception
It's not a secret anymore!