I'm trying to create a friends list mod that saves and loads a list of friends to a text file but i'm getting nullPointerExceptions from saveFriends, loadFriends, addfriend, and removefriend. I have no idea why probably because i'm a bit of a noob :tongue.gif:. Any help please? Here's my source:
package net.minecraft.src;
import java.io.*;
import java.util.*;
import java.util.logging.Logger;
public class FriendsList
{
public static List Friends;
private static File FriendFile;
public FriendsList()
{
FriendFile = new File("Friends_List.txt");
}
public static void addFriend(String s)
{
try
{
Friends.add(s.toLowerCase());
} catch(Exception e)
{
e.printStackTrace();
}
try
{
saveFriends();
} catch(Exception e)
{
e.printStackTrace();
}
}
public static void removeFriend(String s)
{
Friends.remove(s.toLowerCase());
saveFriends();
}
private void loadFriends()
{
try
{
Friends.clear();
BufferedReader bufferedreader = new BufferedReader(new FileReader(FriendsList.FriendFile));
for (String s = ""; (s = bufferedreader.readLine()) != null;)
{
Friends.add(s.trim().toLowerCase());
}
bufferedreader.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static void saveFriends()
{
try
{
PrintWriter printwriter = new PrintWriter(new FileWriter(FriendsList.FriendFile, true));
String s;
for (Iterator iterator = Friends.iterator(); iterator.hasNext(); printwriter.println(s))
{
s = (String)iterator.next();
}
printwriter.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
I have never done anything like this but my guess is with:
for (String s = ""; (s = bufferedreader.readLine()) != null;)
You are asking s to be SET TO (=) bufferedreader.readLine() instead of asking IF IT IS EQUAL TO (==) bufferedreader.readLine().
e.g.
BAD:
s = bufferedreader.readLine()
BETTER:
s == bufferedreader.readLine()
AND IF THAT STILL IS NOT WORKING TRY:
s.equals(bufferedreader.readLine())
Hope this helped
If it did please let me know it is really appreciated.
I have never done anything like this but my guess is with:
for (String s = ""; (s = bufferedreader.readLine()) != null;)
You are asking s to be SET TO (=) bufferedreader.readLine() instead of asking IF IT IS EQUAL TO (==) bufferedreader.readLine().
e.g.
BAD:
s = bufferedreader.readLine()
BETTER:
s == bufferedreader.readLine()
AND IF THAT STILL IS NOT WORKING TRY:
s.equals(bufferedreader.readLine())
Hope this helped
If it did please let me know it is really appreciated.
Nope, that doesn't work. Btw it's a logic error. That's why i'm having trouble.
Error will be easier to spot if you can trace it (most basic way is to put prints in all possible places).
Or well, error log too :wink.gif: (exception.printStackTrace())
java.lang.NullPointerException
at net.minecraft.src.FriendsList.addFriend(FriendsList.java:20)
at net.minecraft.src.EntityClientPlayerMP.sendChatMessage(EntityClientPlayerMP.java:282)
at net.minecraft.src.GuiChat.keyTyped(GuiChat.java:48)
at net.minecraft.src.GuiScreen.handleKeyboardInput(GuiScreen.java:137)
at net.minecraft.src.GuiScreen.handleInput(GuiScreen.java:109)
at net.minecraft.client.Minecraft.runTick(Minecraft.java:1328)
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:694)
at net.minecraft.client.Minecraft.run(Minecraft.java:634)
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileWriter.<init>(Unknown Source)
at net.minecraft.src.FriendsList.saveFriends(FriendsList.java:63)
at net.minecraft.src.FriendsList.addFriend(FriendsList.java:27)
at net.minecraft.src.EntityClientPlayerMP.sendChatMessage(EntityClientPlayerMP.java:282)
at net.minecraft.src.GuiChat.keyTyped(GuiChat.java:48)
at net.minecraft.src.GuiScreen.handleKeyboardInput(GuiScreen.java:137)
at net.minecraft.src.GuiScreen.handleInput(GuiScreen.java:109)
at net.minecraft.client.Minecraft.runTick(Minecraft.java:1328)
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:694)
at net.minecraft.client.Minecraft.run(Minecraft.java:634)
at java.lang.Thread.run(Unknown Source)
You are asking s to be SET TO (=) bufferedreader.readLine() instead of asking IF IT IS EQUAL TO (==) bufferedreader.readLine().
e.g.
BAD:
s = bufferedreader.readLine()
BETTER:
s == bufferedreader.readLine()
AND IF THAT STILL IS NOT WORKING TRY:
s.equals(bufferedreader.readLine())
Hope this helped
If it did please let me know it is really appreciated.
Nope, that doesn't work. Btw it's a logic error. That's why i'm having trouble.
Thanks, I changed it, but I've still got the nullPointer problem.
I'm starting to think it might be how i add friends ingame. I'm doing it like this:
well, I don't get it. I'll come back to it later.