处理空指针异常多个类 [英] Processing Null Pointer Exception Multiple Classes

查看:70
本文介绍了处理空指针异常多个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Processing处理乒乓游戏。单个类的游戏运行正常,但是,我必须添加多个类。每当我得到一个空指针异常。这是扩展 PApplet

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();
}

这是按钮类:

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)


推荐答案

您的代码存在一些问题,因为您没有按照原本打算的方式使用处理。

Your code has a few problems, because you aren't using Processing the way it was designed to be used.

首先,创建<的实例没有任何意义。 code> PApplet 像这样:

PApplet app = new PApplet();  

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

Instead, you want to extend the PApplet class to create your own sketch, like this:

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);
    }
}

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

Secondly, you have to make sure you don't call Processing's functions (like fill() and rect()) until after the setup() function of your sketch class has been called.

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

Also, some feedback: please try to use standard naming conventions in your code. Classes and constructors should start with an upper-case letter, and functions and variables should start with a lower-case letter. Following this convention will make your code much easier to read.

无耻的自我推广:此处是有关将处理用作Java库的教程。

Shameless self-promotion: here is a tutorial on using Processing as a Java library.

这篇关于处理空指针异常多个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