I am working on my first Android app. It was working fine until I decided to add a functionality to save and load objects and settings on OnCreate and onRestart. After that the app crashes on startup. Here is the setSettings that I call from OnCreate.
public void setSettings(){
SharedPreferences settings = getSharedPreferences("Prefs",0);
newGame = settings.getBoolean("newGame", true);
autoDecide = settings.getBoolean("autoDecide", true);
saved = settings.getBoolean("saved", false);
if (saved){
player1 = readPlayer("Player 1");
player2 = readPlayer("Player 2");
((TextView)findViewById(R.id.Player1name)).setText(""+player1.name);
((TextView)findViewById(R.id.Player2name)).setText(""+player2.name);
((TextView)findViewById(R.id.player1wins)).setText(""+player1.totalWins);
((TextView)findViewById(R.id.player2wins)).setText(""+player2.totalWins);
((TextView)findViewById(R.id.Player1life)).setText(""+player1.life);
((TextView)findViewById(R.id.Player2life)).setText(""+player2.life);
}
}
the saved boolean is supposed to be false until I saved things, but I don't think it is working. Here is the readPlayer that it calls where the problem might be.
public Player readPlayer(String loc) {
//FileInputStream fis;
try {
BufferedInputStream buf = new BufferedInputStream( openFileInput(loc));
ObjectInputStream stream = new ObjectInputStream(buf);
Player re = (Player) stream.readObject();
stream.close();
buf.close();
return re;
} catch (FileNotFoundException e) {
//e.printStackTrace();
return new Player(loc);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return new Player(loc);
}
and here is the logCat that I get when it crashes.
05-26 03:49:05.739: E/AndroidRuntime(391): java.lang.RuntimeException: Unable to start activity ComponentInfo{jjcard.app.lifecount/jjcard.app.lifecount.Life_counterActivity}: java.lang.NullPointerException
05-26 03:49:05.739: E/AndroidRuntime(391): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
05-26 03:49:05.739: E/AndroidRuntime(391): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
05-26 03:49:05.739: E/AndroidRuntime(391): at android.app.ActivityThread.access$500(ActivityThread.java:122)
05-26 03:49:05.739: E/AndroidRuntime(391): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
05-26 03:49:05.739: E/AndroidRuntime(391): at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 03:49:05.739: E/AndroidRuntime(391): at android.os.Looper.loop(Looper.java:132)
05-26 03:49:05.739: E/AndroidRuntime(391): at android.app.ActivityThread.main(ActivityThread.java:4123)
05-26 03:49:05.739: E/AndroidRuntime(391): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 03:49:05.739: E/AndroidRuntime(391): at java.lang.reflect.Method.invoke(Method.java:491)
05-26 03:49:05.739: E/AndroidRuntime(391): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-26 03:49:05.739: E/AndroidRuntime(391): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-26 03:49:05.739: E/AndroidRuntime(391): at dalvik.system.NativeStart.main(Native Method)
05-26 03:49:05.739: E/AndroidRuntime(391): Caused by: java.lang.NullPointerException
05-26 03:49:05.739: E/AndroidRuntime(391): at jjcard.app.lifecount.Life_counterActivity.setSettings(Life_counterActivity.java:72)
05-26 03:49:05.739: E/AndroidRuntime(391): at jjcard.app.lifecount.Life_counterActivity.onCreate(Life_counterActivity.java:42)
05-26 03:49:05.739: E/AndroidRuntime(391): at android.app.Activity.performCreate(Activity.java:4397)
05-26 03:49:05.739: E/AndroidRuntime(391): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
05-26 03:49:05.739: E/AndroidRuntime(391): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
I don't know if you would need the player creation but here it is
public Player(String nameN){
name = nameN;
life = 8000;
totalWins = 0;
totalLosses = 0;
wins = new HashMap<String, Integer>();
}
It happens everytime I start it up and I've tried a few things as you can probably see by the code with no success. I tried searching around but I can't find anything. Hope you guys can help.
EDIT: here is line 72 that the LogCat shows is the problem.
((TextView)findViewById(R.id.Player1name)).setText(""+player1.name);
So it looks like you are right, it might be making reading an empty object instead of returning a new player like I thought it would. And since I don't know a lot about making writing and reading objects, I just used defaultWriteObject and defaultReadObject
private void writeObject(ObjectOutputStream out) throws IOException{
out.defaultWriteObject();
}
private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException{
in.defaultReadObject();
}
EDIT: after changing the read/write and trying to check for the name being null, it still doesn't work. I even tried setting the text to a String and it still gives a nullPointerException, so guess it has something do do with the FindViewById(R.id.Player1name), but I don't know exactly what yet. I use different views for different screens using ViewPagerIndicator, could that have something to do with it?