如何解决java.lang.NullPointerException?

时间:2014-10-10 18:42:00

标签: java nullpointerexception null

这是我为蛇和梯子写的代码。请帮助我除外。例外是在第72和114行。我已经突出显示了导致异常的评论行。谢谢你提前寻求帮助。

 package practical1;

 import java.io.Console;
 import java.util.ArrayList;
 import java.util.Scanner;

public class snakeandladder1 {

int totalpos=100;
int v=0;
int[] scores;
int score=0;
int l=0;

public int throwdice()
{
    int i=0;
    {
        while(i==0)
        {
            i=(int)Math.random()*100;
            i=i%13;
        }
        return i;
    }
}

public int ladder(int score)
{
    if(score==15)
        score=30;
    else if(score == 45)
        score=71;
    else if(score == 25)
        score=62;
    else if(score == 81)
        score=91;
    else if(score == 9)
        score=39;
    scores[l]=score;
    return score;
}

public int snake(int score)
{
    if(score == 29)
        score=11;
    else if(score == 81)
        score=48;
    else if(score == 92)
        score=71;
    else if(score == 30)
        score=6;
    else if(score == 58)
        score=19;
    scores[l]=score;
    return score;
}

void start()
{
    System.out.println("Enter the number of players:");
    Scanner in=new Scanner(System.in);
    int n=in.nextInt();
    in.close();
    l=n;
    System.out.println("Enter Players names in order:");
    ArrayList<String> name1=new ArrayList<String>();
    for (int i=0;i<n;i++)
    {
        Console in1=System.console();
        String name2=in1.readLine(); //---- I am getting this null pointer exception here.
        name1.add(name2);
    }
    while(true)
    {
        while(l>0)
        {
            System.out.println("Click y to roll dice %d"+(l+1));
            Console in2=System.console();
            String yes=in2.readLine();
            if (yes == "y")
            {
                v=throwdice();
            }
            score = scores[l]+v; ((//--- the null pointer exception after changing all user input         to  scanner))
            if(score==totalpos)
            {
                System.out.println("User:"+name1.get(l)+" got "+v+".Winner!!!");
                break;
            }
            else if(score > totalpos)
            {
                scores[l]=scores[l];
            }
            int s1;
            s1=ladder(score);
            if(s1==score)
            {
                snake(score);
            }
            System.out.println("Current score of"+name1.get(l)+"is:"+scores[l]);
            l--;
        }
        if(l==0)
        {
            l=n;
        }
    }
}
public static void main(String[] args) throws Exception{
    // TODO Auto-generated method stub
    snakeandladder1 sal=new snakeandladder1();  ((//null pointer exception here as well after the changes))
    sal.start();   //------ i am getting this null pointer exception here 
}

}

1 个答案:

答案 0 :(得分:1)

Scanner in=new Scanner(System.in);
int n=in.nextInt();
in.close();

请注意,在上面的代码close()方法中,不仅会关闭Scanner对象本身,还会关闭其下划线流(即键盘输入)。 System.in以及实施Readable的所有其他来源都会发生这种情况。

来自the docs

  

扫描仪关闭时,如果是源,它将关闭其输入源   实现 Closeable 接口。

我的建议:

  • 删除in.close();
  • 使用name2 = in.readLine();