Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更改另一个类中的布尔值_Java_Reference_Nullpointerexception_Boolean - Fatal编程技术网

Java 更改另一个类中的布尔值

Java 更改另一个类中的布尔值,java,reference,nullpointerexception,boolean,Java,Reference,Nullpointerexception,Boolean,我是编程新手。我在学校和家里都做了一些练习。我只学到了基本的东西。我有一个叫做SPIELAUTOMAT的类。我还有另一个类,叫做GUTHABEN。在第一个类中,有一个带有setter和getter方法的布尔变量。当我现在尝试访问和更改GUTHABEN类中的变量时,我得到了一个NullPointerExeption 当我被正确告知时,那就意味着被调用的对象不存在?我将把我当前的项目上传到谷歌硬盘上,下面是代码片段 我现在的问题是:如何在GUTHABEN中设置变量,使其在SPIELAUTOMAT中更

我是编程新手。我在学校和家里都做了一些练习。我只学到了基本的东西。我有一个叫做
SPIELAUTOMAT
的类。我还有另一个类,叫做
GUTHABEN
。在第一个类中,有一个带有setter和getter方法的布尔变量。当我现在尝试访问和更改
GUTHABEN
类中的变量时,我得到了一个
NullPointerExeption

当我被正确告知时,那就意味着被调用的对象不存在?我将把我当前的项目上传到谷歌硬盘上,下面是代码片段

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

这是古塔本课程:

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


您必须实例化您的类:

改变

 public SPIELAUTOMAT Spielautomat; 

所以当你用guthabenverwaltung方法调用它时

  public void guthabenverwaltung () {
    while (this.guthaben > 0) {
        Spielautomat.zufall();
        Spielautomat.gewinn();
    }
    Spielautomat.setEndlosspielAktiv(false);
}
Spielautomat将被正确实例化。
希望对你有帮助

你在哪一行得到NPE?可能是NPE的复制品?我不知道这个NPE=Null指针异常。如果你能尽可能减少这个例子,你会更容易得到帮助。在英语中也使用变量。阅读stackoverflow社区指南:)您应该添加stacktrace并标记发生异常的行。英语中的变量是不必要的。这将给GUTHABEN一个新的SPIELAUTOMAT,这几乎肯定是错误的。当我尝试使用你的解决方案时,在我的SPIELAUTOMAT类中,GUTHABEN字段想要一个参数,我应该在“()”中写什么?这就是我提到的,并引用了:“This”,在这种情况下,它就是SPIELAUTOMAT。
 public SPIELAUTOMAT Spielautomat = new SPIELAUTOMAT();
  public void guthabenverwaltung () {
    while (this.guthaben > 0) {
        Spielautomat.zufall();
        Spielautomat.gewinn();
    }
    Spielautomat.setEndlosspielAktiv(false);
}