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

Error trying to perform insert into table

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am trying to insert a row of data into a database but I keep getting an exception..The code snippet as well as the exception are as follows:
public void insertQuestions() {
PreparedStatement s;
ResultSet rs;
SurveyVec surveyVec = new SurveyVec();
try {
System.out.println("Reached here");
s = con
.prepareStatement("INSERT INTO question (number, position, surveyname, question, answerchoice, answer) VALUES ('1', '2', 'a', 'b', 'c', 'd')");
s.setInt(1, surveyVec.getNumber());
s.setInt(2, 2);
s.setString(3, surveyVec.getName());
s.setString(4, surveyVec.getQn());
s.setString(5, surveyVec.getAnschoice());
s.setString(6, surveyVec.getAnswer());
String ss = s.toString();
System.out.println("ss is " + ss);
s.executeUpdate();
rs = s.getResultSet();
while (rs.next()) {
System.out.println(rs.getString(1) + " " + rs.getString(2)
+ "<br>");
}

} catch (SQLException sqlEx) {
System.out.println("SQLException 1 roblem reading rows "
+ "from the Account Table:" + sqlEx.getMessage() + ":"
+ sqlEx.getErrorCode());
}

The error is :
java.lang.NullPointerException
surveysys.DBClass.insertQuestions(DBClass.java:42)
surveysys.SurveyController.doPost(SurveyController.java:56)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

Line 42 of my DBClass is the line "prepareStatement("INSERT INTO question (number, position, surveyname, question, answerchoice, answer) VALUES ('1', '2', 'a', 'b', 'c', 'd')"); "

I dont understand y a NullPointerException is being thrown.

Can someone please throw light on this?

Thanks in advance
Meenakshi
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the value of the "con" variable when you print "Reached here"?
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you doing setXxx()??
Probably your "con" object is null.

What are you trying to do. You are intantiated a bean and then straight trying to get its properties. What are you expecting?
[ March 11, 2005: Message edited by: Adeel Ansari ]
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by meenakshi venkateswaran:

s = con.prepareStatement("INSERT INTO question (number, position, surveyname, question, answerchoice, answer) VALUES ('1', '2', 'a', 'b', 'c', 'd')");




meenakshi

you connecdtion could be null, but do modify your code as adeel said
What is doing sexXXX() ?

seems you have copied your test query in your code.

make your prepared statement as




hope this helps
 
meenakshi venkateswaran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

Thanks a lot for all your replies!!!

Meenakshi
 
Hey! You're stepping on my hand! Help me tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic