处理空指针异常多个类

时间:2018-10-01 15:10:08

标签: java class processing

我正在使用Processing处理乒乓游戏。单个类的游戏运行正常,但是,我必须添加多个类。每次我得到一个空指针异常。这是扩展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();
}

这是按钮类:

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;

}

如果有人有任何线索,将大有帮助。我想说的是,该代码在主类中的某个方法中运行正常,但是由于某种原因,我似乎找不到它在另一个类中不起作用。

踪迹:

    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)

1 个答案:

答案 0 :(得分:0)

您的代码有一些问题,因为您没有按照设计使用的方式使用处理。

首先,像这样直接创建PApplet的实例是没有道理的:

PApplet app = new PApplet();  

相反,您想扩展 PApplet类以创建自己的草图,如下所示:

import processing.core.PApplet;

public class MySketch extends PApplet{

    public void settings(){
        size(500, 500);
    }

    public void draw(){
        ellipse(mouseX, mouseY, 50, 50);
    }

    public void mousePressed(){
        background(64);
    }

    public static void main(String[] args){
        String[] processingArgs = {"MySketch"};
        MySketch mySketch = new MySketch();
        PApplet.runSketch(processingArgs, mySketch);
    }
}

第二,必须确保直到草图的fill()函数之后之后,才调用Processing的函数(例如rect()setup())类已被调用。

也有一些反馈:请尝试在代码中使用标准命名约定。类和构造函数应以大写字母开头,函数和变量应以小写字母开头。遵循此约定将使您的代码更易于阅读。

无耻的自我促进:here是有关将处理用作Java库的教程。