GeeCON Prague 2014*
The moose likes Servlets and the fly likes Servlet NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet NullPointerException" Watch "Servlet NullPointerException" New topic
Author

Servlet NullPointerException

Waldemar Macijewski
Ranch Hand

Joined: Jun 22, 2012
Posts: 32
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!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61299
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Waldemar Macijewski
Ranch Hand

Joined: Jun 22, 2012
Posts: 32
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.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Waldemar Macijewski wrote:


my wild guess is that SAXHelper.getDriver() is null
Waldemar Macijewski
Ranch Hand

Joined: Jun 22, 2012
Posts: 32
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

Joined: Jun 22, 2012
Posts: 32
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.
 
GeeCON Prague 2014
 
subject: Servlet NullPointerException