Raghav Kishan
Raghav Kishan

Reputation: 19

Cant figure out the null pointer exception

Please help me figure out the null pointer exception. I am not able to understand which variable or object is null.and hoe do i fix it ?

package coll.org;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Scanner;

public class SnakeAndladder1 {
ArrayList<String> name1=new ArrayList<String>(); //an array to store players names

public int throwdice() //to calculate the dice value
{
    return (int)(Math.random()*6)+1;
}

public int ladder(int curscore)
{
    Hashtable ld = new Hashtable();
    ld.put(15,30);
    ld.put(45,71);
    ld.put(25,62);
    ld.put(81,91);
    ld.put(9,39);
    Object v=ld.get(curscore);
    return (int)v;
}
public int snake(int curscore)
{
    Hashtable ld = new Hashtable();
    ld.put(29,11);
    ld.put(81,48);
    ld.put(30,6);
    ld.put(92,71);
    ld.put(58,19);
    Object v=ld.get(curscore);
    return (int)v;
}


public boolean Game (String name1){
    int score=0;
    String name;
    int v=0;
    name=name1.toString();
        System.out.println("Click y to roll dice");
        Scanner in2=new Scanner(System.in);
        String yes=in2.nextLine();
        if(yes.equalsIgnoreCase("y"))
        {
            v=throwdice();
            System.out.println("dice value:"+v);
        }
        score=score+v;
        if(score==100)
        {
            System.out.println("User:"+name+"got"+v+".Winner!!!");
            return false;
        }
        if (score>100)
        {
            score=score-v;
            System.out.println("Current score of"+name+"is"+score);
            return true;
        }

        int s1=ladder(score);
        if(s1==score)
        {
            score=snake(score);
            System.out.println("Current score of"+name+"is"+score);
            return true;
        }
        else
        {
            score=s1;
            System.out.println("Current score of"+name+"is"+score);
            return true;
        }


}

public static void main(String[] args) {
// TODO Auto-generated method stub
int l=0;
boolean flag=true;
System.out.println("Enter the number of players:");
Scanner in=new Scanner(System.in);
int n=in.nextInt();
System.out.println("Enter Players names in order:");
ArrayList<String> name1=new ArrayList<String>(); //an array to store players names
for (int i=0;i<n;i++)
{
    Scanner in1=new Scanner(System.in);
    String name2=in1.nextLine();
    name1.add(name2);
}

SnakeAndladder1 players[];
players = new SnakeAndladder1[n];
while(flag)
  {
    while(l<n)
    {
        flag = players[l].Game(name1.get(l)); //----Error occuring here.Its a null pointer 
    }                                                exception
  }
 }
}

the stack trace is :

Enter the number of players:
3
Enter Players names in order:
raghav
kishan
sr
Exception in thread "main" java.lang.NullPointerException
    at coll.org.SnakeAndladder1.main(SnakeAndladder1.java:107)

Upvotes: 1

Views: 145

Answers (3)

brso05
brso05

Reputation: 13232

SnakeAndladder1 players[];
players = new SnakeAndladder1[n];

Add this after above:

for(int i = 0; i < players.length; i++)
{
    players[i] = new SnakeAndladder1();
}

This will initialize an object at each element of the array.

Upvotes: 1

Afzaal Ahmad Zeeshan
Afzaal Ahmad Zeeshan

Reputation: 15860

The n in your case is a null, since you're getting the value from the user. So it won't create the array as a finite array. They're all nulls.

Try to initialize the number first from the user and then initialize the array.

Upvotes: 0

NPE
NPE

Reputation: 500495

The following initializes the array with n nulls:

players = new SnakeAndladder1[n];

To avoid the NPE, you need to create the objects before calling Game() on them.

Upvotes: 3

Related Questions