Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 ???


What I have tried:

I tried fixing the layout I tried changing the variables,
I tried everything, pls help
Posted
Updated 12-Jan-17 20:19pm
Comments
Richard MacCutchan 13-Jan-17 4:55am    
Use the debugger to find out why your Button objects are null.
wseng 15-Jan-17 8:14am    
Post your layout here

1 solution

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and your development environment will help you here. Run your program in the debugger and when it fails, the debugger will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, it will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900