Search code examples
javaandroidnullpointerexceptionfindviewbyid

How to pass a variable to findViewById method using classes and instances?


I've been trying to make this tic-tac-toe game on android studios recently, I only have basic knowledge about android studio and java so i wanted to accomplish this app by making classes and objects, but whenever i try to run my app the app crashes i don't know what's wrong with the code it compiles correctly, can someone help me out?

this is the logcat window:

11-04 23:48:20.249 4534-4534/? I/zygote: Not late-enabling -Xcheck:jni (already on)
11-04 23:48:20.258 4534-4534/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
11-04 23:48:20.480 4534-4534/com.example.home.tictactoe I/InstantRun: starting instant run server: is main process
11-04 23:48:20.845 4534-4534/com.example.home.tictactoe D/AndroidRuntime: Shutting down VM
11-04 23:48:20.851 4534-4534/com.example.home.tictactoe E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.example.home.tictactoe, PID: 4534
                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.home.tictactoe/com.example.home.tictactoe.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewPropertyAnimator android.widget.ImageView.animate()' on a null object reference
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                              at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                              at android.os.Looper.loop(Looper.java:164)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                           Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewPropertyAnimator android.widget.ImageView.animate()' on a null object reference
                                                                              at com.example.home.tictactoe.MainActivity$1pawn.hideObj(MainActivity.java:45)
                                                                              at com.example.home.tictactoe.MainActivity.onCreate(MainActivity.java:53)
                                                                              at android.app.Activity.performCreate(Activity.java:6975)
                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                                                                              at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                              at android.os.Looper.loop(Looper.java:164) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

and this is my MainActivity.java file

    package com.example.home.tictactoe;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;

    public class MainActivity extends AppCompatActivity {

        public void clickButton(View view){

            Log.i("test","working");


        }




        @Override
        protected void onCreate(Bundle savedInstanceState) {


            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);




            class pawn{


                int resID = 0;

                ImageView object2 = (ImageView) findViewById(resID);

                public void showObj()
                {
                    object2.animate().alpha(1f).setDuration(1000);//add code here to change alpha of object
                }
                public void hideObj()
                {
                    object2.animate().alpha(0f).setDuration(1000);
                }

            }
            final pawn x_11 = new pawn();
            final pawn zero_11 = new pawn();
            x_11.resID = R.id.x11;
            zero_11.resID=R.id.zero11;
            x_11.hideObj();

   }

}

See picture: enter image description here


Solution

  • ImageView object2 = (ImageView) findViewById(resID); is called when your pawn object created. At this point you've not set resId for your object yet so it will be 0. This cause object2 to null.

    You can change your class like this:

    class pawn {
          int resID = 0;
    
          ImageView object2;
    
          public pawn(int resID) {
              this.resID = resID;
              object2 = (ImageView) findViewById(resID)
          }
    
          public void showObj()
          {
              object2.animate().alpha(1f).setDuration(1000);//add code here to change alpha of object
          }
    
          public void hideObj()
          {
              object2.animate().alpha(0f).setDuration(1000);
          }
    }
    

    Then use it

    pawn x_11 = new pawn(R.id.x11);
    x_11.hideObj();