NullPointerException error
Hi, my program reads out of a textfile containing about 20 hockey players. At the beginning of the program, the user is given the option to sort by name, rating, club or position. No matter what I press, I always get a nullpointerexception error. This never happened before and the program works fine on my friends computer, it also used to work fine on mine and then I randomly got this error. What do I do to fix this?
This is what is says in the bluej thingy
java.lang.NullPointerException
at java.lang.String$CaseInsensitiveComparator.compare (String.java:1217)
at java.lang.String$CaseInsensitiveComparator.compare (String.java:1211)
at java.lang.String.compareToIgnoreCase(String.java:1 258)
at FINAL.sort(FINAL.java:206)
at FINAL.options(FINAL.java:48)
at FINAL.main(FINAL.java:41)
Re: NullPointerException error
Quote:
What do I do to fix this?
Your first need to post code, preferably an abbreviated version, which clearly demonstrates the problem. The stack trace you posted provides a lot of information but is meaningless without the context of the code that throws the exception.
Re: NullPointerException error
I get the error at this part of my program. It's weird though, it was working on my computer a few days ago and I suddenly got this error.
Code Java:
if(options.equalsIgnoreCase("A"))
{
for (int x=0; x<players.length; ++x)
{
for (int y=0;y<players.length-1; ++y)
{
if (players [y][0].compareToIgnoreCase(players [y+1][0]) > 0)
{
for(int b=0;b<=4;b++)
{
String temp = players [y][b];
players [y][b] = players [y + 1][b];
players [y + 1][b] = temp;
}
}
}
}
}
Re: NullPointerException error
Better, but the code still doesn't provide much context. An SSCCE is much more demonstrative. As is, I could guess that not all of your players array has been instantiated.
Re: NullPointerException error
Thanks, it works now. i have another question, when I add a player to the database, it works fine. But when I try to remove a player from the database, it doesn't work. Another thing is, after I use the remove function, the other functions won't work anymore (for example, after using remove, i tried to sort the players by name, it didn't work because I got an array out of bounds error) Here is the code I use for adding players
Code Java:
public static void add(String players [][],String args[]) throws IOException
{
BufferedReader c = new BufferedReader(new InputStreamReader(System.in));
PrintWriter output = new PrintWriter ( new FileWriter ("nhlstats.txt",true));//append file
System.out.println ("Enter the name of the player (first then last)");
String hplayer = c.readLine ();
System.out.println ("Enter the position(C,RW,LW,D,G) of " + hplayer);
String posadd = c.readLine ();
System.out.println ("Enter the rating of " +hplayer);
String ratingadd=c.readLine ();
System.out.println ("Enter the country of origin of " + hplayer);
String countadd = c.readLine ();
System.out.println ("Enter the current team " + hplayer + " plays for");
String clubadd = c.readLine ();
System.out.println ("");
System.out.println ("");
System.out.println("The player has been added toe.");
System.out.println("Please see text file for the added players.");
int x = 0;
for (int i = 0 ; i <=players[x].length-1 ; ++i)
{
if (players [x][0] == null)
{
i = x;
break;
}
}
players [x][0] = hplayer;
players [x][1] = posadd;
players [x][2] = ratingadd;
players [x][3] = countadd;
players [x][4] = clubadd;
output.println ();
output.println (hplayer);
output.println (posadd);
output.println (ratingadd);
output.println (countadd);
output.println (clubadd);
output.close();
for (int j=0; j<players.length; ++j)//sort the name after it is added
{
for (int y=0;y<players.length-1; ++y)
{
if (players [y][0].compareToIgnoreCase(players [y+1][0]) > 0)
{
for(int b=0;b<=4;b++)
{
String temp = players [y][b];
players [y][b] = players [y + 1][b];
players [y + 1][b] = temp;
}
}
}
}
}
The code I "tried" to use for removing players from the database is:
Code Java:
public static void takeout(String players[][],String args[]) throws IOException
{
BufferedReader c = new BufferedReader (new InputStreamReader (System.in));
PrintWriter out = new PrintWriter ( new FileWriter ("nhlstats.txt"));
System.out.println ("What do you want to remove?.");
String read = c.readLine ();
for (int x =0; x<=players.length-1; ++x)
{
if (players[x][0] != null)
{
if (players[x][0].equalsIgnoreCase(read))
{
for (int y = 0; y <= 4; ++y)
{
players [x][y] = null;
}
}
}
}
for (int x =0; x<=players.length-1; ++x)
{
for (int y = 0; y <= 4; ++y)
{
if (players[x][y] != null)
{
out.println(players[x][y]);
}
}
out.close();
}
}
I think what happens is that instead of that specific player getting deleted from the text file, all the players get deleted. How do I delete only one player?
Re: NullPointerException error
I really don't mean to be rude, but can someone please help me?
Re: NullPointerException error
Re: NullPointerException error
Please don't bump threads. I recommend you read the Welcome Announcements, in particular clause F. Moreover, you've provided very little information for us to help. "It doesn't work" could mean a million things. Describe the behavior you expect, and the behavior you receive (suggested reading: How To Ask Questions)