# Null Pointers that shouldnt be null

**URL:** <https://discourse.processing.org/t/null-pointers-that-shouldnt-be-null/6752>\
**Category:** Coding Questions\
**Created:** [December 19, 2018, 4:13am UTC](https://discourse.processing.org/t/null-pointers-that-shouldnt-be-null/6752 "2018-12-19T04:13:26Z")\
**Posts on this page:** 1\
**Page:** 2

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [December 20, 2018, 10:48am UTC](https://discourse.processing.org/t/null-pointers-that-shouldnt-be-null/6752/21 "2018-12-20T10:48:04Z")

</div>

> [@CallMeAza](#):
>
> I dont understand why do I need an object GTC and what is that, and why to give it a value from the main, and not on the static class

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 😂

[Previous page](https://discourse.processing.org/t/null-pointers-that-shouldnt-be-null/6752.md?page=1)
