nullpointerexception - Null Pointer Exception with Multiple Classes in Processing -


i'm trying make button class tic tac toe game in processing. code class this

import processing.core.papplet;  public class button extends papplet{      float buttonx;     float buttony;     float buttonwidth;     float buttonheight;     boolean cliked;      public button(papplet canvas, float buttonx, float buttony, float buttonwidth, float buttonheight) {         this.buttonx = buttonx;         this.buttony = buttony;         this.buttonwidth = buttonwidth;         this.buttonheight = buttonheight;     }      public void drawbutton() {         rect(200, 200, 200, 200);         textsize(40);         fill(200, 200, 200);         text("start game", 300, 300);     }  } 

the code inside of setup() method processing is

button startbutton = new button(this, 200, 200, 200, 200); startbutton.drawbutton(); 

i have no clue i'm doing wrong, keep receiving nullpointerexception.

this caused because you've got 2 classes extend papplet. should have 1 class extends papplet. think of class sketch, , other classes need refer draw stuff sketch.

you pass papplet canvas button constructor. instead of having button class extend papplet, need use canvas variable drawing. should this:

import processing.core.papplet;  public class button{      papplet canvas;     float buttonx;     float buttony;     float buttonwidth;     float buttonheight;     boolean cliked;          public button(papplet canvas, float buttonx, float buttony, float buttonwidth, float buttonheight) {         this.canvas = canvas;         this.buttonx = buttonx;         this.buttony = buttony;         this.buttonwidth = buttonwidth;         this.buttonheight = buttonheight;     }      public void drawbutton() {         canvas.rect(200, 200, 200, 200);         canvas.textsize(40);         canvas.fill(200, 200, 200);         canvas.text("start game", 300, 300);     } } 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -