0

The following code doesn't show any error and the code executes but the data that is calculated and stored in rfv is not updated in the database.

try{
  Class.forName("com.mysql.jdbc.Driver").newInstance();
  com.mysql.jdbc.Connection con = (com.mysql.jdbc.Connection)  DriverManager.getConnection("jdbc:mysql://localhost:3308/rk","root","root");
  ResultSet rsfact=null;
  PreparedStatement psfact=(PreparedStatement) con.prepareStatement("SELECT * FROM fact");
  PreparedStatement psgraph=(PreparedStatement) con.prepareStatement("INSERT INTO graph values(?,?,?,?)");
  int cp,ic,rc,fi,ct,tr;
  while(rsfact.next())
  {
      cp=rsfact.getInt("1");
      ic=rsfact.getInt("2");
      rc=rsfact.getInt("3");
      fi=rsfact.getInt("4");
      ct=rsfact.getInt("5");
      tr=rsfact.getInt("6");
      int rfv;

      rfv=(cp+ic+rc+fi+ct+tr)/6;
      graph.setInt(1, rfv);
      graph.executeUpdate();
  }
 }
 catch(Exception e){
 System.out.println(e);
 }

I checked the code and everything seems to be fine for me but i couldn't figure out the reason for the data which is not inserted into the table. I get the following when this is executed

java.lang.NullPointerException

 try{
   Class.forName("com.mysql.jdbc.Driver").newInstance();
    com.mysql.jdbc.Connection con = (com.mysql.jdbc.Connection)        DriverManager.getConnection("jdbc:mysql://localhost:3308/rk","root","root");
   ResultSet rsfact=null;
  ResultSet rsfault=null;
  ResultSet rspriority=null;
  ResultSet rsreq=null;
  PreparedStatement psfact=(PreparedStatement) con.prepareStatement("SELECT * FROM fact");
  PreparedStatement psfault=(PreparedStatement) con.prepareStatement("SELECT * FROM fault");
  PreparedStatement pspriority=(PreparedStatement) con.prepareStatement("SELECT * FROM priority");
  PreparedStatement psreq=(PreparedStatement) con.prepareStatement("SELECT * FROM req");
  PreparedStatement graph=(PreparedStatement) con.prepareStatement("INSERT INTO gr values(?)");
  int cp,ic,rc,fi,ct,tr;
  rsfact = psfact.executeQuery();
  rsfault=psfault.executeQuery();
  rspriority=pspriority.executeQuery();
  rsreq=psreq.executeQuery();
  while(rsfact.next())
  {
      cp=rsfact.getInt(1);
      ic=rsfact.getInt(2);
      rc=rsfact.getInt(3);
      fi=rsfact.getInt(4);
      ct=rsfact.getInt(5);
      tr=rsfact.getInt(6);
      int rfv;

      rfv=(cp+ic+rc+fi+ct+tr)/6;
      graph.setInt(1, rfv);
      graph.executeUpdate();
  }
  while(rsfault.next()){
  cp=rsfact.getInt(1);
      fic=rsfact.getInt(2);
      frc=rsfact.getInt(3);
      ffi=rsfact.getInt(4);
      fct=rsfact.getInt(5);
      ftr=rsfact.getInt(6);
      int tsfv;

      tsfv=((fcp*2)+(fic*3)+(frc*4)+(ffi*3.14)+(fct-4)+(ftr+9))/6;
      graph.setInt(2, tsfv);
      graph.executeUpdate();

  }
 }
4
  • can you please tell what is stack trace... sorry if this is a silly question. i don't know what is stack trace.
    – radish
    Sep 28, 2012 at 19:31
  • Stack trace is full error message you get in Logcat.
    – kosa
    Sep 28, 2012 at 19:32
  • i get only this "java.lang.NullPointerException"
    – radish
    Sep 28, 2012 at 19:34
  • This is not an answer, just a small point. You don't need to convert con.preparedstatement() to preparedstatement ( " PreparedStatement psfact=(PreparedStatement) con.prepareStatement("SELECT * FROM fact"); ") . That method returns a PreparedStatement , so converting it is useless.
    – PeakGen
    Sep 29, 2012 at 19:15

1 Answer 1

1

you are not instatiating rsfact(ResultSet object) and trying to invoke methods of result set thus NULLPOINTEREXCEPTION.

    ResultSet rsfact=null; // **rsfact is null**
       int colCount=1;
      PreparedStatement psfact=(PreparedStatement) con.prepareStatement("SELECT * FROM fact");
      PreparedStatement psgraph=(PreparedStatement) con.prepareStatement("INSERT INTO graph values(?,?,?,?)");
      int cp,ic,rc,fi,ct,tr;
      rsfact = psFact.executeQuery(); **// this instantiates ResultSet, if you dont includee this statement rsfact would still be null.**
while(rsfact.next()) // **if rsfact null and u execute this line it would throw nullpointer exception(cuz rsfact is null)**
  {
      cp=rsfact.getInt("1");
      ic=rsfact.getInt("2");
      rc=rsfact.getInt("3");
      fi=rsfact.getInt("4");
      ct=rsfact.getInt("5");
      tr=rsfact.getInt("6");
      int rfv;

      rfv=(cp+ic+rc+fi+ct+tr)/6;
      graph.setInt(colCount, rfv);
       colCount++;
}
      graph.executeUpdate();
8
  • Now i get this error "java.sql.SQLException: No value specified for parameter 2" in stack trace
    – radish
    Sep 28, 2012 at 19:40
  • but here i need to update only one column of that particular table
    – radish
    Sep 28, 2012 at 19:47
  • @radish if you wanna insert only one column in graph table then change your insert like this INSERT INTO graph values(?)" Sep 28, 2012 at 19:49
  • i need to update only one column inside this while loop and the second column in the the next while loop and the third in third while and fourth in fourth
    – radish
    Sep 28, 2012 at 19:49
  • 1
    @radish i updated the code. try now. you should have an int variable colCount initialized to 1 and on every iteration you increment it by 1. and also have executeUpdate() outside while. :) Sep 28, 2012 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.