This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "null pointer exception" Watch "null pointer exception" New topic
Author

null pointer exception

nene batungbakal
Greenhorn

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)
{start=1;
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));
//i=0;
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]);
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
java.lang.NullPointerException
at _displayartifact__jsp._jspService(/DisplayArtifact.jsp:108)
at com.caucho.jsp.JavaPage.service(JavaPage.java:87)
at com.caucho.jsp.JavaPage.subservice(JavaPage.java:81)
at com.caucho.jsp.Page.service(Page.java:474)
at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:166)
at com.caucho.server.http.Invocation.service(Invocation.java:277)
at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:129)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:216)
at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:158)
at com.caucho.server.TcpConnection.run(TcpConnection.java:140)
at java.lang.Thread.run(Thread.java:536)

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
Greenhorn

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: 5371
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.
HTH


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: null pointer exception