I am getting the java.lang.NullPointerException and i am not sure why. I am trying to use a try-catch block but it does not work. Can anyone explain what is wrong with this block?
public PriceDataAdder(Connection conn) {
try {
this.conn = conn;
statement = conn.prepareStatement("INSERT INTO `blah` (a,b,c,d,e,f,g,h,`i`,j) VALUE( ?,?,?,?,?,?,?,?,?,? )");
} catch (SQLException ex) {
Logger.getLogger(PriceDataAdder.class.getName()).log(Level.SEVERE, null, ex);
}
}
Try catching
Exceptioninstead ofSQLException. It will work becauseExceptionis the superclass of all exceptions, and by catching it you’re catching all possible exceptions that might get generated inside thetryblock.Anyway, the problem is probably caused because
connisnullat the point whereprepareStatement()is called, start by making sure that you’re passing a correct instance ofConnectionwhere thePriceDataAddermethod is called.Also take a look at the log that’s being generated, that
Loggerobject should be put to good use.