Null Pointers that shouldnt be null

OK this will take some explanation.

You have a sketch called GTC so when Processing runs your sketch it wraps your code up in a class called, wait for it, GTC. Inside your sketch you have declared a number of other classes specifically

  • Circle
  • Line
  • Matrix
  • Point2D
  • Segments

these are called inner classes because they are declared inside another class which is called the enclosing class. Inner classes can see the attributes (variables) of the enclosing class and vice versa.

Now we come to the util class. From now on I will call this class Util, by convention all user defined classes in Java start with a capital letter. I suggest you change your code accordingly.

So the Util class …

You have declared the methods inside the class as static which means you don’t need an instance of the class to use them. This is very common practice for instance the Java Math class has only static methods. The problem is that inner classes cannot have static methods. To overcome this you have declared the Util class static, this raises it from an inner class to a top level class, but it also means that it cannot see anything in the GTC class.

So you can’t use the Point2D and the other inner classes without an object of the enclosing class.

So in your code you had this statement to get an object of the enclosing class
private static GTC gtc = new GTC();
and later used the object like this
Matrix matrix = gtc.new Matrix(d);

When Processing runs your program it will create an instance of GTC which you can access in the main code using this.

The problem with your code is that the Util class creates a second instance of GTC and uses that one instead. This is causing internal errors inside the Processing source code hence the NPEs. In my modification the Util class will use the instance created by Processing - hence happiness reigns :joy:

3 Likes