I created an array of int values. These int values are the path id to a drawable image like so:
int[] picItems = {R.drawable.pic_one, R.drawable.pic_two, R.drawable.pic_three};
However, the picItem array is null. I want to be able to take the value from an index and use it as the background, like so:
relative.setBackgroundResource(picItems[index]);
Now, if I use the above code and set the parameter field as R.drawable.pic_one, for example, it works. But, whenever I declare and instantiate the int array picItems, I get a nullpointerexception. I must be over looking something and making a simple mistake but if I am please feel free to point it out. I appreciate any help, thanks!
EDIT: I have tried many different ways of declaring and instantiating the array. And the array is null even after instantiating it but hits an error when trying to use it (I used an if statement to catch it). Here’s a little more code:
public class Snow extends Activity{
int[] picItems;
RelativeLayout relative;
SharedPreferences sp;
int checkedItem = 0;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
picItems = new int[]{R.drawable.pic_one, R.drawable.pic_two, R.drawable.pic_three};
sp = getPreferences(MODE_PRIVATE);
checkedItem = sp.getInt("picture", 0);
if (!(picItems == null) || !(picItems.equals(null))){
Toast.makeText(getApplicationContext(), "Array is null for some reason", Toast.LENGTH_SHORT).show();
}else{
relative.setBackgroundResource(picItems[checkedItem]);
}//end of if
...} ...}
And Here’s the Logcat Output:
11-20 19:00:27.363: E/AndroidRuntime(18311): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.Snow/com.app.snow.Snow}: java.lang.NullPointerException
11-20 19:00:27.363: E/AndroidRuntime(18311): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1821)
11-20 19:00:27.363: E/AndroidRuntime(18311): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
11-20 19:00:27.363: E/AndroidRuntime(18311): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
11-20 19:00:27.363: E/AndroidRuntime(18311): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
11-20 19:00:27.363: E/AndroidRuntime(18311): at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 19:00:27.363: E/AndroidRuntime(18311): at android.os.Looper.loop(Looper.java:150)
11-20 19:00:27.363: E/AndroidRuntime(18311): at android.app.ActivityThread.main(ActivityThread.java:4263)
11-20 19:00:27.363: E/AndroidRuntime(18311): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 19:00:27.363: E/AndroidRuntime(18311): at java.lang.reflect.Method.invoke(Method.java:507)
11-20 19:00:27.363: E/AndroidRuntime(18311): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-20 19:00:27.363: E/AndroidRuntime(18311): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-20 19:00:27.363: E/AndroidRuntime(18311): at dalvik.system.NativeStart.main(Native Method)
11-20 19:00:27.363: E/AndroidRuntime(18311): Caused by: java.lang.NullPointerException
11-20 19:00:27.363: E/AndroidRuntime(18311): at com.app.snow.Snow.onCreate(Snow.java:54)
11-20 19:00:27.363: E/AndroidRuntime(18311): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
11-20 19:00:27.363: E/AndroidRuntime(18311): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
You had me fooled for a bit.
picItemsis fine.relativeis null, initialize it inonCreate():