2

I'm new to programming. I practice a little in school and at home . I have only learnt the basics. I have a class which is called SPIELAUTOMAT. And I have another class which is called GUTHABEN. In the first class there is a boolean variable with a setter and getter methods. When I'm now trying to access and change the variable in the GUTHABEN class, then I get a NullPointerExeption,

When I'm right informed, then that means that the called Object doesn't exist? I will upload my current Project on Google Drive, code snippets are below this text.

My question now is: How can i set the variable in GUTHABEN, that it is changed in SPIELAUTOMAT.

Here is the GUTHABEN class:

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

And here is the Spielautomat class:

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

}
Abhishek Ghosh
  • 2,593
  • 3
  • 27
  • 60
school_guy
  • 300
  • 2
  • 14

2 Answers2

2

I see the constructor of SPIELAUTOMAT creates a GUTHABEN, but the GUTHABEN constructor doesn't initialize the Spielautomat field. Probably you should pass that (in this case "this") to the constructor, so it can be initialized.

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

and

Guthaben = new GUTHABEN(this);
Bram
  • 479
  • 2
  • 10
  • When I try to use your solution, in my SPIELAUTOMAT class, the field Guthaben wants an Argument, what should i write in the "()"? – school_guy Mar 30 '15 at 17:59
  • That's what I mentioned, and quoted: "this", which in that context is the SPIELAUTOMAT. – Bram Mar 30 '15 at 18:05
0

You have to instantiate your class:

change

 public SPIELAUTOMAT Spielautomat; 

with

 public SPIELAUTOMAT Spielautomat = new SPIELAUTOMAT();

So when you call it on guthabenverwaltung method

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

the Spielautomat will be properly instantiate. Hope helped you!

vathek
  • 531
  • 2
  • 9
  • This will give the GUTHABEN a new SPIELAUTOMAT, which is almost certainly incorrect. – Bram Mar 30 '15 at 17:52