Inside one of my java files I have to use this new instance of a TextView in two separate methods, so rather than creating the new instances of the TextView in both methods I thought I would just declare it outside of the methods although still inside the class.
I didn't receive any errors before compiling however when I tried to get to the activity this class is concerned with the app crashes. I fixed this by putting the declaration in both separate methods and the app stopped crashing I was just wondering why and would there be a more eloquent way of doing it as I'm trying to stick to the DRY principle.
Here's the line of code:
final TextView tv_scoreDisplay = (TextView) findViewById(R.id.tv_scoreDisplay);
Logcat:
10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: FATAL EXCEPTION: main 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: Process: com.example.aaron.dartpro, PID: 9474 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.aaron.dartpro/com.example.aaron.dartpro.FiveZeroOne}: java.lang.NullPointerException 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:135) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5001) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: Caused by: java.lang.NullPointerException 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.Activity.findViewById(Activity.java:1884) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at com.example.aaron.dartpro.FiveZeroOne.(FiveZeroOne.java:26) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at java.lang.Class.newInstanceImpl(Native Method) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at java.lang.Class.newInstance(Class.java:1208) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.Instrumentation.newActivity(Instrumentation.java:1061) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:135) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5001) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)