The try catch block cannot suppress Java lang.NullPointerException
•
Java
I got Java Lang.nullpointerexception, I don't know why I tried to use the try catch block, but it didn't work Can anyone explain what's 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); } }
Solution
Try to catch exception instead of sqlexception It will work because exception is the superclass of all exceptions, and by catching it, you will catch all possible exceptions that may be generated in the try block
In any case, the problem may be that conn is null when calling preparestatement(). First, make sure to pass the correct connection instance when calling the pricedatadder method
In addition, please check the log being generated. You should make full use of the logger object
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码