gkiar
gkiar

Reputation: 478

Declaring String Arrays Causing Issues

I am trying to declare a string array which I will be working with in my code. I have done this two ways, both of which I thought to be viable options, but one causes my app to crash on load without fail.

Working Code:

private String[] list = {"apple", "pear"};

Not Working Code:

Resources res = getResources();
private String[] list = res.getStringArray(R.array.Fruit);

The weird thing is, the latter code was copied directly from the Android String Resource page. I've made sure that the array (In this case titled Fruit) exists, and am now kind of lost.

The Log Cat Error I am getting is as follows:

02-25 12:38:18.015: ERROR/AndroidRuntime(605): FATAL EXCEPTION: main
02-25 12:38:18.015: ERROR/AndroidRuntime(605): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ikiar.appname/com.ikiar.appname.Main}: java.lang.NullPointerException
02-25 12:58:52.284: ERROR/AndroidRuntime(717):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
02-25 12:58:52.284: ERROR/AndroidRuntime(717):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-25 12:58:52.284: ERROR/AndroidRuntime(717):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-25 12:58:52.284: ERROR/AndroidRuntime(717):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-25 12:58:52.284: ERROR/AndroidRuntime(717):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 12:58:52.284: ERROR/AndroidRuntime(717):     at android.os.Looper.loop(Looper.java:123)
02-25 12:58:52.284: ERROR/AndroidRuntime(717):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-25 12:58:52.284: ERROR/AndroidRuntime(717):     at java.lang.reflect.Method.invokeNative(Native Method)
02-25 12:58:52.284: ERROR/AndroidRuntime(717):     at java.lang.reflect.Method.invoke(Method.java:521)
02-25 12:58:52.284: ERROR/AndroidRuntime(717):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-25 12:58:52.284: ERROR/AndroidRuntime(717):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-25 12:58:52.284: ERROR/AndroidRuntime(717):     at dalvik.system.NativeStart.main(Native Method)
02-25 12:58:52.284: ERROR/AndroidRuntime(717): Caused by: java.lang.NullPointerException
02-25 12:58:52.284: ERROR/AndroidRuntime(717):     at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
02-25 12:58:52.284: ERROR/AndroidRuntime(717):     at com.ikiar.appname.Main.<init>(Main.java:24)
02-25 12:58:52.284: ERROR/AndroidRuntime(717):     at java.lang.Class.newInstanceImpl(Native Method)
02-25 12:58:52.284: ERROR/AndroidRuntime(717):     at java.lang.Class.newInstance(Class.java:1429)
02-25 12:58:52.284: ERROR/AndroidRuntime(717):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-25 12:58:52.284: ERROR/AndroidRuntime(717):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)

Thank you for your help!

EDIT: More Log Cat information, Line of code @ location of crash added

@ Location of Crash:

Resources res = getResources();

Solution:

Moved the location of Resources res - getResources(); into the onCreate method.

Upvotes: 0

Views: 486

Answers (2)

user
user

Reputation: 87064

You've set the string-array in the string.xml file. If you want to use:

private String[] list = res.getStringArray(R.array.Fruit);

Try to create an array.xml file in the values folder and there put your Fruit array.

Upvotes: 2

Tarator
Tarator

Reputation: 1524

You get a Nullpointer Exception. I guess "res" is null. What does System.out.println(res) say!? When it says "null" then your res is not loaded properly.

Upvotes: 0

Related Questions