aspose file tools*
The moose likes Servlets and the fly likes Null PointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Null PointerException" Watch "Null PointerException" New topic
Author

Null PointerException

jonas okwara
Ranch Hand

Joined: Jun 22, 2004
Posts: 58

Hi,

I have a jsp/servlet/JDBC application deployed on Tomcat 5.5. At the work place it runs fine without problems but at home I get

a Nullpointer Exception. I even tarred up the entire tomcat directory at work with the application and moved it to my system at home and got the same problem.

I have worked on this for 3 weeks still I cannot get the application to work at home neither have I been able to fish out what the problem is.

Please has anyone encoutered this before ? I will appreciate some directions here.

thanks
jones
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
In fact you just need to look at the first line of the stacktrace of the NPE to find out the class/method/codeline where it is been thrown and finally fix the code logic/flow accordingly.

Do you understand at any way when a NPE would occur and how you could fix it?
jonas okwara
Ranch Hand

Joined: Jun 22, 2004
Posts: 58
Thank you Bauke. Any directions would be appreciated. I am just learning the jsp/servlet technology
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Start here: http://www.0xcafefeed.com/2004/06/of-thread-dumps-and-stack-traces/
It's not part of JSP/Servlet knowledge though, but just a trivial part of basic Java.
jonas okwara
Ranch Hand

Joined: Jun 22, 2004
Posts: 58

Thanks for the direction.

jones
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

jonas, just saying "I get an error" isn't enough information for use to help you. We need to see the details of the error message and of the code that's causing it. Please read this for more information.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
jonas okwara
Ranch Hand

Joined: Jun 22, 2004
Posts: 58

Thanks. the problem is why does is run fine on one tomcat platform but give errors on another. Even when I copied the entire

tomcat directory .

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

That is not a question we can answer in a vacuum.
jonas okwara
Ranch Hand

Joined: Jun 22, 2004
Posts: 58
thanks Bear below are the NPE and DAO code

The NPE:
SEVERE: Servlet.service() for servlet Register threw exception
java.lang.NullPointerException
at Uxdomain.StudentDAO.insert(StudentDAO.java:19)
at Uxweb.RegistrationServlet.processRequest(RegistrationServlet.java:51)
at Uxweb.RegistrationServlet.doPost(RegistrationServlet.java:26)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)






The Data Access Class:
My DAO data access code:
public void insert(String name, String phone, String email, String gender, String amountpaid) {
try {
Connection conn = null;
conn= connPool.mysqlConnection();
PreparedStatement stmt = conn.prepareStatement(INSERT_STMT);
stmt.setString(1, name);
stmt.setString(2, phone);
stmt.setString(3, email);
stmt.setString(4, gender);
stmt.setString(5, amountpaid);
stmt.executeUpdate();

}catch (SQLException se) {
System.out.println("Exception:" + se.getMessage());
}

}

private static final String INSERT_STMT="INSERT INTO studentInfo(studID, name, phone, email, gender, amountpaid) VALUES (0,?,?,?,?,?)";
}


Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


at Uxdomain.StudentDAO.insert(StudentDAO.java:19)


The error at 19 line. check what object is used up in the line , and the probable cause is that its null.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Sigh. Please use code tags; read this for more information.

Which line is 19? The harder you make it for people to help you, the less help you'll get.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
jonas okwara wrote:
java.lang.NullPointerException
at Uxdomain.StudentDAO.insert(StudentDAO.java:19)

You didn't have read my link at all and you actually don't understand at all when a NPE will be thrown? That's too bad.

You know that you can assign null to a reference, do you?

Can you tell what will happen when you invoke s.length() or any other method on that? Can you also explain why that happens?

The same is happening in your code. It's very simple, one of the most simplest exceptions to hunt and solve. The first line of the stacktrace tells that it's the line 19 of StudentDAO class, inside the insert() method where an object reference is been invoked while it is null. Fixing it is simple: just make sure that it is not null, or bypass the invocation.
jonas okwara
Ranch Hand

Joined: Jun 22, 2004
Posts: 58
Bauke, Bear and Balu,

I really appreciate your direction and good words. Bauke I have printed that excellent article at 0xCAFEFEED and will certainly keep me engaged this weekend. I have been worked up by my failure to resolve this that I failed to see the error right under my nose.

Thanks again
jones
 
Consider Paul's rocket mass heater.
 
subject: Null PointerException