A null pointer error is caused when we are calling on a variable that has not been created at the time that we are running out code. This occurs usually in two cases. The first is that you might have initialized a variable after a different variable that uses the first material. The second is that you did not initialize the variable at all.

Initialized after variable

For instance, if a tool material is initialized after the tool that uses it, it produces a null pointer error in the crash log. The fix is to move the line that initializing that the tool material to the line above the one that initializes the tool. An example of this would look like the following. 

This causes an error as in our CustomSword.java we have:

So our CustomSword is trying to use myToolMaterial, but since myToolMaterial has not been initialized yet, it is considered Null. 

To fix this issue, simply switch the order of the code so that the material is initialized before the item or armor. Such as below:

Once the material is being initialized before the item or armor, test if it works. 

Never Initialized Variable

If you have looked through your Initialize Here section and see that you are missing one of the Variables that you declared in your Declare Here section, then make sure to add the initialization for that variable in the correct order for the section. For instance if you forgot to initialize your ToolMaterial it will need to be initialized before any tools or weapons. If you do not remember how to initialize the variable you are working with, then look for a variable of the same type in your Initialize Here section or review the videos to see the code. 

Did this answer your question?