• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

null pointer exception

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You aren't checking whether InputString is null before trying to call its length() method.
 
nene batungbakal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic