I'm working on a pong game with Processing. The game works just fine with a single class, however, I have to add multiple classes. Every time I get a Null Pointer Exception. This is part of the main class which extends PApplet
PApplet app = new PApplet();
void MAINMENU() {
// Start the Menu Song
if (musicStartMenu) {
MENUsong.loop();
musicStartMenu = false;
}
// Stop the Level song if needed
if (!musicStart) {
BGsong.stop();
musicStart = true;
}
// Resetting player scores
ScoreP1 = ScoreP2 = 0;
// Creating the Background for this scene
image(menuBG, 0, 0);
textFont(SC);
// Setting the title
text("PONG", width / 2 - 100, 150);
// Creating the buttons for this scene
Button Play= new Button(width / 2 - 150, height / 2 - 70, 300, 100, "PLAY", width / 2 - 100, height / 2 + 10, 1, 1,MAIN,app);
Button Exit= new Button(width / 2 - 150, height / 2 + 70, 300, 100, "EXIT", width / 2 - 100, height / 2 + 150, 3, 2,MAIN,app);
Play.Create();
Exit.Create();
}
and this is the button class:
import processing.core.PApplet;
public class Button {
int Bx,By, width, height;
String label;
int labelW, labelH, action, style, MAIN;
PApplet app;
public Button(int Bx, int By, int width, int height, String label, int labelW, int labelH, int action, int style, int MAIN, PApplet app) {
this.Bx = Bx;
this.By = By;
this.width = width;
this.height = height;
this.label = label;
this.labelW = labelW;
this.labelH = labelH;
this.action = action;
this.style = style;
this.MAIN = MAIN;
this.app = app;
}
void Create(){
// Check if we hover the mouse over and select a style
if (ButtonBorder(Bx, By, height, width)) {
if (style == 1)
// light green
app.fill(100, 155, 100);
else if (style == 2)
// light red
app.fill(255, 100, 100);
} else app.fill(0, 50); // black transparent
// Nobody likes borders
app.stroke(0, 0);
// Create the button box
app.rect(Bx, By, width, height);
// CHeck if the mouse is pressed and is hovering above the button
if (app.mousePressed && ButtonBorder(Bx, By, height, width)) {
// Select scene on click
MAIN = action;
}
// SET the fill of the label and create the label
app.fill(255);
app.text(label, labelW, labelH);
}
boolean ButtonBorder(int xB, int yB, int ButtonHeight, int ButtonWidth) {
// returns true if the mouse pointer is located inside the button
if (!(app.mouseX >= xB && app.mouseX <= xB + ButtonWidth))
return false;
if (!(app.mouseY >= yB && app.mouseY <= yB + ButtonHeight))
return false;
return true;
}
If anyone has any clue it would be of much help. I want to say that the code is working just fine in a method inside the main class but for some reason that I can't seem to find it doesn't work in another class.
Trace:
java.lang.NullPointerException
at processing.core.PApplet.fill(PApplet.java:14521)
at ButtonB.Create(ButtonB.java:41)
at Pong.MAINMENU(Pong.java:205)
at Pong.draw(Pong.java:161)
at processing.core.PApplet.handleDraw(PApplet.java:2429)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)