• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problem in accessing string array resources

I have following code in my main Activity.

Code:
private List<Notes> chap = new ArrayList<>();
 Resources res = getResources();
    String[] chap1Titles = res.getStringArray(R.array.Introductiontitles);
    String[] chap1Notes = res.getStringArray(R.array.Introductionnotes);
    private void initializeData(){
        chap.add(new Notes(chap1Titles[0],chap1Notes[0]));
        chap.add(new Notes(chap1Titles[1],chap1Notes[1]));
        }

In Android Studio it does not give any error for this code.But when I launch Activity it's display nothing and gets stopped. When I changed this code as follows it worked. I am not understanding problem with above code.
Working code:

Code:
private List<Notes> chap = new ArrayList<>();
 
    private void initializeData(){
        chap.add(new Notes("Introduction ","About Introduction topic"));
        chap.add(new Notes("Motion ","About Motion topic"));
        }
 
Last edited by a moderator:
Logcat:

04-11 18:16:30.150 24035-24035/? D/dalvikvm: Late-enabling CheckJNI
04-11 18:16:30.290 24035-24035/? D/ActivityThread: handleBindApplication:com.ilearn.itemdetail
04-11 18:16:30.300 24035-24035/? D/ActivityThread: setTargetHeapUtilization:0.75
04-11 18:16:30.300 24035-24035/com.ilearn.itemdetail D/ActivityThread: setTargetHeapMinFree:2097152
04-11 18:16:30.370 24035-24035/com.ilearn.itemdetail D/AndroidRuntime: Shutting down VM
04-11 18:16:30.370 24035-24035/com.ilearn.itemdetail W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x415e3d58)
04-11 18:16:30.380 24035-24035/com.ilearn.itemdetail E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ilearn.itemdetail, PID: 24035
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ilearn.itemdetail/com.ilearn.itemdetail.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2126)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5113)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
at com.ilearn.itemdetail.MainActivity.<init>(MainActivity.java:23)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2117)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5113)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
 
Logcat:

04-11 18:16:30.150 24035-24035/? D/dalvikvm: Late-enabling CheckJNI
04-11 18:16:30.290 24035-24035/? D/ActivityThread: handleBindApplication:com.ilearn.itemdetail
04-11 18:16:30.300 24035-24035/? D/ActivityThread: setTargetHeapUtilization:0.75
04-11 18:16:30.300 24035-24035/com.ilearn.itemdetail D/ActivityThread: setTargetHeapMinFree:2097152
04-11 18:16:30.370 24035-24035/com.ilearn.itemdetail D/AndroidRuntime: Shutting down VM
04-11 18:16:30.370 24035-24035/com.ilearn.itemdetail W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x415e3d58)
04-11 18:16:30.380 24035-24035/com.ilearn.itemdetail E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ilearn.itemdetail, PID: 24035
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ilearn.itemdetail/com.ilearn.itemdetail.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2126)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5113)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
at com.ilearn.itemdetail.MainActivity.<init>(MainActivity.java:23)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2117)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5113)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
 
You have a null pointer exception at line 23 in MainActivity
 
Back
Top Bottom