java - 更改另一个类中的 boolean 值

标签 java reference nullpointerexception boolean

我是编程新手。我在学校和家里练习了一下。我只学到了基础知识。我有一个名为 SPIELAUTOMAT 的类。我还有另一个类,名为 GUTHABEN。在第一个类中,有一个带有 setter 和 getter 方法的 boolean 变量。当我现在尝试访问和更改 GUTHABEN 类中的变量时,我得到一个 NullPointerExeption

当我得到正确的通知时,这意味着被调用的对象不存在?我将在 Google 云端硬盘上上传我当前的项目,代码片段位于此文本下方。

我现在的问题是:如何在 GUTHABEN 中设置变量,使其在 SPIELAUTOMAT 中更改。

这是 GUTHABEN 类:

public class GUTHABEN {

    //Variablen
    private JLabel Guthaben;
    private JButton Increase;
    private JButton Decrease;
    private int guthaben;
    public SPIELAUTOMAT Spielautomat;

    //Methoden

    //Konstruktor
    public GUTHABEN () {
        this.Guthaben = new JLabel("");
        this.Increase = new JButton("Guthaben erhoehen");
        this.Decrease = new JButton("Guthaben verringern");
        this.guthaben = 0;
        zeichne();
    }

    //Funktionen
    public void zeichne () {
        ZEICHENFENSTER.gibFenster().komponenteHinzufuegen(Increase,"rechts");
        ZEICHENFENSTER.gibFenster().komponenteHinzufuegen(Decrease,"rechts");
        ZEICHENFENSTER.gibFenster().komponenteHinzufuegen(Guthaben,"unten");
        regestriereListener();
    }

    public void guthabenverwaltung () {
        while (this.guthaben > 0) {
            Spielautomat.zufall();
            Spielautomat.gewinn();
        }
        Spielautomat.setEndlosspielAktiv(false);
    }

    public void guthabenHoch () {
        this.guthaben ++;
        System.out.println(guthaben);
    }

    public void guthabenRunter () {
        if (this.guthaben > 1) {
            this.guthaben = guthaben - 1;
        }
        else {
            //Nix, spaeter mehr
            if (guthaben == 1) {
                guthaben = 0;
            }
            Guthaben.setText("Kein Guthaben mehr");
        }
        System.out.println(guthaben);
    }

    public void regestriereListener () {
        this.Increase.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guthabenHoch();
            }
        });
        this.Decrease.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guthabenRunter();
            }
        });
    }
}

这是 Spielautomat 类:

package Spielautomat;

//Hier importiere ich Java Klassen (Nachzuschauen in BlueJ unter:
//  Help/Java Class Libraries oder http://docs.oracle.com/javase/6/docs/api/)
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//Klasse
public class SPIELAUTOMAT {

    //Variablen
    private boolean endlosspielAktiv;
    private JButton schaltflaeche;
    private JButton schaltflaeche2;
    private KASTEN kasten;
    public GUTHABEN Guthaben;
    private JLabel schildText;
    public JLabel warnungText;
    //Methoden

    //Konstruktor
    public SPIELAUTOMAT() {
        kasten = new KASTEN ();
        schaltflaeche = new JButton ("Spielen");
        schaltflaeche2 = new JButton("Spiele bis Gewinn");
        schildText = new JLabel("Nicht gespielt");
        warnungText = new JLabel("");
        Guthaben = new GUTHABEN();
        regestriereListener();
    }

    //Funktionen

    public void zeichne () {
        //Hier muss noch was hin was ich noch nicht machen konnte. Das lernen wir noch in der Schule,
        //  ich kann das Problem bei meiner Loesung nicht finden.
        kasten.zeichne();
        ZEICHENFENSTER.gibFenster().komponenteHinzufuegen(schaltflaeche,"unten");
        ZEICHENFENSTER.gibFenster().komponenteHinzufuegen(schaltflaeche2,"unten");
        ZEICHENFENSTER.gibFenster().komponenteHinzufuegen(schildText,"unten");
        ZEICHENFENSTER.gibFenster().komponenteHinzufuegen(warnungText, "unten");
    }

    public void zufall () {
        kasten.Ringe.faerbeUm();
    }

    public void gewinn () {
        if (kasten.Ringe.Gewonnen) {
            schildText.setText("Gewonnen");
        } else if (kasten.Ringe.Gewonnen == false) {
            schildText.setText("Verloren");
        }
        kasten.Ringe.Gewonnen = false;
    }

    public void loeschen () {
        ZEICHENFENSTER.gibFenster().loescheAlles ();
    }

    private void schaltflaecheAction () {
        if (endlosspielAktiv) {
            warnungText.setText("Nicht Moeglich!");
        }
        else {
            warnungText.setText("");
            zufall();
            gewinn();
        }

    }

    public void setEndlosspielAktiv (boolean endlosspielAktiv) {
        this.endlosspielAktiv = endlosspielAktiv;
    }

    public boolean getEndlosspielAktiv () {
        return endlosspielAktiv;
    }

    private void regestriereListener () {
        this.schaltflaeche.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                schaltflaecheAction();
            }
        });
        this.schaltflaeche2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //Guthaben überprüfen und dann spielen
                setEndlosspielAktiv(true);
                Guthaben.guthabenverwaltung();
            }
        });
    }

}

最佳答案

我看到 SPIELAUTOMAT 的构造函数创建了一个 GUTHABEN,但 GUTHABEN 构造函数没有初始化 Spielautomat 字段。也许您应该将其(在本例中为“this”)传递给构造函数,以便可以对其进行初始化。

public GUTHABEN (SPIELAUTOMAT spielautomat) {
    this.Guthaben = new JLabel("");
    this.Increase = new JButton("Guthaben erhoehen");
    this.Decrease = new JButton("Guthaben verringern");
    this.guthaben = 0;
    this.Spielautomat = spielautomat;
    zeichne();
}

Guthaben = new GUTHABEN(this);

关于java - 更改另一个类中的 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29352610/

相关文章:

java - 你知道在 Java 中将 CopyBook 转换为 XSD 的方法吗?

java - 连接多个字符串的最快方法

java - 交换或创建新的引用?

C++ 基础 vector 指派生 vector

java - Java 可以抛出对异常的空引用吗?

java - 选择数组大小并用用户输入填充它

c++ - 支持引用、值和指针的模板函数?

c++ - 引用每个 const 字符串是一种好习惯吗?

java - 什么是NullPointerException,我该如何解决?

java - (链接)BlockingQueue.put(null) 抛出 NullPointerException