• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Null PointerException

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Bauke. Any directions would be appreciated. I am just learning the jsp/servlet technology
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for the direction.

jones
 
Sheriff
Posts: 67750
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
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.
 
jonas okwara
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67750
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
That is not a question we can answer in a vacuum.
 
jonas okwara
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,?,?,?,?,?)";
}


 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67750
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
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
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
I love a woman who dresses in stainless steel ... and carries tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic