Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>NullPointerException</code>s are exceptions that occur when you try to use a reference that points to no location in memory (null) as though it were referencing an object. Calling a method on a null reference or trying to access a field of a null reference will trigger a <code>NullPointerException</code>. These are the most common, but other ways are listed on the <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html" rel="noreferrer"><code>NullPointerException</code></a> javadoc page.</p> <p>Probably the quickest example code I could come up with to illustrate a <code>NullPointerException</code> would be:</p> <pre><code>public class Example { public static void main(String[] args) { Object obj = null; obj.hashCode(); } } </code></pre> <p>On the first line inside <code>main</code>, I'm explicitly setting the <code>Object</code> reference <code>obj</code> equal to <code>null</code>. This means I have a reference, but it isn't pointing to any object. After that, I try to treat the reference as though it points to an object by calling a method on it. This results in a <code>NullPointerException</code> because there is no code to execute in the location that the reference is pointing.</p> <p>(This is a technicality, but I think it bears mentioning: A reference that points to null isn't the same as a C pointer that points to an invalid memory location. A null pointer is literally not pointing <em>anywhere</em>, which is subtly different than pointing to a location that happens to be invalid.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload