4

For some reason, I keep on getting the following error every time I try to launch my application:

Unable to instantiate activity ComponentInfo{com.example.lifeahead/com.example.lifeahead.MainActivity}:java.lang.NullPointerException

I checked my manifest file and every activity has been added.

The only method I use when I start the application up is:

private void logIn(){
    Button logIn = (Button) findViewById(R.id.login);
    logIn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent loginIntent = new Intent(MainActivity.this, HomeActivity.class);
            loginIntent.putExtra("cmUN", cmUN);
            loginIntent.putExtra("cmPW", cmPW);
            startActivity(loginIntent);
        }
    });
}

Here's the onCreate:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    logIn();
}

Full log:

E

/AndroidRuntime(1607): FATAL EXCEPTION: main
E/AndroidRuntime(1607): Process: com.example.lifeahead, PID: 1607
E/AndroidRuntime(1607): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.lifeahead/com.example.lifeahead.MainActivity}: java.lang.NullPointerException
E/AndroidRuntime(1607):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
E/AndroidRuntime(1607):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
E/AndroidRuntime(1607):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
E/AndroidRuntime(1607):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
E/AndroidRuntime(1607):     at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(1607):     at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(1607):     at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime(1607):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1607):     at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(1607):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/AndroidRuntime(1607):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
E/AndroidRuntime(1607):     at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(1607): Caused by: java.lang.NullPointerException
E/AndroidRuntime(1607):     at android.app.Activity.findViewById(Activity.java:1884)
E/AndroidRuntime(1607):     at com.example.lifeahead.MainActivity.<init>(MainActivity.java:22)
E/AndroidRuntime(1607):     at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(1607):     at java.lang.Class.newInstance(Class.java:1208)
E/AndroidRuntime(1607):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
E/AndroidRuntime(1607):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
E/AndroidRuntime(1607):     ... 11 more

I appreciate all the help I can get!

2
  • 3
    post the full stacktrace Commented Mar 17, 2014 at 13:56
  • and the code of the activity, not just the function, where are you calling to that function? Commented Mar 17, 2014 at 13:59

4 Answers 4

10

the stacktrace is telling you that

  Button logIn = (Button) findViewById(R.id.login);

the object returned by findViewById is null, since the only method that you have is logIn(). The only reason why findViewById returns null is because you are looking for a view that does not belongs to the current activity's view hierarchy

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

2 Comments

Or maybe this method is being called before onCreate
@GuillermoMerino exactly but if you call it before, it still does not make part of the activity's view hierarchy, at that point.
1

Button logIn = (Button) findViewById(R.id.login);

This error is mainly caused due to the declaration, initialization done outside on onCreate(). Either you declare and initialize inside onCreate(), or you declare it outside and initialize it using a method by calling the same inside onCreate().

Comments

0

You called findViewById() before Activity got window. You should call findViewById() after setContentView(R.layout.activity_main);. I can see that you call it after, but I don't see whole MainActivity.java. I believe you put another findViewById() somewhere before.

Check line 22 in com.example.lifeahead.MainActivity. Your code fails there.

Comments

0

Maybe you use Fragments and your problem is resolved in layouts... See:

<fragment ... android:name="your.package.activity" ... />    

Rename the android:name to new package name!

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.