Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Self-Destructing Message Android App Adding Users Using Parse.com Logging Out

Adam Zollinger
Adam Zollinger
1,269 Points

Fatal Error when trying to open LoginActivity intent

At this point, whenever my app trys to use the navigateToLogin() method, it crashes. In the logcat it gives me java.lang.Runtimeexception: Unable to start activity Component info {...LoginActivity}: java.lang.NullPointerException

Any part of my code that calls upon this method and tries to load the LoginActivity intent instantly crashes the app.

Any thoughts? --What other info is pertinent to answering this question?

One other thing, when I debug and step into the navigateToLogin() method, the first line works to create a new intent, but when I step into the next line it says this:

Class File Editor

Source not found The JAR file ...android.jar has no source attachment.

Not sure what that means or why it is happening.

6 Answers

Could you copy the contents of LogCat in here - the nullPointer is a good start but, somewhere, it'll tell you the line of code that's causing it. That's a good starting point!

Don't worry about the JAR file - it does that! ;-)

Adam Zollinger
Adam Zollinger
1,269 Points

LogCat:

07-17 21:45:03.076: E/AndroidRuntime(22107): FATAL EXCEPTION: main 07-17 21:45:03.076: E/AndroidRuntime(22107): Process: com.example.ribbit, PID: 22107 07-17 21:45:03.076: E/AndroidRuntime(22107): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ribbit/com.example.ribbit.LoginActivity}: java.lang.NullPointerException 07-17 21:45:03.076: E/AndroidRuntime(22107): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184) 07-17 21:45:03.076: E/AndroidRuntime(22107): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 07-17 21:45:03.076: E/AndroidRuntime(22107): at android.app.ActivityThread.access$800(ActivityThread.java:135) 07-17 21:45:03.076: E/AndroidRuntime(22107): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 07-17 21:45:03.076: E/AndroidRuntime(22107): at android.os.Handler.dispatchMessage(Handler.java:102) 07-17 21:45:03.076: E/AndroidRuntime(22107): at android.os.Looper.loop(Looper.java:136) 07-17 21:45:03.076: E/AndroidRuntime(22107): at android.app.ActivityThread.main(ActivityThread.java:5001) 07-17 21:45:03.076: E/AndroidRuntime(22107): at java.lang.reflect.Method.invokeNative(Native Method) 07-17 21:45:03.076: E/AndroidRuntime(22107): at java.lang.reflect.Method.invoke(Method.java:515) 07-17 21:45:03.076: E/AndroidRuntime(22107): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 07-17 21:45:03.076: E/AndroidRuntime(22107): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 07-17 21:45:03.076: E/AndroidRuntime(22107): at dalvik.system.NativeStart.main(Native Method) 07-17 21:45:03.076: E/AndroidRuntime(22107): Caused by: java.lang.NullPointerException 07-17 21:45:03.076: E/AndroidRuntime(22107): at com.example.ribbit.LoginActivity.onCreate(LoginActivity.java:47) 07-17 21:45:03.076: E/AndroidRuntime(22107): at android.app.Activity.performCreate(Activity.java:5231) 07-17 21:45:03.076: E/AndroidRuntime(22107): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 07-17 21:45:03.076: E/AndroidRuntime(22107): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) 07-17 21:45:03.076: E/AndroidRuntime(22107): ... 11 more

Thanks for the reply, Steve.

Adam Zollinger
Adam Zollinger
1,269 Points

Okay, thank you for your help Steve, I found the problem. My onClickListener was references the wrong id (SignUpButton instead of LoginButton). Stupid mistake, but you helped me narrow it down, and gave me the idea of where to start looking.

That's a bit opaque ... can you post your code for LoginActivity too? Maybe that'll throw something up!

Cheers,

Steve.

Adam Zollinger
Adam Zollinger
1,269 Points

package com.example.ribbit;

import com.parse.LogInCallback; import com.parse.ParseException; import com.parse.ParseUser; import com.parse.SignUpCallback;

import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView;

public class LoginActivity extends Activity {

protected EditText mUsername;
protected EditText mPassword;

protected Button mLoginButton;
protected TextView mSignUpTextView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    mSignUpTextView = (TextView)findViewById(R.id.signUpText);
    mSignUpTextView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(LoginActivity.this, SignUpActivity.class);
            startActivity(intent);
        }
    });

    mUsername = (EditText)findViewById(R.id.usernameField);
    mPassword = (EditText)findViewById(R.id.passwordText);

    mLoginButton = (Button)findViewById(R.id.signUpButton);

    mLoginButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String username = mUsername.getText().toString();
            String password = mPassword.getText().toString();


            username = username.trim();
            password = password.trim();


            if (username.isEmpty() || password.isEmpty()){
                AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                builder.setMessage(R.string.login_error_message);
                builder.setTitle(R.string.signUp_error_title);
                builder.setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }
            else{
                ParseUser.logInInBackground(username, password, new LogInCallback() {

                    @Override
                    public void done(ParseUser user, ParseException e) {
                        // TODO Auto-generated method stub
                        if (e==null){
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);


                            startActivity(intent);
                        }
                        else{
                            AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                            builder.setMessage(e.getMessage());
                            builder.setTitle(R.string.signUp_error_title);
                            builder.setPositiveButton(android.R.string.ok, null);
                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }
                    }
                });
            }
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_login, menu);
    return true;
}

}

Here is the code for LoginActivity. It could be something here in the onCreate() method, since it crashes as soon as LoginActivity tries to start.

One other thing, I have found that if I change the navigatetoLogin() function to load the signUpActivity, everything works fine, so I figure the problem must be in this LoginActivity somewhere, right?

*UPDATE: I have isolated it to this piece of code:

    mLoginButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub



            String username = mUsername.getText().toString();
            String password = mPassword.getText().toString();


            username = username.trim();
            password = password.trim();


            if (username.isEmpty() || password.isEmpty()){
                AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                builder.setMessage(R.string.login_error_message);
                builder.setTitle(R.string.signUp_error_title);
                builder.setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }
            else{
                ParseUser.logInInBackground(username, password, new LogInCallback() {

                    @Override
                    public void done(ParseUser user, ParseException e) {
                        // TODO Auto-generated method stub
                        if (e==null){
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);


                            startActivity(intent);
                        }
                        else{
                            AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                            builder.setMessage(e.getMessage());
                            builder.setTitle(R.string.signUp_error_title);
                            builder.setPositiveButton(android.R.string.ok, null);
                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }
                    }
                });
            }
        }


    });

Thanks in advance for your help, Steve.

Glad you got it sorted!

Zachary Zell
Zachary Zell
4,747 Points

So I believe I am having the same problem as you did, what exactly did you do to resolve this?

Greg Austin
Greg Austin
10,890 Points

Wow, and I told myself not to forget to change it after copying and pasting the code from SignUpActivity...doesn't take much to distract me lol.

In case anyone is still having issues, the offending line of code was in the onCreate() method in LoginActivity:

mLoginButton = (Button)findViewById(R.id.signUpButton);

Should be:

mLoginButton = (Button)findViewById(R.id.loginButton);