0

My java code throws exception e which is null (debug shows e==null)

How can the code catch this at:

} catch (IOException e) {
  e.printStackTrace();
}

If e is null how can it have a type?

enter image description here

Daniel Larsson
  • 6,278
  • 5
  • 44
  • 82
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • 5
    How have you verified that `e` is `null` *inside* the `catch` block? If the breakpoint is stopped on the `catch` line itself, `e` would be `null` at that time. But inside the block `e` would have a value. – David Jan 03 '14 at 21:20
  • 5
    Impossible. What you're seeing is a flaw in the debugger (I've seen a few interesting situations with Eclipse's debugger myself). – Kayaman Jan 03 '14 at 21:21
  • Can you confirm that `IOException` isn't actually `Exception` or `RuntimeException` in your snippet? That's the only case where what you're saying could happen. Also, try adding a simple `System.out.println(e)` (or log or whatever) and see what you get. – arshajii Jan 03 '14 at 21:22
  • Post the rest of the code, because that is not possible. (Of course, if it is, you could wrap `try {e.printStackTrace();} catch (NullPointerException npe) {}`) – Elliott Frisch Jan 03 '14 at 21:22
  • see my photo attached – Elad Benda Jan 03 '14 at 21:23
  • 1
    Try `if(e==null) ...` – Franz Ebner Jan 03 '14 at 21:25
  • Is UserRecoverableAuthIOException a subclass of IOException? If so, is it possible that the second exception is being thrown after the first has consumed/nullified the exception instance? – Bill Horvath Jan 03 '14 at 21:31
  • 1
    What most debuggers display is e.toString(). If you have an exception object whose toString() is returning null (or "null"), that might explain what you're seeing. What _type_ does the debugger say e is? One other possibility is that your source code doesn't match the class currently executing, and you're at a different line than you think you are. I've been caught out that way. – keshlam Jan 03 '14 at 21:32
  • @keshlam please write an answer. I will mark you as solving answer – Elad Benda Jan 03 '14 at 21:35
  • 1
    So, just out of curiousity, which one was the problem? – keshlam Jan 03 '14 at 21:54
  • your option (1). the real exception was `com.google.android.gms.auth.GoogleAuthException: Unknown` and it wasn't solved: http://stackoverflow.com/questions/20908174/cannot-upload-to-google-drive-from-my-android-app – Elad Benda Jan 03 '14 at 21:56

1 Answers1

1

Two ideas occur to me:

1) What most debuggers display is e.toString(). If you have an exception object whose toString() is returning null (or "null"), that might explain what you're seeing. What type does the debugger say e is?

2) Another possibility is that your source code doesn't match the class currently executing, and you're at a different line than you think you are. I've been caught out that way.

keshlam
  • 7,931
  • 2
  • 19
  • 33