import java.util.Random;
import java.util.Scanner;
public class Dice {
static Random ranNum = new Random();
static int output ;
public Dice(){
output = ranNum.nextInt(6) + 1;
}
public int OutPut(){
return(output);
}
}
public class Player {
private int position;
Dice roll = new Dice();
boolean cont;
public Player(){
this.position = 0;
}
public void Move(){
if( (position + roll.OutPut()) >= 100){
position = 100;
cont = true;
}
else{
position += roll.OutPut();
cont = false;
}
}
public boolean Cont(){
return(cont);
}
}
public class GamePlay {
Player[] person;
int takeTurn;
public GamePlay(int numPl){
this.person = new Player[numPl];
takeTurn = 0;
}
public void Turn(){
do{
person[takeTurn].Move();
if(takeTurn == (person.length-1))
takeTurn = 0;
else
takeTurn += 1;
}while(person[takeTurn].Cont() == true);
System.out.printf("Player %f Wins",(takeTurn + 1));
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
GamePlay newGame = new GamePlay(scan.nextInt());
newGame.Turn();
}
}
null pointer exception occurs.
Page 1 of 111 Replies - 130 Views - Last Post: 10 January 2013 - 10:17 AM
#1
null pointer exception occurs.
Posted 10 January 2013 - 01:15 AM
Replies To: null pointer exception occurs.
#2
Re: null pointer exception occurs.
Posted 10 January 2013 - 02:34 AM
In the future, post a stack trace t show where your null pointer exception is.
Your problem is on Turn(), which should follow the camel case naming convention. Capital letters in method names like that is Pascal case c# uses.
Anyway, your GamePlay constructor creates an array of Player - I typed in 10, so I got
All this does is create an array of length 10 that will hold Player objects. You are getting the null pointed exception because you have not added any Player objects to the array, so when you call:
You get the null pointer exception because person[1 through 10] are NULL. Add some players to the array first.
Your problem is on Turn(), which should follow the camel case naming convention. Capital letters in method names like that is Pascal case c# uses.
Anyway, your GamePlay constructor creates an array of Player - I typed in 10, so I got
Player[] person = new Player[10]
All this does is create an array of length 10 that will hold Player objects. You are getting the null pointed exception because you have not added any Player objects to the array, so when you call:
person[takeTurn].Move();
You get the null pointer exception because person[1 through 10] are NULL. Add some players to the array first.
#3
Re: null pointer exception occurs.
Posted 10 January 2013 - 05:57 AM
takeTurn = o. So isnt "person[takeTurn].Move();" equals to "person[0].Move();" sir?
#4
Re: null pointer exception occurs.
Posted 10 January 2013 - 06:15 AM
And what is the stack trace?
#5
Re: null pointer exception occurs.
Posted 10 January 2013 - 06:22 AM
dulvinrivindu, on 10 January 2013 - 07:57 AM, said:
takeTurn = o. So isnt "person[takeTurn].Move();" equals to "person[0].Move();" sir?
It is. However, person[0] is not initialized, which is why you're getting the null pointer exception.
This line
Player[] person;
declares an array: now person means something to the machine
This line
initializes the array. Now person can be referred to.
this.person = new Player[numPl];
But until you do something like
person[i] = new Player()
person [i] will not be intialized.
Think of an array as being like an egg carton. You can say "foo is the term we'll use to refer to an egg carton" - that's declaring the array. Then you can say "And this particular egg carton will be the one that we refer to as foo" = that's initializing the array object. But until you put eggs into it, you can't take eggs out of it - that is, you have to initialize each object in the array.
Here's a tip: every object is ultimatly created by a call to a constructor. Unless there's a
new Foo()
there's not a Foo object.
#6
Re: null pointer exception occurs.
Posted 10 January 2013 - 08:37 AM
i added "person[takeTurn] = new Player();" between line 50 and 52. Still the same error comes out. Can you explain the error in what I did please. Thanks
#7
Re: null pointer exception occurs.
Posted 10 January 2013 - 08:57 AM
You want a Player to be a persistent object - you don't want a new Player each turn. Create the Players when you create the Game, in the constructor.
If you're still getting errors, can you please post the stack trace? That's the error output from the compiler. It'll look something like this:
If you're still getting errors, can you please post the stack trace? That's the error output from the compiler. It'll look something like this:
Junk.java:12: cannot find symbol symbol : variable q location: class Junk } while (q ==2); ^ 1 error
#8
Re: null pointer exception occurs.
Posted 10 January 2013 - 09:09 AM
I put this after line 50
now no errors but if I enter the number of players as 2 always the player 2 wins. i cant understand the error
for(int i = 0; i < numPl; i ++){
person[i] = new Player();
}
now no errors but if I enter the number of players as 2 always the player 2 wins. i cant understand the error
#9
Re: null pointer exception occurs.
Posted 10 January 2013 - 09:14 AM
You're making a new Player each turn.
Quote
You want a Player to be a persistent object - you don't want a new Player each turn. Create the Players when you create the Game, in the constructor.
#10
Re: null pointer exception occurs.
Posted 10 January 2013 - 09:17 AM
I added
outside the do-while loop.
for(int i = 0; i < numPl; i ++){
person[i] = new Player();
}
outside the do-while loop.
#11
Re: null pointer exception occurs.
Posted 10 January 2013 - 10:11 AM
everything works fine now. Thanks
#12
Re: null pointer exception occurs.
Posted 10 January 2013 - 10:17 AM
Excellent. Glad I could help.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|