FANDOM


Overview

This error will occur when java tries to access an object which has not yet been instantiated. So, for example, if you declare a field

Joystick js;

you must make sure to say

js = new Joystick(0);

otherwise, a NullPointerException will be thrown.

The more low-level reason behind this error is that behind the scenes, objects are essentially memory addresses. When declared and not instantiated, the object "points to" the memory address 0 (i.e. null). Unfortunately (or perhaps fortunately), attempts to access data stored at 0 are illegal (the java police will arrest you). So an address must be assigned. The keyword new does just that: it allocates a space in memory and can be used to store it in an object. If you know someone who knows C or C++, they might be able to explain this better.

How to Fix It

This is an easy but tedious fix. Make sure that every time you use a variable, it has been instantiated previously. It is actually pretty easy to forget to do this with class variables, so make sure that in a method that always run (such as the init methods), each variable is initialized. Good luck...

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.