5 Replies - 472 Views - Last Post: 12 October 2012 - 12:17 PM Rate Topic: -----

#1 alex71385  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 27-October 09

NullPointerException

Posted 12 October 2012 - 11:09 AM

I'm getting an error:
Exception in thread "main" java.lang.NullPointerException
at DeckOfCards2.deal(DeckOfCards2.java:63)
at DeckOfCards2.main(DeckOfCards2.java:19)

But can't figure out why. handArray is declared and initialized prior to deal method being called and deck is not null based on if I print contents of shuffle method. Can someone please explain why I'm getting the error please?
import javax.swing.JOptionPane;


public class DeckOfCards2 {
	private static String[] suits = {"S", "H", "D", "C"};
	private static String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
	
	public static void main(String[] args){
		int[] deck = new int[52];
		String ans;
		final int DECK_SIZE = 52;
		
		ans = JOptionPane.showInputDialog(null, "How many players are there?");
		int players = Integer.parseInt(ans);
	
		if (2 <= players && players <= 6){
			
			int[][] handArray = new int[players][];
			deal(deck, DECK_SIZE, players, handArray);
			for (int i = 0; i < handArray.length; i++) {
				for (int j = 0; j <handArray[i].length; j++){
					System.out.print(handArray[i][j] + " ");
				}
			}
		}
		else{
			JOptionPane.showMessageDialog(null, "That amount of players is invalid.");
		}
		 
	}

	public static void shuffle(int[]deck) {
		//Initialize cards
		for (int i = 0; i < deck.length; i++)
			deck[i] = i;
		
		//Shuffle the cards for 2-51
		for (int i = 51; i > 2; i--) {
			//Generate and index randomly
			int index = (int)(Math.random() * deck.length);
			int temp = deck[i];
			deck [i] = deck[index];
			deck[index] = temp;
			}
		
		//Shuffle card for 0-1
		for (int i = 1; i < 2; i++) {
		int index = 0 + (int)(Math.random() * 2);
		int temp = deck[i];
		deck [i] = deck[index];
		deck[index] = temp;
		}
	}
	
	//Deal the cards
	public static void deal(int[] deck, int DECK_SIZE, int players, int[][] handArray) {
		shuffle(deck);
		int handSize = (int) Math.ceil(DECK_SIZE / players);
		
		for (int k = 0; k < deck.length; k++){
			for (int i = 0; i < handSize; i++){
				for (int j = 0; j < players; j++){
					handArray[j][i] = deck[k];
				}
			}
		}
	}
	
}



Is This A Good Question/Topic? 0
  • +

Replies To: NullPointerException

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: NullPointerException

Posted 12 October 2012 - 11:20 AM

Well after a try / catch I was able to print out to what point you were getting a null pointer.

		for (int k = 0; k < deck.length; k++){
			for (int i = 0; i < handSize; i++){
				for (int j = 0; j < players; j++){
					try {
						handArray[j][i] = deck[k];
					}
					catch(NullPointerException e) {
						System.out.println(j+  " : " + i + " : " + k);
					}
				}
			}
		}



However I also got the error

Exception in thread "main" java.lang.NullPointerException
at DeckOfCards2.main(DeckOfCards2.java:21)

So it seems your null pointer is inside handArray.

Now lets look at this line

int[][] handArray = new int[players][];



I think you are assuming that this array will now be automatically filled with 0 correct?

Well lets try to print it out.

int[][] handArray = new int[players][];
			
			for(int i = 0; i < handArray.length; i++) {
				System.out.println(handArray[i]);
			}

//prints null



Now why would this array have a null value inside? It all has to do with how you initialized your array.

Take a look back at.

int[][] handArray = new int[players][(why is this empty?)];



You made a array with however many columns, but you did not specify how many rows. Java has no clue how many rows you have, how deep you need to go. For safety it just sets it to null.
Was This Post Helpful? 1
  • +
  • -

#3 alex71385  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 27-October 09

Re: NullPointerException

Posted 12 October 2012 - 11:45 AM

Quote

int[][] handArray = new int[players][];



I think you are assuming that this array will now be automatically filled with 0 correct?

Well lets try to print it out.

int[][] handArray = new int[players][];
			
			for(int i = 0; i < handArray.length; i++) {
				System.out.println(handArray[i]);
			}

//prints null



Now why would this array have a null value inside? It all has to do with how you initialized your array.

Take a look back at.

int[][] handArray = new int[players][(why is this empty?)];



You made a array with however many columns, but you did not specify how many rows. Java has no clue how many rows you have, how deep you need to go. For safety it just sets it to null.


You are right, I assumed that whatever array it created, was with 0 value because in the book it said if you initialize it without giving values, it will automatically assign 0 for value.

As far as why that is empty, again, the book. That's how it said to initialize a ragged array if you know the sizes but not values, and that was supposed to assign amount of rows and the amount of columns gets assigned in deal method. So I moved the handsize to main method but if I assign row length that way, it won't be a ragged array anymore.

I'm trying to figure out how to assign values to it to where all rows-1 length = handsize and last rowlength = deck size % players
Was This Post Helpful? 0
  • +
  • -

#4 alex71385  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 27-October 09

Re: NullPointerException

Posted 12 October 2012 - 11:53 AM

This is what I tried and I am now getting a different error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at DeckOfCards2.main(DeckOfCards2.java:22)

import javax.swing.JOptionPane;


public class DeckOfCards2 {
	private static String[] suits = {"S", "H", "D", "C"};
	private static String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
	
