11 Replies - 308 Views - Last Post: 10 January 2013 - 10:17 AM Rate Topic: -----

#1 dulvinrivindu  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 51
  • Joined: 25-September 12

null pointer exception occurs.

Posted 10 January 2013 - 01:15 AM

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();
	}
	 
}





Is This A Good Question/Topic? 0
  • +

Replies To: null pointer exception occurs.

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 824
  • View blog
  • Posts: 2,542
  • Joined: 29-July 11

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

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.
Was This Post Helpful? 2
  • +
  • -

#3 dulvinrivindu  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 51
  • Joined: 25-September 12

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?
Was This Post Helpful? 0
  • +
  • -

#4 dulvinrivindu  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 51
  • Joined: 25-September 12

Re: null pointer exception occurs.

Posted 10 January 2013 - 06:15 AM

And what is the stack trace?
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7654
  • View blog
  • Posts: 12,909
  • Joined: 19-March 11

Re: null pointer exception occurs.

Posted 10 January 2013 - 06:22 AM

View Postdulvinrivindu, 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.
Was This Post Helpful? 4
  • +
  • -

#6 dulvinrivindu  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 51
  • Joined: 25-September 12

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
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7654
  • View blog
  • Posts: 12,909
  • Joined: 19-March 11

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:

Junk.java:12: cannot find symbol
symbol  : variable q
location: class Junk
		} while (q ==2);
		         ^
1 error

Was This Post Helpful? 1
  • +
  • -

#8 dulvinrivindu  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 51
  • Joined: 25-September 12

Re: null pointer exception occurs.

Posted 10 January 2013 - 09:09 AM

I put this after line 50
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
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7654
  • View blog
  • Posts: 12,909
  • Joined: 19-March 11

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.

Was This Post Helpful? 0
  • +
  • -

#10 dulvinrivindu  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 51
  • Joined: 25-September 12

Re: null pointer exception occurs.

Posted 10 January 2013 - 09:17 AM

I added
for(int i = 0; i < numPl; i ++){
			person[i] = new Player(); 
		}


outside the do-while loop.
Was This Post Helpful? 0
  • +
  • -

#11 dulvinrivindu  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 51
  • Joined: 25-September 12

Re: null pointer exception occurs.

Posted 10 January 2013 - 10:11 AM

everything works fine now. Thanks
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7654
  • View blog
  • Posts: 12,909
  • Joined: 19-March 11

Re: null pointer exception occurs.

Posted 10 January 2013 - 10:17 AM

Excellent. Glad I could help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1