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

Getting NullPointerException

Swapna Gouri Kalanidhi
Ranch Hand

Joined: Mar 14, 2008
Posts: 107
Hi All
I have written a Servlet program and a JSP program. When I'm trying to execute on Tomcat, am getting a NullPointerException. I'm not able to identify the problem. Here's the program:

RegServlet.java


Reg.jsp :



Can anyone tell where the program fails?

Thanks...!


Thanks & Regards
Swapna Gouri
I "try", I "catch" and "finally" WIN !!!
Swapna Gouri Kalanidhi
Ranch Hand

Joined: Mar 14, 2008
Posts: 107
I forgot to mention, the JSP is working fine, i.e , I'm able to enter data on the JSP page but when I click on Submit I'm getting NullPointerException. And the database fields also match....not able to trace out the problem...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Do you not think that letting us know which line is generating the NPE would be a good idea?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Lin Hung Ju
Greenhorn

Joined: Dec 05, 2008
Posts: 3
Can you post your error message (Exception call stack)?
Swapna Gouri Kalanidhi
Ranch Hand

Joined: Mar 14, 2008
Posts: 107
Sorry for not posting the stack trace earlier.....


This is what I get when I run the JSP on the Tomcat(after submitting the data).



And I'm getting this message on the Tomcat console.
Lin Hung Ju
Greenhorn

Joined: Dec 05, 2008
Posts: 3
I think there is a problem when loading your JDBC driver:

1. try to print out the stack trace in init() method:
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql:///test", "root","Varshini@9");
pstmt = con.prepareStatement("INSERT INTO reg VALUES(?,?,?,?)");
} catch (Exception e) {
e.printStackTrace(System.err); //add this line and run again
}

2. if you want to insert records into your database,
you need to invoke the executeUpdate() method of
the PreparedStatement object, so...

pstmt.setInt(1, regid);
pstmt.setString(2, name);
pstmt.setString(3, email);
pstmt.setString(4, mobile);

pstmt.executeUpdate(); //add this line

Please add the line number when you post stack trace again.
Swapna Gouri Kalanidhi
Ranch Hand

Joined: Mar 14, 2008
Posts: 107
You were right Lin, I'm getting a ClassNotFoundException...Look at the stack trace below : @ line no 29 in the RegServlet.java



What could be the problem and what do I need to do to get this work? Kindly suggest....And Thanks for reminding me about the executeUpdate statement....
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Swapna Gouri Kalanidhi:
What could be the problem and what do I need to do to get this work?


hi you need to put mysql driver jar into your tomcat common/lib folder. if you are not having click here to download

Hope this helps
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
hi swapna,




put printing stack trace code here. You will get exact idea (root cause of
the problem). printing just a message like "insert failed" wont help you
much.


cmbhatt
Swapna Gouri Kalanidhi
Ranch Hand

Joined: Mar 14, 2008
Posts: 107
Thanks a bunch SeethaRaman! My code is working now...!
Swapna Gouri Kalanidhi
Ranch Hand

Joined: Mar 14, 2008
Posts: 107
And Thanks to others too who have helped me fix this problem...!
Swapna Gouri Kalanidhi
Ranch Hand

Joined: Mar 14, 2008
Posts: 107
But SeethaRaman, I would like to know how exactly it worked? I mean, when I did not place the mysql driver in Tomcat lib folder, why did I get NPE? What's the connection between both? Can you explain me in brief?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Swapna Gouri Kalanidhi:
I would like to know how exactly it worked?


so that tomcat can find your mysql jar , your application can communicate with your DataBase .for exact detail you can google it


I mean, when I did not place the mysql driver in Tomcat lib folder, why did I get NPE?


you wont get NullPointerException ,instead you get ClassNotFoundException . your code may throw NullPointerException due to operation on null connection .


What's the connection between both? Can you explain me in brief?


please goto tomcat/webapp/tomcate-doc folder in that read jndi-resources-howto.html document

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Swapna Gouri Kalanidhi:
Thanks a bunch SeethaRaman! My code is working now...!


You "try", You "catch" and "finally" You WIN !!!


you are welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting NullPointerException