mardi 8 septembre 2015

If Else statement NullpointerException [duplicate]

This question already has an answer here:

I'm new to programming, and during a course in taking im required to do an assignment with DieCups, buti keep running into the following message:

java.lang.NullPointerException at DieCup.roll(DieCup.java:36)

I can see that the problem is with my if/else-statement in the roll, but i've been trying for a long time to see if i can find the error in my code, and i simply cant.

my code:

import java.util.Random;
import java.util.ArrayList;

public class DieCup
{

private ArrayList<Die> die;
private ArrayList<Die> dies;
private int sum;
private int maxEyes;


public DieCup()
{
    die = new ArrayList<Die>();
    die.add(new Die());
    die.add(new Die());
    }

public DieCup(int dice)
{
    dies=new ArrayList<Die>();
    for(int i=0; i<=dice;i++)
    {
        dies.add(new Die());
    }
}
/**
 *Kast de to terninger - Forløkke
 */
public void roll()
{
    if (dies.size() >= 1)
    {
        for(int i=0; i<dies.size();i++)
        {
            dies.get(i).roll();
        }
    }
    else{
        for (Die p: die){
            p.roll();
        }
    }    
    sum = getEyes();
    if(sum > maxEyes)
        {
            maxEyes = sum;
        }    

}

/**
 *Get total number of eyes
 */
public int getEyes()
{
    int sum = 0;
    if (dies.size() > 1)
    {
        for(int i=0; i<dies.size();i++)
        {
            sum = sum + dies.get(i).getEyes();
        }
        return sum;
    }
    else
    {
        for(Die p: die){
            sum = sum + p.getEyes();
        }
        return sum;
    } 
}

Aucun commentaire:

Enregistrer un commentaire