NullPointerException when calling fill() from another class

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:

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:

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:

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:

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!

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://github.com/processing/processing/wiki/Common-Errors

Perhaps they may help you or others.

:)

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