Skip to main content
added 671 characters in body
Source Link
Stephen C
  • 723.7k
  • 96
  • 851
  • 1.3k

See also: What is a stack trace, and how can I use it to debug my application errors?

What about on Android?

On Android, tracking down the immediate cause of an NPE is a bit simpler. The exception message will typically tell you the (compile time) type of the null reference you are using and the method you were attempting to call when the NPE was thrown. This simplifies the process of pinpointing the immediate cause.

But on the flipside, Android has some common platform-specific causes for NPEs. A very common is when getViewById unexpectedly returns a null. My advice would be to search for Q&As about the cause of the unexpected null return value.

See also: What is a stack trace, and how can I use it to debug my application errors?

What about on Android?

On Android, tracking down the immediate cause of an NPE is a bit simpler. The exception message will typically tell you the (compile time) type of the null reference you are using and the method you were attempting to call when the NPE was thrown. This simplifies the process of pinpointing the immediate cause.

But on the flipside, Android has some common platform-specific causes for NPEs. A very common is when getViewById unexpectedly returns a null. My advice would be to search for Q&As about the cause of the unexpected null return value.

Correct grammatically error
Source Link
Krishna Sony
  • 1.4k
  • 17
  • 27

the statement labelledlabeled "HERE" is going to attempt to run the length() method on a null reference, and this will throw a NullPointerException.

Second observation: when I run the program, it outputs two lines of "gobbledy-gook". WRONG!! That's not gobbledy-gook. It is a stacktrace ... and it provides vital information that will help you track down the error in your code, if you take the time to read it carefully.

In short, the stack trace will tell us unambiguously which statement of the program has thrown the NPE.

This is the hard part. The short answer is to apply logical inference to the evidence provided by the stack trace, the source code, and the relevant API documentation.

In fact, there is only one way: it can only happen if foo has the value null. We then try to run the length() method on null and .... BANG!

Well, if that happened, the stack trace would look different. The first "at" line would say that the exception was thrown in some line in the java.lang.String class, and line 4 of Test.java would be the second "at" line.

So where did that null come from? In this case, it is obvious, and it is obvious what we need to do to fix it. (Assign a non-null value to foo.)

So what about our second scenario? Well, we can see that pos is 1, so that means that foo[1] must be null. Is thatthis possible?

the statement labelled "HERE" is going to attempt to run the length() method on a null reference, and this will throw a NullPointerException.

Second observation: when I run the program, it outputs two lines of "gobbledy-gook". WRONG!! That's not gobbledy-gook. It is a stacktrace ... and it provides vital information that will help you track down the error in your code, if you take the time to read it carefully.

In short the stack trace will tell us unambiguously which statement of the program has thrown the NPE.

This is the hard part. The short answer is to apply logical inference to the evidence provided by the stack trace, the source code and the relevant API documentation.

In fact there is only one way: it can only happen if foo has the value null. We then try to run the length() method on null and .... BANG!

Well, if that happened, the stack trace would look different. The first "at" line would say that the exception was thrown in some line in the java.lang.String class, and line 4 of Test.java would be the second "at" line.

So where did that null come from? In this case it is obvious, and it is obvious what we need to do to fix it. (Assign a non-null value to foo.)

So what about our second scenario? Well, we can see that pos is 1, so that means that foo[1] must be null. Is that possible?

the statement labeled "HERE" is going to attempt to run the length() method on a null reference, and this will throw a NullPointerException.

Second observation: when I run the program, it outputs two lines of "gobbledy-gook". WRONG!! That's not gobbledy-gook. It is a stacktrace ... and it provides vital information that will help you track down the error in your code if you take the time to read it carefully.

In short, the stack trace will tell us unambiguously which statement of the program has thrown the NPE.

This is the hard part. The short answer is to apply logical inference to the evidence provided by the stack trace, the source code, and the relevant API documentation.

In fact, there is only one way: it can only happen if foo has the value null. We then try to run the length() method on null and... BANG!

Well, if that happened, the stack trace would look different. The first "at" line would say that the exception was thrown in some line in the java.lang.String class and line 4 of Test.java would be the second "at" line.

So where did that null come from? In this case, it is obvious, and it is obvious what we need to do to fix it. (Assign a non-null value to foo.)

So what about our second scenario? Well, we can see that pos is 1, so that means that foo[1] must be null. Is this possible?

deleted 44 characters in body
Source Link
A. AMAIDI
  • 6.9k
  • 6
  • 37
  • 53
    public class Test {
        public static void main(String[] args) {
            String foo = null;
            int length = foo.length();   // HERE
        }
    }
            int length = foo.length(); // HERE
    return args[pos].length();
    int length = test(foo, 1);
    
    public class Test {
        public static void main(String[] args) {
            String foo = null;
            int length = foo.length();   // HERE
        }
    }
            int length = foo.length(); // HERE
    return args[pos].length();
    int length = test(foo, 1);
    
public class Test {
    public static void main(String[] args) {
        String foo = null;
        int length = foo.length();   // HERE
    }
}
int length = foo.length(); // HERE
return args[pos].length();
int length = test(foo, 1);
    
Minor grammar improvements
Source Link
Nae
  • 15.5k
  • 8
  • 62
  • 89
Loading
deleted 1 character in body
Source Link
Stephen C
  • 723.7k
  • 96
  • 851
  • 1.3k
Loading
Active reading [<https://en.wiktionary.org/wiki/let%27s#Contraction> <https://en.wiktionary.org/wiki/stack_trace#Noun>]
Source Link
Peter Mortensen
  • 31.2k
  • 22
  • 110
  • 134
Loading
Post Made Community Wiki by Shog9
edited body
Source Link
Stephen C
  • 723.7k
  • 96
  • 851
  • 1.3k
Loading
Rollback to Revision 4
Source Link
Stephen C
  • 723.7k
  • 96
  • 851
  • 1.3k
Loading
deleted 20 characters in body
Source Link
vallentin
  • 26.5k
  • 6
  • 70
  • 93
Loading
added 17 characters in body
Source Link
Stephen C
  • 723.7k
  • 96
  • 851
  • 1.3k
Loading
small typo
Source Link
assylias
  • 330.1k
  • 84
  • 680
  • 806
Loading
added 106 characters in body
Source Link
Stephen C
  • 723.7k
  • 96
  • 851
  • 1.3k
Loading
Source Link
Stephen C
  • 723.7k
  • 96
  • 851
  • 1.3k
Loading