I'm making a simple game to learn how to use Android Studio. The objective of the game is to do some simple tasks as fast as possible. In my main game screen, I have a thread that makes an int count up every second, so the player knows how much time has gone by. When the player has done all the simple tasks, the game goes to the end screen. Now I want to display the time in the endscreen, so the player knows how fast they finished. I get an error when I run the game.
This is the code to run the thread every second:
Timer d = new Timer(true);
thread1 thread1 = new thread1(this, myCanvas);
t.scheduleAtFixedRate(thread1, 0, 1000);
This is the tick function that the thread does every second:
public void tick1(){
timer++;
}
This is the getter for the timer:
public int getTimer(){
return timer;
}
When the game ends and it goes to the endscreen, I use the getTimer() to have a textview that should display the timer:
err = (TextView)findViewById(R.id.textView);
err.setText("Tijd: "+ timer+"");
This is the error message I get when I try to run it:
2021-03-06 11:18:38.153 21729-21729/com.example.test_game E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test_game, PID: 21729
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test_game/com.example.test_game.Eindscherm}: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.test_game.MyCanvas.getTimer()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3792)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3968)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8506)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.test_game.MyCanvas.getTimer()' on a null object reference
at com.example.test_game.Eindscherm.onCreate(Eindscherm.java:24)
at android.app.Activity.performCreate(Activity.java:8198)
at android.app.Activity.performCreate(Activity.java:8182)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3765)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3968)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8506)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)