• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error trying to perform insert into table

 
meenakshi venkateswaran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the value of the "con" variable when you print "Reached here"?
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • 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 ]
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Hi all

Thanks a lot for all your replies!!!

Meenakshi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic