-2
    The below code gives null pointer exception in android studio    
please help me to solve that 

02-10 22:42:11.702 30997-30997/? I/art: Late-enabling -Xcheck:jni 02-10 22:42:11.749 30997-30997/? D/TidaProvider: TidaProvider() 02-10 22:42:11.757 30997-30997/? W/ReflectionUtils: java.lang.NoSuchMethodException: android.os.MessageQueue#enableMonitor()#bestmatch at miui.util.ReflectionUtils.findMethodBestMatch(ReflectionUtils.java:338) at miui.util.ReflectionUtils.findMethodBestMatch(ReflectionUtils.java:375) at miui.util.ReflectionUtils.callMethod(ReflectionUtils.java:800) at miui.util.ReflectionUtils.tryCallMethod(ReflectionUtils.java:818) at android.os.BaseLooper.enableMonitor(BaseLooper.java:47) at android.os.Looper.prepareMainLooper(Looper.java:111) at android.app.ActivityThread.main(ActivityThread.java:5584) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 02-10 22:42:11.789 30997-30997/com.androidexample.courtcounter W/ResourceType: No package identifier when getting name for resource number 0x00000000 02-10 22:42:11.803 30997-30997/com.androidexample.courtcounter W/System: ClassLoader referenced unknown path: /data/app/com.androidexample.courtcounter-1/lib/arm64 02-10 22:42:11.826 30997-30997/com.androidexample.courtcounter D/AndroidRuntime: Shutting down VM 02-10 22:42:11.827 30997-30997/com.androidexample.courtcounter E/AndroidRuntime: FATAL EXCEPTION: main Process: com.androidexample.courtcounter, PID: 30997 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.androidexample.courtcounter/com.androidexample.courtcounter.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2396) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545) at android.app.ActivityThread.access$1100(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5601) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:160) at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:109) at android.content.Context.obtainStyledAttributes(Context.java:517) at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:839) at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806) at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:630) at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:223) at com.androidexample.courtcounter.MainActivity.(MainActivity.java:19) at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1068) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2386) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545)  at android.app.ActivityThread.access$1100(ActivityThread.java:151)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:157)  at android.app.ActivityThread.main(ActivityThread.java:5601)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 

     public class MainActivity extends AppCompatActivity {
         int team_a_Score=0;
         int team_b_Score=0;
         @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
        
            }
            final Button button1=(Button)findViewById(R.id.score2);
            final Button button_teamB1=(Button)findViewById(R.id.TeamB_score1);
            final Button buttonScore3A=(Button)findViewById(R.id.score3);
            final Button buttonScore3B=(Button)findViewById(R.id.TeamB_score3);
            final Button buttonScore_freeshotA=(Button)findViewById(R.id.freeshot);
            final Button buttonScore_freeshotB=(Button)findViewById(R.id.TeamB_freeshot);
        
                public void onClick(View v) {
                    if (v.equals(button1)) {
                        team_a_Score = team_a_Score + 1;
                        TextView text = (TextView) findViewById(R.id.score_team_a);
                        text.setText(team_a_Score);
        
                    } else if (v.equals(button_teamB1)) {
                        team_b_Score = team_b_Score + 1;
                        TextView text = (TextView) findViewById(R.id.score_teamB);
                        text.setText(team_b_Score);
                    } else if (v.equals(buttonScore3A)) {
                        team_a_Score = team_a_Score + 3;
                        TextView text = (TextView) findViewById(R.id.score_team_a);
                        text.setText(team_b_Score);
                    } else if (v.equals(buttonScore3B)) {
                        team_b_Score = team_b_Score + 3;
                        TextView text = (TextView) findViewById(R.id.score_teamB);
                        text.setText(team_b_Score);
                    } else if (v.equals(buttonScore_freeshotA)) {
                        team_a_Score = team_a_Score + 4;
                        TextView text = (TextView) findViewById(R.id.score_team_a);
                        text.setText(team_a_Score);
                    } else if (v.equals(buttonScore_freeshotB)) {
                        team_b_Score = team_b_Score + 4;
                        TextView text = (TextView) findViewById(R.id.score_teamB);
                        text.setText(team_b_Score);
                    }
        
                }
            }
        
        List item
  • Reopening this question. Closing as dup of the canonical NPE question is not helpful here, as the crash occurs in code that is in the framework and not OP's code. In fact, the entire stacktrace is framework code! OP cannot possibly find the NPE because none of it is his/her code. This is a situation where OP needs help from people who understand how the framework works. – David Wasser Feb 10 '21 at 20:22

1 Answers1

0

It looks like this code:

 final Button button1=(Button)findViewById(R.id.score2);
 final Button button_teamB1=(Button)findViewById(R.id.TeamB_score1);
 final Button buttonScore3A=(Button)findViewById(R.id.score3);
 final Button buttonScore3B=(Button)findViewById(R.id.TeamB_score3);
 final Button buttonScore_freeshotA=(Button)findViewById(R.id.freeshot);

is member variable definitions outside of a method. That doesn't work on Android, because this code executes at object instantiation time and you cannot call findViewById() before setContentView() is called. You are calling setContentView() in onCreate() which is correct, but onCreate() is called after the object is instantiated.

You need to split the member variable definitions from the initialization. To do that, declare the member variables without initialization outside of a method:

 final Button button1;
 final Button button_teamB1;
 final Button buttonScore3A;
 final Button buttonScore3B;
 final Button buttonScore_freeshotA;

Then, in onCreate(), after calling setContentView(), you can initialize the member variables like this:

 button1=(Button)findViewById(R.id.score2);
 button_teamB1=(Button)findViewById(R.id.TeamB_score1);
 buttonScore3A=(Button)findViewById(R.id.score3);
 buttonScore3B=(Button)findViewById(R.id.TeamB_score3);
 buttonScore_freeshotA=(Button)findViewById(R.id.freeshot);
David Wasser
  • 93,459
  • 16
  • 209
  • 274