0

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.

The code I am currently trying to use

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);
    }
  }

}

edit:

This is what I get from the Debug Console. http://pastebin.com/s0rEZEE9

The line 24 from LoginForm.Java is

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);

edit 2:

This is the entire LoginForm which is what the debug console says is giving an error.

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 & 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<String, Object> params = new HashMap<String, Object>();
    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<String>() {

        @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();
}
}
7
  • 1
    Please add the error message that shows the NullPointerException, and also indicate which line of code the exception is occurring on. Commented Mar 24, 2012 at 21:18
  • And what does the stack trace look like? Commented Mar 24, 2012 at 21:18
  • Please always provide your logcat trace errors.. Commented Mar 24, 2012 at 21:18
  • 1
    The code looks strange, what's the logic of the code? Commented Mar 24, 2012 at 21:24
  • @Alex I'm trying to check if I have previously saved data and if I don't then launch the preference intent, else If I do then I want to launch my form's login intent. Commented Mar 25, 2012 at 1:20

2 Answers 2

1

Without a stack trace, my guess would be that either your catch or finally is the cause. Anytime you put logic inside a catch/finally, you run the risk of throwing an error again, thus negating the caught exception. You can always try wrapping your catch/finally blocks in a try/catch that does nothing (or something that will definitely cause another error)

Your error is most likely coming from newing up Intent, accessing one of the parameters that are used to new it up (MainActivity.this, LoginForm.class), or from startActivity(loginIntent). Those are the place I would look at least

Sign up to request clarification or add additional context in comments.

3 Comments

Where would I find the stack trace? This is the first time I really tried to get into Java.
@mike That I am not sure about. This is not my language, but it is a general programming problem. Sorry, is there no debugger that you might be able to use?
Just post logcat output from Eclipse Logcat window
0

I found out that I could only declare the variables once the activity is started and then assign them under oncreate.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.