Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException in SimpleTag

 
Arun Somasundaram
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I use tomact 5.5.17 container.
I am getting a NullPointerException when using a simple tag

Here is my code for jsp(simpletag.jsp):-



Here is the code for the class that implements SimpleTagSupport class



Here is the TLD:-
This file is located at employee/WEB-INF/tlds folder.
where employee is the webapp name


When i execute the simpletag.jsp

http://localhost:8080/employee/simpletag.jsp?id=2

I get the follwing error:-



Can anyone give me a solution to get out of this problem.
[ September 15, 2006: Message edited by: Arun Somasundaram ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javaranch tip:

If you are going to post more than a line or two of your code, wrap that
code in a set of UBB Code tags.
Doing so will help to preserve your code's indenting, making it easier to read.
If it is easier to read, more people will actaully read it and you will
stand a better chance of getting help with your question.
See UseCodeTags for more
help with UBB code tags.
 
Arun Somasundaram
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Souther,I have already added my code with in the tags,So what is the reason for asking me to add the code between the code tags.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.lang.NullPointerExceptioncom.Image.doTag(Image.java:66)


So what's in line 66?

A quick look at your code revealed this



Shouldnt the executeQuery() method which returns a ResultSet be assigned to the 'rs' variable



ram.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see a few issues with this code.

1.) Why are you escaping the quotes in the getProperty method in your JSP?


2.) Your never checking to see if the "id" property is null before trying
to use it in your query.

3.) You're not testing rs.next() to see if it's actually returning a row.
Should be:

or if you expect multiple rows:



4.) Java exceptions will be more help to you if you print the entire stack trace:



Before writing to forums or lists to help with debugging a
NullPointerException, you should test all of your variables with simple
System.out.println(varName) statements to see which one is null.
Also, that stacktrace has line numbers for the the offending code.

Some of the lines are in your tag but some are in the generated servlet
code (which in Tomcat lives under the {tomcat install}/work directory.
These line numbers should help narrow down the search for you.



Also...

Once you get past this NullPointerException, you're probably going to run in to an IllegalStateException because you're trying to mix binary and texual output in the same response.
See: http://faq.javaranch.com/view?IllegalStateException

Custom tag or not, it is never a good idea to try to stream binary data from a JSP. This should be done from a servlet.
[ September 15, 2006: Message edited by: Ben Souther ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Somasundaram:
Ben Souther,I have already added my code with in the tags,So what is the reason for asking me to add the code between the code tags.


One reason for the code tags is the preservation of your code's indenting.
Since your code has no indenting, my first assumption was that there were no
code tags.
Had I looked closer, I would have seen that you did actually use them.

Do you really write your code without indenting?
[ September 15, 2006: Message edited by: Ben Souther ]
 
Arun Somasundaram
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to assign the ResultSet to its variable while posting.
But the code is right.I have corrected it in the posting.
 
Arun Somasundaram
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben I have checked the id , and the rs these are not null.But I can't able to find where the actual mistake lies.

Here is the stack trace from my logs directory.
 
Arun Somasundaram
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben I have checked the id , and the rs these are not null.But I can't able to find where the actual mistake lies.

Here is the stack trace from my logs directory.

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at org.apache.jsp.tag2_jsp._jspService(tag2_jsp.java:63)

Hint: Go back and read all of my earlier response if you're not sure what this means.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic