# NullPointerException when calling fill() from another class

**URL:** <https://discourse.processing.org/t/nullpointerexception-when-calling-fill-from-another-class/24594>\
**Category:** Coding Questions\
**Created:** [October 14, 2020, 11:13am UTC](https://discourse.processing.org/t/nullpointerexception-when-calling-fill-from-another-class/24594 "2020-10-14T11:13:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![S\_V](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@S\_V](https://discourse.processing.org/u/S_V)\
**Post date:** [October 14, 2020, 11:13am UTC](https://discourse.processing.org/t/nullpointerexception-when-calling-fill-from-another-class/24594/1 "2020-10-14T11:13:16Z")

</div>

Hi,  
I’m new to processing and trying to learn the basics of it. However I’m having this weird error which is hindering me from properly organizing my code.  
Basically: I have a base class which has functions for drawing stuff (DrawRect, DrawCircle, DrawText etc), in those functions the appropriate functions of Processing are called (rect, ellipse etc).  
Next I have a class which extends this base class. So I can call DrawRect from there and it works properly, I have no problems with that.  
However I start encountering errors when I created another class called “Button” (which also extends the base class), in this class I wanted to implement all the functions that check for hovering and clicking and that kind of stuff. So I would just have to call button1.drawHover and it would draw new square if the mouse was hovering over the button.

So the problem is when I try to call DrawRect from the Button class itself, I get a nullpointer exception. Even when printing all the values directly before the rect() call in my Base Class, and seeing that the values are properly assigned, I still get a NullPointerException. I encounter the same error with every other drawing function, like fill().

Base class:

```auto
class GameBase extends PApplet {
....

  def drawRectangle(r: Rectangle): Unit =
    rect(r.left,r.top, r.width, r.height, r.radii)

  def setFillColor(c: Color): Unit = {
    println(c.red.toString + " " + c.green.toString + " " + c.blue.toString + " " + c.alpha.toString + " ")
    fill(c.red, c.green, c.blue, c.alpha)
  }

...

```

Button class:

```auto
class Button(a : Int, b : Int, c : Int, d : Int) extends GameBase{
  ....

 def drawHover(mouseX : Int, mouseY : Int, color : Color = Color.LightGray, radii : Int = 5): Unit = {
   if(testMouse(mouseX, mouseY)) {
     setFillColor(color)
     val Rect = Rectangle(Point(a,b),c,d) // a,b are the coordinates, c is width, d is height
     drawRectangle(Rect)
   }
 }
}

```

Error:

```auto
98.0 98.0 98.0 255.0 (Print from setFillColor just before the crash, showing the values are valid)
java.lang.NullPointerException
	at processing.core.PApplet.fill(PApplet.java:14780)
	at engine.GameBase.setFillColor(GameBase.scala:67)
	at snake.game.Button.drawHover(Button.scala:13)
	at snake.game.SnakeGame.$anonfun$settingsPage$3(SnakeGame.scala:98)
	at snake.game.SnakeGame.$anonfun$settingsPage$3$adapted(SnakeGame.scala:98)

```

both setFillColor and DrawRectangle throw an error (like if I remove setFillColor, then DrawRectangle will error)

And well the code for MainClass just works, its pretty much the same code, don’t really need to paste it in here I think

A short explanation of what I mean:

 ![base](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/35f55d2aca2dda6c1f9f75faf5717471e77a6c07.png)

I also tried having Button extends MainClass, and then call a function from in MainClass (from Button) that draws for me. Same error

I’m guessing I’m probably doing something wrong with my file structure? Do I perhaps need to declare something within the Button Class to tell it to use the same drawing board or whatever? I looked at a bunch of other questions, but they had uninitialized functions and stuff. Maybe I have the same but I don’t really see where that would be since the printed values are valid.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 14, 2020, 12:51pm UTC](https://discourse.processing.org/t/nullpointerexception-when-calling-fill-from-another-class/24594/2 "2020-10-14T12:51:04Z")

</div>

Hello,

I did a search and thought these were worthy links:

_[https://forum.processing.org/two/discussion/8071/why-do-i-get-a-nullpointerexception](https://forum.processing.org/two/discussion/8071/why-do-i-get-a-nullpointerexception)_

_[https://github.com/processing/processing/wiki/Common-Errors](https://github.com/processing/processing/wiki/Common-Errors)_

Perhaps they may help you or others.

`:)`

---

<div class="post-metadata">

**Author:** ![S\_V](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@S\_V](https://discourse.processing.org/u/S_V)\
**Post date:** [October 14, 2020, 4:06pm UTC](https://discourse.processing.org/t/nullpointerexception-when-calling-fill-from-another-class/24594/3 "2020-10-14T16:06:57Z")

</div>

Well I looked at them before asking the question, but they mainly touch upon forgetting to initialize stuff. I’ve looked at my code and don’t seem to forget anything. Besides I print every value just before the rect() or fill() call. Every value is valid and non-null. Hence the question
