java.lang.NullPointerException
From: Max010 (patacca_at_hotmail.co.uk)
Date: 03/27/05
- Previous message: Kris M: "Tuning an application"
- Next in thread: klynn47_at_comcast.net: "Re: java.lang.NullPointerException"
- Reply: klynn47_at_comcast.net: "Re: java.lang.NullPointerException"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 26 Mar 2005 15:34:47 -0800
Hi there I was wondering if someone can help me. I m fairly new to
java. I m developing a game where a user is trying to guess the letter
of an hindden word.
I ve developed 4 classes but I ve got problem with only 1 the class
Guesser.
1st class;
import java.io.*;
public class EasyInput
{
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
public char readChar()
{
char c = 0;
try
{
String str = in.readLine();
if( !str.equals(""))
{
c = str.charAt(0);
}
}
catch(Exception e)
{
System.out.println("Sorry, character input is required
here.");
}
return c;
}
}
2nd class;
public class Player
{
private String name;
private int score;
public void setName(String n)
{
name = n;
}
public String getName()
{
return name;
}
public void incrementScore()
{
score++;
}
public int getScore()
{
return score;
}
public boolean sameScore(Player p)
{
return (score == p.getScore());
}
public String toString()
{
String nameScore;
nameScore = name + " ("+score + " points).";
return nameScore;
}
public Player(String aPlayerName)
{
name = aPlayerName;
score = 0;
}
}
3rd class;
public class Guesser extends Player
{
private char guess;
private int turns;
private char[]guessedAlready;
public final static int MAX_RIGHT_GUESSES = 26;
private final static char EXIT_CHARACTER = '*';
public Guesser(String aName)
{
super(aName);
turns = 0;
guess = '\u0000';
char[]guessedAlready = new char[MAX_RIGHT_GUESSES];
}
// method to tell if the received character is
// a lowercase letter of the English alphabet
public boolean isLetter(char ch)
{
if(ch >= 'a' && ch <= 'z')
return true;
else
return false;
}
//return true if the received character has been
// stored in the guessedAlready array before, otherwise return false
public boolean isGuessedAlready(char ch)
{
int i = 0;
boolean guessed = false;
while(i < MAX_RIGHT_GUESSES)
{
if(ch == guessedAlready[i])
{
guessed = true;
i++;
}
else
i++;
}
return guessed;
}
// return true if a valid letter or exit character
// is received as argument, otherwise it returns false
public boolean isValid(char ch)
{
boolean valid = false;
if((isLetter(ch)&& !isGuessedAlready(ch))|| ch == EXIT_CHARACTER)
valid = true;
if(!isLetter(ch))
System.out.println("We \'re only using lower-case letters");
if(!isGuessedAlready(ch))
System.out.println("You guessed that one already!");
return valid;
}
// a method to get a character from the keyboard
private char readChar()
{
return new EasyInput().readChar();
}
// guess is set to either the exit character
// or a valid guess (lowercase letter, not guessed already).
public void setGuess(String prompt)
{
System.out.println(prompt);
if(this.isLetter(readChar())&& !this.isGuessedAlready(readChar()))
{
guess = readChar();
guessedAlready[turns] = guess;
turns++;
}
else
guess = EXIT_CHARACTER;
}
// return the value of the guess variable
public char getGuess()
{
return guess;
}
// to return the value of the exit character
public char getExitCharacter()
{
return EXIT_CHARACTER;
}
// Write a toString method
public String toString()
{
String z = "";
for(int i = 0; i < MAX_RIGHT_GUESSES; i++)
{
z = z + guessedAlready[i];
}
return super.toString()+ " Guesses [ "+ z +" ]";
}
}
I cannot understand why I get the following error:
java.lang.NullPointerException
at Guesser.toString(Guesser.java:120)
at TestGuesser.main(TestGuesser.java:25)
Exception in thread "main"
Can anyone point me to the right direction????
Thanks Max :-)
- Previous message: Kris M: "Tuning an application"
- Next in thread: klynn47_at_comcast.net: "Re: java.lang.NullPointerException"
- Reply: klynn47_at_comcast.net: "Re: java.lang.NullPointerException"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|