Getting null pointer exception error in class using PVector

Java’s non-primitive datatypes, like String and PVector, defaults to null when we declare a variable:

So before using a non-primitive variable, for example invoking any of its methods as you did at location.set(_location);, we have to make sure we’ve already initialized it.

That’s why I’ve assigned a PVector to location: final PVector location = new PVector();

Just to make sure those variables can’t be unintentionally re-assigned later on:

Its usage is completely optional, of course.

1 Like