• 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

Servlet NullPointerException

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I don't know if this is the appropriate place to post because I still consider this as a beginners question., but still the problem is servlet related. I have two classes ("ChatServlet.class" and "SAXHelper.class") which reside in package called "wm.macijewski". The servlet application ("ChatServlet.class") basically attempts to connect to database and retrieve some values, "SAXHelper.class" is a class which uses SAX API to retrieve some values from .XML document (as you might guess, these values are necessary information to connect to database). I have compiled these files with the following compiler options: "javac -d . SAXHelper.java" and javac -cp "/home/okti/apache-tomcat-7.0.32/lib/servlet-api.jar":"/home/okti/workspace/ChatServlet/src/wm/macijewski/" ChatServlet.java.

Copied those two .class files into "/home/okti/apache-tomcat-7.0.32/webapps/ChatServlet/WEB-INF/classes/wm/macijewski/". My WEB.xml file looks like this:


When i try to execute the servlet, I get:

java.lang.NullPointerException
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:186)
wm.macijewski.ChatServlet.createUser(ChatServlet.java:104)
wm.macijewski.ChatServlet.doGet(ChatServlet.java:80)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

I'm pretty sure the problem is, that server cannot initiate (or find) my class. But why? For me, it looks like I have placed them in correct directories, that is: /classes/wm/macijewski, and if the server fails to find my class, so which one? is it ChatServlet.class or SAXHelper.class? Also, I don't think the problem is within my code, because I have tried doing this with "normal" applications and it works fine.

Any Help is greatly appreciated!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Waldemar Macijewski wrote:
I'm pretty sure the problem is, that server cannot initiate (or find) my class.


Nope. The stack trace clearly shows that the NPE is occurring inside the servlet. It's even giving you the line number where it is occurring.
 
Waldemar Macijewski
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought the problem was with the server for sure!. Anyway here are some code samples from these files:

ChatServlet.java (please ignore the name, it has nothing do with any chat, this just comes from my "rough notes" folder, so I just usually came up with any name)



SAXHelper.java



I have omitted some lines of code as they contain irrelevant information. Also I have placed "Data.xml" file in /webapps/ChatServlet/ folder.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Waldemar Macijewski wrote:


my wild guess is that SAXHelper.getDriver() is null
 
Waldemar Macijewski
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:

my wild guess is that SAXHelper.getDriver() is null


Well yes it is, but I still don't understand why? I have copied content of SAXHelper.java and used it with ordinary Java application, and I have tried exactly the same connect to database, and it worked fine..

 
Waldemar Macijewski
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nah there was problem in the code after all. InputSource constructor should point to fully qualified path name of file, not just "Data.xml" how I did. I didn't see this because, i haven't handled all exceptions (in ChatServlet.java) which might occur when parsing the file, and when I did this, I saw that there was IOException all the time.
 
reply
    Bookmark Topic Watch Topic
  • New Topic