-1

The NullPointerException should come when I have a null value which im calling I dont get how calling a normal button invokes that

6-13 19:22:10.359 19386-19386/com.example.saisriram.thevote E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.saisriram.thevote, PID: 19386
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.saisriram.thevote/com.example.saisriram.thevote.MainPage}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5265)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at com.example.saisriram.thevote.MainPage.onCreate(MainPage.java:21)
    at android.app.Activity.performCreate(Activity.java:6033)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
    at android.app.ActivityThread.access$800(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5265) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) 

This is what the log shows and I dont understand why ,here is my code in Main:

package com.example.saisriram.thevote;

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Handler;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.view.Window;
 import android.view.WindowManager;
 import android.widget.Button;
 import android.widget.ImageButton;

 public class MainPage extends Activity {
 public static int NO_TIME = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Button mainPage = (Button)findViewById(R.id.mainPage);

    mainPage.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view) {

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run(){
                    Intent intent = new Intent(MainPage.this, HeadBoy.class);
                    startActivity(intent);
                    finish();
                }
            }, NO_TIME);
        }
    });
    ImageButton resultClick = (ImageButton) findViewById(R.id.resultClick);
    resultClick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainPage.this, Results.class);
            startActivity(intent);
        }
    });

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main_page);


}
}

I searched up that error ,and it says it happens when the button isnt initialized but I did initialize mine,so I don't get whats wrong.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

5

You're searching for a View using findViewById(int) before you've actually set the Activity's content via setContentView(int).

Call setContentView(R.layout.activity_main_page); before Button mainPage = (Button)findViewById(R.id.mainPage);

Cruceo
  • 6,763
  • 2
  • 31
  • 52
  • 1
    Correct answer. IMO Android Studio should actually warn if this happens. – xdevs23 Jun 13 '18 at 14:42
  • Thanks it works!And one more doubt when I run this app and press on the button it takes a few seconds before the next activity opens,and in the log it says skipped frames too much activity on main thread ,how do I fix this? – Saisriram Jun 13 '18 at 15:16
  • @Saisriram it is probably due to some big image in the HeadBoy Activity, so it takes a lot of effort to animate the transition between activities. If it is the case, lower the image resolution. – giroxiii Jun 13 '18 at 15:28
  • There are no images in the other activities,I still have to add them ,and in the MainActivity all there is,is one Image button which is in the drawable-nodpi folder and it still lags I dont know why.And how do I resize the images to lower resolutions is it just by dragging them to a smaller size? – Saisriram Jun 13 '18 at 15:44
  • Without seeing the code in your second Activity, it would be very difficult to help you identify the problem. Since it's unrelated to this issue and comment space is severely limited, you should create another question here on SO with concrete examples of the new issue you're facing – Cruceo Jun 13 '18 at 16:32