This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Getting NullPointerException  RSS feed

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...!
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Author and ninkuma
Marshal
Posts: 66789
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you not think that letting us know which line is generating the NPE would be a good idea?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post your error message (Exception call stack)?
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a bunch SeethaRaman! My code is working now...!
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Thanks to others too who have helped me fix this problem...!
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!