Find out what variable is throwing a NullPointerException programatically

Posted by Hectoret on Stack Overflow See other posts from Stack Overflow or by Hectoret
Published on 2010-04-19T13:55:10Z Indexed on 2010/04/19 14:03 UTC
Read the original article Hit count: 237

I know I can find out if a variable is null in Java using these techniques:

  • if (var==null) -> too much work
  • try { ... } catch (NullPointerException e) { ...} -> it tells me what line is throwing the exception
  • using the debugger -> by hand, too slow

Consider this line of code:

if (this.superSL.items.get(name).getSource().compareTo(VIsualShoppingList.Source_EXTRA)==0)  {

I would like to know if there's a generic way to find out programatically what variable (not just the line) is throwing the NullPointerException in a certain area of code. In the example, knowing that

© Stack Overflow or respective owner

Related posts about java

Related posts about debugging