-1

In javascript when NullPointerException (sort of) occurs in stack trace I can see the method and line where it occurred. However, in Java it just shows the name of the method where the exception occurred without any indication which variable was Null or at which line this occurred. I'm wondering as to why this difference exists? Is it because Javascript is an interpreted language while Java is compiled to bytecode? And when compiled there is no such thing as line or variable?

Update:

05-22 10:53:31.656  31583-31583/com.inreado.reader E/AndroidRuntime﹕ FATAL  
    EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.inreado.reader/com.inreado.reader.TextsListActivity}: java.lang.NullPointerException
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
                at android.app.ActivityThread.access$600(ActivityThread.java:141)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)

And when I go to the ActivityThread.performLaunchActivity(ActivityThread.java:2180) it shows the following:

private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
    // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");

    ActivityInfo aInfo = r.activityInfo;
    if (r.packageInfo == null) {
        (this line is 2180) r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
                Context.CONTEXT_INCLUDE_CODE);
    }

I don't see how to figure out where is NPE since it's my activity that failed.

laalto
  • 150,114
  • 66
  • 286
  • 303
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • 7
    java definitely logs the line number in stack trace, unless you have catched the exception and did not log it yourself. – Abubakkar May 22 '15 at 09:50
  • But not the variable name, as it may not even be a variable that was null. – biziclop May 22 '15 at 09:50
  • That depends, if you have code like: catch(NullPointerException npe){ System.out.println("NULL");} it indeed doesn't show. – Stultuske May 22 '15 at 09:51
  • 1
    However, if you compile your classes without debug information, you won't see line numbers. But the default setting is to include debug information in class files, so unless you explicitly disable it, you'll see line numbers in the stack trace. Except if you've got many NPEs from the same location and [fast throw](http://stackoverflow.com/questions/2411487/nullpointerexception-in-java-with-no-stacktrace) kicks in. – biziclop May 22 '15 at 09:52
  • 1
    Additionally, Java and Javascript have very little to do with each other, in spite of the name similarity. Javascript's "real" name is ECMAScript. – Eric Leibenguth May 22 '15 at 09:58

2 Answers2

1

EDIT: This was posted before the asker updated his question to include actual log output which cleared up the problem. I'll keep the below tips as they are generally helpful in cases where your exception really doesn't get logged.

There are three likely scenario's as also suggested by some comments:

  • The NPE you're talking about is caught in a try-catch block and the output you are referring to is generated by your (or a third party's) own code, ie System.out.println("This method threw an NPE");

  • You are compiling the java files without debug information. This is a flag that you have to activate compile time (default is to compile with debug information).

  • That specific exception in that specific place is thrown so many times that the JVM optimizes the exception in which case stacktrace information is lost (you typically see no stacktrace, only a single line). This can be disabled by adding a specific flag to the JVM arguments. For the sun jvm it's -XX:-OmitStackTraceInFastThrow

Buurman
  • 1,914
  • 17
  • 26
1

You can see the NullPointerException with correct file and line information below in your logcat output, as the "caused by" exception.

Exceptions thrown out from lifecycle callbacks such as Activity onCreate() get wrapped in RuntimeException and what you posted was just the the stacktrace for this wrapper.

laalto
  • 150,114
  • 66
  • 286
  • 303