code_junkie

How to trace a NullPointerException in a chain of getters

Nov 13th, 2011
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. someObject.getSomething().getSomethingElse().
  2. getAnotherThing().getYetAnotherObject().getValue();
  3.  
  4. Exception in thread "main" java.lang.NullPointerException
  5. at package.SomeClass.someMethod(SomeClass.java:12)
  6.  
  7. Foo ret1 = someObject.getSomething();
  8. Bar ret2 = ret1.getSomethingElse();
  9. Baz ret3 = ret2.getAnotherThing();
  10. Bam ret4 = ret3.getYetAnotherOject();
  11. int ret5 = ret4.getValue();
  12.  
  13. a.getB().getC().getD().getE();
  14.  
  15. someObject.getSomething()
  16. .getSomethingElse()
  17. .getAnotherThing()
  18. .getYetAnotherObject()
  19. .getValue();
  20.  
  21. public Foo getSomething()
  22. {
  23. Foo result;
  24. ...
  25. if (result == null) {
  26. throw new IllegalStateException("Something is missing");
  27. }
  28. return result;
  29. }
  30.  
  31. someObject.getSomething().getSomethingElse().
  32. getAnotherThing().getYetAnotherObject().getValue();
Add Comment
Please, Sign In to add comment