title: Null Pointer Exception in Try/Catch/Finally statement
body: <p>I'm trying to display a preferences screen if my saved preferences aren't found. But I seem to be running into a problem with my app crashing due to a Null Pointer Exception.</p>
  
  <p>The code I am currently trying to use</p>
  
  <pre><code>public class MainActivity extends Activity {
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      try {
          SharedPreferences sharedPrefs = PreferenceManager
                  .getDefaultSharedPreferences(this);
      } catch (Exception e) {
          // Preferences
          Intent prefsIntent = new Intent(MainActivity.this,
                  Preferences.class);
          startActivity(prefsIntent);
      } finally {
          Intent loginIntent = new Intent(MainActivity.this,
                  LoginForm.class);
          startActivity(loginIntent);
      }
    }
  
  }
  </code></pre>
  
  <p>edit:</p>
  
  <p>This is what I get from the Debug Console. <a href="http://pastebin.com/s0rEZEE9" rel="nofollow">http://pastebin.com/s0rEZEE9</a></p>
  
  <p>The line 24 from LoginForm.Java is </p>
  
  <pre><code>SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
  </code></pre>
  
  <p>edit 2:</p>
  
  <p>This is the entire LoginForm which  is what the debug console says is giving an error.</p>
  
  <pre><code>package com.smashedbits.livestreams;
  
  import java.util.HashMap;
  import java.util.Map;
  
  import com.androidquery.AQuery;
  import com.androidquery.callback.AjaxCallback;
  import com.androidquery.callback.AjaxStatus;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.content.SharedPreferences;
  import android.os.Bundle;
  import android.preference.PreferenceManager;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.EditText;
  
  public class LoginForm extends Activity {
  
  public AQuery aq;
  
  SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(LoginForm.this);
  boolean autoLogin = sharedPrefs.getBoolean("remember_login", false);
  
  EditText eUsername = (EditText) findViewById(R.id.usernameField);
  EditText ePassword = (EditText) findViewById(R.id.passwordField);
  
  @Override
  public void onBackPressed() {
      super.onBackPressed();
      // return;
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.login);
  
      aq = new AQuery(this);
  
      // Views
      final Button prefsButton = (Button) findViewById(R.id.prefsButton);
      final Button loginButton = (Button) findViewById(R.id.loginButton);
  
      // Check for saved data
      String usrn = sharedPrefs.getString("usr", "NULL");
      String pswd = sharedPrefs.getString("pwd", "NULL");
      if (autoLogin == true &amp; usrn != "NULL") {
          eUsername.setText(usrn);
          ePassword.setText(pswd);
      }
  
      // Preferences
      prefsButton.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
              Intent prefsIntent = new Intent(LoginForm.this,
                      Preferences.class);
              startActivity(prefsIntent);
          }
      });
  
      // Login
      loginButton.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
              ls_login();
          }
      });
  
  }
  
  public void ls_login() {
  
        boolean autoLogin = sharedPrefs.getBoolean("remember_login", false);
  
        if (autoLogin == true) { saveLogin(); }
  
      String url = "http://{redacted}";
      EditText eUsername = (EditText) findViewById(R.id.usernameField);
      EditText ePassword = (EditText) findViewById(R.id.passwordField);
  
      Map&lt;String, Object&gt; params = new HashMap&lt;String, Object&gt;();
      params.put("log", eUsername.getText().toString());
      params.put("pwd", ePassword.getText().toString());
      params.put("rememberme", "forever");
      params.put("wp-submit", "Log In");
      params.put("redirect_to", "{redacted}");
      params.put("testcookie", "1");
  
      aq.ajax(url, params, String.class, new AjaxCallback&lt;String&gt;() {
  
          @Override
          public void callback(String url, String html, AjaxStatus status) {
  
              if (html.contains("LOG OUT")) {
                  Intent guideIntent = new Intent(LoginForm.this,
                          ChannelGuide.class);
                  startActivity(guideIntent);
              }
  
          }
      });
  }
  
  private void saveLogin() {
      SharedPreferences.Editor editor = sharedPrefs.edit();
  
      editor.putString("usr", eUsername.getText().toString());
      editor.putString("pwd", ePassword.getText().toString());
  
      editor.commit();
  }
  }
  </code></pre>