3

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);
        }
    }
magic_frank
  • 161
  • 1
  • 9
  • Do you want to know a) why you are getting a NPE or b) why it's not being caught? a) Not possible to say with the information you've given but conn may be null b) you're catching a SQLException not a NPE – Martin Wilson Dec 08 '12 at 19:41
  • Probably the `conn` getting passed in is null; _there's_ your problem. – Louis Wasserman Dec 08 '12 at 19:41
  • Do NOT try to catch a NullPointerException. Fix your code instead. To figure out why an exception is thrown, use a debugger. In modern IDEs, you can also add Java Exception Breakpoints to your debugger if you'd only like to stop running your code and check what is happening when the given exception is thrown. – Sk8erPeter Jan 23 '21 at 08:07

3 Answers3

6

Try catching Exception instead of SQLException. It will work because Exception is the superclass of all exceptions, and by catching it you're catching all possible exceptions that might get generated inside the try block.

Anyway, the problem is probably caused because conn is null at the point where prepareStatement() is called, start by making sure that you're passing a correct instance of Connection where the PriceDataAdder method is called.

Also take a look at the log that's being generated, that Logger object should be put to good use.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
4

You are only catching SQLException objects, and NullPointerException is not a subclass of SQLException. You can insert NullPointerException| to catch those also; see http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html.

Emily
  • 5,869
  • 1
  • 22
  • 15
2

You should manage your exception like the below code

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);
    } catch (NullPointerException ex) {
        Logger.getLogger(PriceDataAdder.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception ex) {
        Logger.getLogger(PriceDataAdder.class.getName()).log(Level.SEVERE, null, ex);
    }
Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
  • Don't catch NullPointerException, but prevent it in the first place by ensuring 'conn' is not null. – mhaller Dec 08 '12 at 20:57
  • @mhaller Thats true, but still we have to catch that exception also. – Bhavik Ambani Dec 09 '12 at 01:49
  • Nonsense, you do not have to catch NullPointerException. In fact, you should really not catch it in this case, since it is a programming mistake to use 'conn' although it's null. Catchine NPE here is just hiding the real bug. – mhaller Dec 09 '12 at 02:12
  • @mhaller Yes you are right, still we have to catch if not then programm execution will be stopped. – Bhavik Ambani Dec 09 '12 at 02:13
  • Bhavik, please read http://stackoverflow.com/questions/2586290/is-catching-a-null-pointer-exception-a-code-smell and http://stackoverflow.com/questions/4716353/if-catching-null-pointer-exception-is-not-a-good-practice-is-catching-exception and http://stackoverflow.com/questions/4778439/is-it-bad-style-to-use-nullpointerexception-to-test-for-null – mhaller Dec 09 '12 at 02:18