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
    • 477
    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:

       

    1. package com.example.ramy.medicare.Profile.materialtabs.activity;
    2. import android.content.Intent;
    3. import android.os.Bundle;
    4. import android.support.v7.app.AppCompatActivity;
    5. import android.support.v7.widget.Toolbar;
    6. import android.view.LayoutInflater;
    7. import android.view.View;
    8. import android.widget.Button;
    9. import com.example.ramy.medicare.R;
    10. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    11. private Button btnScrollableTabs_b, btnIconTabs_b, btnCustomIconTextTabs_b;
    12. @Override
    13. protected void onCreate(Bundle savedInstanceState) {
    14. super.onCreate(savedInstanceState);
    15. setContentView(R.layout.activity_main);
    16. // toolbar = (Toolbar) findViewById(R.id.toolbar);
    17. // setSupportActionBar(toolbar);
    18. btnScrollableTabs_b = (Button) findViewById(R.id.btnScrollableTabs);
    19. btnIconTabs_b = (Button) findViewById(R.id.btnIconTabs);
    20. btnCustomIconTextTabs_b = (Button) findViewById(R.id.btnCustomIconTabs);
    21. //at these lines is the error:
    22. **btnScrollableTabs_b.setOnClickListener(this);
    23. btnIconTabs_b.setOnClickListener(this);
    24. btnCustomIconTextTabs_b.setOnClickListener(this);**
    25. // btnSimpleTabs = (Button) findViewById(R.id.btnSimpleTabs);
    26. // btnScrollableTabs = (Button) findViewById(R.id.btnScrollableTabs);
    27. // btnIconTextTabs = (Button) findViewById(R.id.btnIconTextTabs);
    28. // btnIconTabs = (Button) findViewById(R.id.btnIconTabs);
    29. // btnCustomIconTextTabs = (Button) findViewById(R.id.btnCustomIconTabs);
    30. // btnSimpleTabs.setOnClickListener(this);
    31. // btnScrollableTabs.setOnClickListener(this);
    32. // btnIconTextTabs.setOnClickListener(this);
    33. // btnIconTabs.setOnClickListener(this);
    34. // btnCustomIconTextTabs.setOnClickListener(this);
    35. }
    36. @Override
    37. public void onClick(View view) {
    38. switch (view.getId()) {
    39. case R.id.btnScrollableTabs:
    40. startActivity(new Intent(MainActivity.this, ScrollableTabsActivity.class));
    41. break;
    42. case R.id.btnIconTabs:
    43. startActivity(new Intent(MainActivity.this, IconTabsActivity.class));
    44. break;
    45. case R.id.btnCustomIconTabs:
    46. startActivity(new Intent(MainActivity.this, CustomViewIconTextTabsActivity.class));
    47. break;
    48. }
    49. }
    50. }

    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
Reset Password
Fill out the form below and reset your password: