Question: What causes a NullPointerException (NPE)?As you should know, Java types are divided into primitive types ( Show
A
the statement labeled
"HERE" is going to attempt to run the There are many ways that you could use a
Question: How do I read the NPE stacktrace?Suppose that I compile and run the program above:
First observation: the compilation succeeds! The problem in the program is NOT a compilation error. It is a runtime error. (Some IDEs may warn your program will always throw an exception ... but the standard 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. So let's look at what it says:
The first line of the stack trace tells you a number of things:
The second line is the most important one in diagnosing an NPE.
This tells us a number of things:
If you count the lines in the file above, line 4 is the one that I labeled with the "HERE" comment. Note that in a more complicated example, there will be lots of lines in the NPE stack trace. But you can be sure that the second line (the first "at" line) will tell you where the NPE was thrown1. In short, the stack trace will tell us unambiguously which statement of the program has thrown the NPE. See also: What is a stack trace, and how can I use it to debug my application errors? 1 - Not quite true. There are things called nested exceptions... Question: How do I track down the cause of the NPE exception in my code?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. Let's illustrate with the simple example (above) first. We start by looking at the line that the stack trace has told us is where the NPE happened:
How can that throw an NPE? In fact, there is only one way: it can only happen if But (I hear you say) what if the NPE was thrown inside the 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 So where did that OK, so let's try a slightly more tricky example. This will require some logical deduction.
So now we have two "at" lines. The first one is for this line:
and the second one is for this line:
Looking at the first line, how could that throw an NPE? There are two ways:
Next, we need to figure out which of those scenarios explains what is actually happening. We will start by exploring the first one: Where does So what about our second scenario? Well, we can see that Indeed it is! And that is the problem. When we initialize like this:
we allocate a 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 What does += mean in C++?+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand. C += A is equivalent to C = C + A.
Which of the following characters is used to specify that a line of code continues on the line below it?Use a backslash ( \ ) as a line continuation character
If a backslash is placed at the end of a line, it is considered that the line is continued on the next line. Only string literals (string surrounded by ' or " ) are concatenated if written consecutively.
When a variable's name contains two or more words most C++ programmers enter the name using what type of notation?Many programmers prefer to use all lowercase letters for their variable names. If the name requires two words (for example, my car), two popular conventions are used: my_car or myCar. The latter form is called camel notation because the capitalization looks something like a camel's hump.
What's the name for the statements that collectively make up a program?A function can be thought of as a GROUP of ONE or more programming statements that collectively has a name.
|