title: Using getSharedPreferences android?
body: <p>This is the <code>save()</code> method in my app:</p>
  
  <pre><code>public void save() {
      Context context;
      SharedPreferences sp = context.getSharedPreferences("gameSave",
              Context.MODE_PRIVATE);
      SharedPreferences.Editor editor = sp.edit();
  
      editor.putInt("X", player.getX());
      editor.putInt("Y", player.getY());
      editor.putInt("level", player.getLevel());
      editor.putFloat("xp", player.getXp());
      editor.commit();
  }
  </code></pre>
  
  <p>I have been trying to get this to work for a while now, but the <code>getSharedPreferences (String name, int mode)</code> has not been working for me. It doesn't come up on it's own, <code>context.getSharedPreferences()</code> works, but that yields a <code>NullPointerException</code>, I think because of my context. I have tried <code>save(Context context){}</code> as a constructor, but calling that from another method with context declared inside of that method does not work either. I've looked at many examples, but none of them have worked for me. So how can I get <code>getSharedPreferences()</code> to work?</p>
  
  <p><strong>EDIT:</strong> I have a class GameScreen and a class SaveManager. When I save in Gamescreen this is the code I use:</p>
  
  <pre><code>Savemanager savemanager;
  savemanager.save();
  state = GameState.Running;
  </code></pre>
  
  <p>And I have my class SaveManager:</p>
  
  <pre><code>package com.package.game;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.SharedPreferences;
  
  public class SaveManager extends Activity {
  
  private Player player = GameScreen.getPlayer();
  
  public void save() {
      SharedPreferences sp = getSharedPreferences("gameSave",
              Context.MODE_PRIVATE);
      SharedPreferences.Editor editor = sp.edit();
  
      editor.putInt("X", player.getX());
      editor.putInt("Y", player.getY());
      editor.putInt("level", player.getLevel());
      editor.putFloat("xp", player.getXp());
  }
  }
  </code></pre>
  
  <p>And when I save in GameScreen my app crashes and I get this logcat:</p>
  
  <pre><code>02-20 01:39:31.979: E/AndroidRuntime(1368): FATAL EXCEPTION: Thread-119
  02-20 01:39:31.979: E/AndroidRuntime(1368): java.lang.NullPointerException
  02-20 01:39:31.979: E/AndroidRuntime(1368): at com.package.game.GameScreen.updateLevelUp(GameScreen.java:364)
  </code></pre>
  
  <p>Line 364 is the line <code>savemanager.save();</code>. I have no idea why this crashes my app.</p>