	public static void main(String[] args){
		int[] deck = new int[52];
		String ans;
		final int DECK_SIZE = 52;
		
		ans = JOptionPane.showInputDialog(null, "How many players are there?");
		int players = Integer.parseInt(ans);
	
		if (2 <= players && players <= 6){
			
			int handSize = (int) Math.ceil(DECK_SIZE / players);
			int[][] handArray = new int[players][handSize];
			
			for (int i = 0; i < players; i++) {
				for (int j = 0; j < handSize; j++){
					handArray[i][j] = 0;					
				}
			}
			
			int lastRow = DECK_SIZE % players;
			
			for (int i = players; i == players; i++) {
				for (int j = 0; j < lastRow; j++){
					handArray[i][j] = 0;					
				}
			}

			
			deal(deck, players, handArray);
			for (int i = 0; i < handArray.length; i++) {
				for (int j = 0; j <handArray[i].length; j++){
					System.out.print(handArray[i][j] + " ");
				}
			}
		}
		else{
			JOptionPane.showMessageDialog(null, "That amount of players is invalid.");
		}
		 
	}

	public static void shuffle(int[]deck) {
		//Initialize cards
		for (int i = 0; i < deck.length; i++)
			deck[i] = i;
		
		//Shuffle the cards for 2-51
		for (int i = 51; i > 2; i--) {
			//Generate and index randomly
			int index = (int)(Math.random() * deck.length);
			int temp = deck[i];
			deck [i] = deck[index];
			deck[index] = temp;
			}
		
		//Shuffle card for 0-1
		for (int i = 1; i < 2; i++) {
		int index = 0 + (int)(Math.random() * 2);
		int temp = deck[i];
		deck [i] = deck[index];
		deck[index] = temp;
		}
	}
	
	//Deal the cards
	public static void deal(int[] deck, int players, int[][] handArray) {
		shuffle(deck);
		
		
		for (int k = 0; k < deck.length; k++){
			for (int i = 0; i < handArray.length; i++){
				for (int j = 0; j < handArray[i].length; j++){
					handArray[i][j] = deck[k];
				}
			}
		}
	}
	
}


Was This Post Helpful? 0
  • +
  • -

#5 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: NullPointerException

Posted 12 October 2012 - 11:56 AM

I think you are missing the point of ragged arrays. A ragged array would be a multidimensional array that is not a perfect square.

Such as

int[][] array = new int[5][2]
/*
would be

00000
00000

*/



If you have nothing in the second parameter for the array. Java cannot guess as to how many rows you want. Just set it to null.

Now with jagged arrays you COULD do this.

int[][] array = new int[3][];

array[0] = new int[4];
array[1] = new int[2];
array[2] = new int[3];

/*
would look like

000
000
0 0
0

*/



This is how you use ragged arrays.

P.S. I might have gotten my rows and columns backwards. I should have checked that, but you know what I'm talking about. :)

View Postalex71385, on 12 October 2012 - 11:53 AM, said:

This is what I tried and I am now getting a different error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at DeckOfCards2.main(DeckOfCards2.java:22)

//code snippet




Remember arrays are 0 indexed.
If I type in the amount of players I want. EX: 5
This would mean handArray has 5 index. These would be indexed as

0,1,2,3,4

If I try to access handArray at index playerAmount (which is 5)
There is no index 5, so Java throws a ArrayIndexOutOfBoundsException
Was This Post Helpful? 1
  • +
  • -

#6 alex71385  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 27-October 09

Re: NullPointerException

Posted 12 October 2012 - 12:17 PM

Ok, I think I figured out how to do the ragged array, but now I'm getting print out of the same number

import javax.swing.JOptionPane;


public class DeckOfCards2 {
	private static String[] suits = {"S", "H", "D", "C"};
	private static String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
	
	public static void main(String[] args){
		int[] deck = new int[52];
		String ans;
		final int DECK_SIZE = 52;
		
		ans = JOptionPane.showInputDialog(null, "How many players are there?");
		int players = Integer.parseInt(ans);
	
		if (2 <= players && players <= 6){
			
			int handSize = (int) Math.ceil(DECK_SIZE / players);
			int[][] handArray = new int[players][];
			int num = 0;
			
			while (num < players) {
				handArray[num] = new int[handSize];
				if (num == players - 1) {
					int lastRow = DECK_SIZE % players;
					handArray[num] = new int[lastRow];
					break;
				}
				num++;
			}
			
					
			deal(deck, players, handArray);
			for (int i = 0; i < handArray.length; i++) {
				for (int j = 0; j <handArray[i].length; j++){
					System.out.print(handArray[i][j] + " ");
				}
			}
		}
		else{
			JOptionPane.showMessageDialog(null, "That amount of players is invalid.");
		}
		 
	}

	public static void shuffle(int[]deck) {
		//Initialize cards
		for (int i = 0; i < deck.length; i++)
			deck[i] = i;
		
		//Shuffle the cards for 2-51
		for (int i = 51; i > 2; i--) {
			//Generate and index randomly
			int index = (int)(Math.random() * deck.length);
			int temp = deck[i];
			deck [i] = deck[index];
			deck[index] = temp;
			}
		
		//Shuffle card for 0-1
		for (int i = 1; i < 2; i++) {
		int index = 0 + (int)(Math.random() * 2);
		int temp = deck[i];
		deck [i] = deck[index];
		deck[index] = temp;
		}
	}
	
	//Deal the cards
	public static void deal(int[] deck, int players, int[][] handArray) {
		shuffle(deck);
		
		
		for (int k = 0; k < deck.length; k++){
			for (int i = 0; i < handArray.length; i++){
				for (int j = 0; j < handArray[i].length; j++){
					handArray[i][j] = deck[k];
				}
			}
		}
	}
	
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1