0

I have the following Code that throws a null pointer exeption

public void checkAcc(String sql)
{
    System.out.print(sql);   // the statement executed is "SELECT AccountID FROM BankAccount"    
    try {
        stmt.executeQuery(sql);

    } catch (Exception e) {
        System.err.println("Nope that is broken - " + e);
    }
}
4
  • First, you don't catch "Exception". [That's a bad idea] (nekulturniy.com/Writings/RebelWithoutAClause/…). And I must guess the line that's giving you the exception is stmt.executeQuery(sql);? Also post the stack trace. Commented Feb 28, 2012 at 13:21
  • Put a break point in and step through it. Commented Feb 28, 2012 at 13:21
  • Yes, i beleive that it is that line, how do you catch exeption? I am fairly new to java Commented Feb 28, 2012 at 13:22
  • stmt is null. Where have you initialized this statement object? Commented Feb 28, 2012 at 13:26

1 Answer 1

2

Have you checked to ensure that stmt isn't null? The way that function is written, it must be a field, and you have to make certain that it has been initialized before checkAcc() is called.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it was null due to part of the program not running when it was supposed to. Now I feel slightly Silly. But thanks!
If this answer solved your problem, please click the checkmark to note it as the accepted answer. Thanks!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.