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