This site requires JavaScript, please enable it in your browser!
Greenfoot back
KaeptnMlapa
KaeptnMlapa wrote ...

2017/6/18

Why do I get a NullpointerException here?

KaeptnMlapa KaeptnMlapa

2017/6/18

#
Hello, I'm trying to programm "Deal or no Deal", but I get this Errormessage: java.lang.NullPointerException at Rechner.RandomWert(Rechner.java:68) at Rechner.Angebot(Rechner.java:79) at KOFFER2.click(KOFFER2.java:37) at KOFFER2.act(KOFFER2.java:17) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) The code:
int RandomZahl()
    {
     RandomZahl = Greenfoot.getRandomNumber(((DealWorld) getWorld()).Kofferungeöffnet);
     return RandomZahl;
    }
    int RandomWert()
    {
      int aktuell = kofferbeträge[RandomZahl()]; //in this line I get the error
      for (int i = RandomZahl; i< kofferbeträge.length - 1; i++)
          kofferbeträge[i] = kofferbeträge[i+1];
      return aktuell;    
    }
    
    void Angebot()
    {
        int RandomWert = RandomWert();
        Gesamtsumme = Gesamtsumme - RandomWert;
        Angebot = Gesamtsumme/((DealWorld) getWorld()).Kofferungeöffnet * ((DealWorld) getWorld()).click/10;}
I hope someone can help me. Thanks in advance!
KaeptnMlapa KaeptnMlapa

2017/6/18

#
Maybe I should add more code as I could have make a mistake in the basics, so:
public class Rechner extends Actor
{
    
    private int Angebot;
    private int RandomZahl;
    private int Gesamtsumme;
    int aktuell;
    int[] kofferbeträge;

public Rechner()
    {   
     Gesamtsumme =  2695431;
     Angebot = 0;
     int []kofferbeträge ={20, 300, 500, 500000, 10000, 1000, 50, 100000, 750, 1, 25000, 1000000, 750000, 5000, 200, 2500, 250000, 50000, 10, 100};
     
     
    }
danpost danpost

2017/6/18

#
The only thing in the line you say a NullPointerException error occurred that could cause that error is if 'kofferbeträge' was null; but it appears that you are initializing it in the constructor of the class. So, it is not determinable as to the cause without more code/info. Insert the following line as the first line in the RandomWert method:
System.out.println("kofferbeträge is null: "+(kofferbeträge == null));
and if it says 'false', change it to:
System.out.println(kofferbeträge.length);
to make sure it has 20 elements like it is supposed to.
KaeptnMlapa KaeptnMlapa

2017/6/19

#
So it actually said "true" but I noticed that I made a basic mistake in my structure, so I had to put the array in another class and now it works. But still thank you very much for your try to help me!!
You need to login to post a reply.