Skip to main content

java.lang.NullPointerException

1 reply [Last post]
waldenboy
Offline
Joined: 2006-07-26

Can somebody give me a pointer as to why the beneath gives a java.lang.NullPointerException.

Thank you Rich.

import java.util.*;

public class Player
{
public static final int RACK_SIZE = 7;
private char[] rack;

/**
* Constructor for instances of class Player.
*/
public Player()
{
super();
this.rack = new char[RACK_SIZE];
}

/**
* Constructor for instances of class Player.
*/
public Player(char aLetter)
{
super();
for(int i=0; i<=Player.RACK_SIZE; i++)
{
this.rack[i] = aLetter;
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tackline
Offline
Joined: 2003-06-19

The second constructor does not initialise the rack field, leaving it as null.

It requires the line

this.rack = new char[RACK_SIZE];

before the loop.

Or better, move the initialisation up to the declaration:

private final char[] rack = new char[RACK_SIZE];