Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Nullpointer exception crash when I click the intent, Don't know what to do??

    • 0
    • 0
    • 0
    • 3
    • 0
    • 0
    • 0
    • 471
    Answer it

    I'm trying to make an android app but I encoutered this error and I don't know what to do and why is it showing from the first place, the app crashes with the following error in the log:

     

     

    E/AndroidRuntime: FATAL EXCEPTION: main
                      Process: com.example.ramy.medicare, PID: 2487
                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ramy.medicare/com.example.ramy.medicare.Profile.materialtabs.activity.MainActivity}: 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:2665)
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                          at android.app.ActivityThread.-wrap12(ActivityThread.java)
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                          at android.os.Handler.dispatchMessage(Handler.java:102)
                          at android.os.Looper.loop(Looper.java:154)
                          at android.app.ActivityThread.main(ActivityThread.java:6119)
                          at java.lang.reflect.Method.invoke(Native Method)
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                       
    ***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.ramy.medicare.Profile.materialtabs.activity.MainActivity.onCreate(MainActivity.java:43)***


                          at android.app.Activity.performCreate(Activity.java:6679)
                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                          at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                          at android.os.Looper.loop(Looper.java:154) 
                          at android.app.ActivityThread.main(ActivityThread.java:6119) 
                          at java.lang.reflect.Method.invoke(Native Method) 
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 


    here is the code:

       

    package com.example.ramy.medicare.Profile.materialtabs.activity;
     
      import android.content.Intent;
      import android.os.Bundle;
      import android.support.v7.app.AppCompatActivity;
      import android.support.v7.widget.Toolbar;
      import android.view.LayoutInflater;
      import android.view.View;
     
      import android.widget.Button;
     
      import com.example.ramy.medicare.R;
     
     
     
     
      public class MainActivity extends AppCompatActivity implements View.OnClickListener {
     
     
        private Button btnScrollableTabs_b, btnIconTabs_b, btnCustomIconTextTabs_b;
     
     
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      //    toolbar = (Toolbar) findViewById(R.id.toolbar);
      //    setSupportActionBar(toolbar);
     
     
     
     
     
     
     
     
          btnScrollableTabs_b = (Button) findViewById(R.id.btnScrollableTabs);
          btnIconTabs_b = (Button) findViewById(R.id.btnIconTabs);
          btnCustomIconTextTabs_b = (Button) findViewById(R.id.btnCustomIconTabs);
     
          //at these lines is the error:
          **btnScrollableTabs_b.setOnClickListener(this); 
          btnIconTabs_b.setOnClickListener(this);
          btnCustomIconTextTabs_b.setOnClickListener(this);**
     
     
     
      //    btnSimpleTabs = (Button) findViewById(R.id.btnSimpleTabs);
      //    btnScrollableTabs = (Button) findViewById(R.id.btnScrollableTabs);
      //    btnIconTextTabs = (Button) findViewById(R.id.btnIconTextTabs);
      //    btnIconTabs = (Button) findViewById(R.id.btnIconTabs);
      //    btnCustomIconTextTabs = (Button) findViewById(R.id.btnCustomIconTabs);
     
      //    btnSimpleTabs.setOnClickListener(this);
     
      //    btnScrollableTabs.setOnClickListener(this);
      //    btnIconTextTabs.setOnClickListener(this);
      //    btnIconTabs.setOnClickListener(this);
      //    btnCustomIconTextTabs.setOnClickListener(this);
     
      }
     
        @Override
        public void onClick(View view) {
          switch (view.getId()) {
     
            case R.id.btnScrollableTabs:
              startActivity(new Intent(MainActivity.this, ScrollableTabsActivity.class));
              break;
            case R.id.btnIconTabs:
              startActivity(new Intent(MainActivity.this, IconTabsActivity.class));
              break;
            case R.id.btnCustomIconTabs:
              startActivity(new Intent(MainActivity.this, CustomViewIconTextTabsActivity.class));
              break;
          }
        }
      }

    Kindly tell me what to do Im so confused, I instantiated the object so why am i getting this error ???

 3 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: