Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8873523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:29:09+00:00 2026-06-14T18:29:09+00:00

I created an array of int values. These int values are the path id

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:29:11+00:00Added an answer on June 14, 2026 at 6:29 pm

    However, the picItem array is null.

    You had me fooled for a bit. picItems is fine. relative is null, initialize it in onCreate():

    relative = (RelativeLayout) findViewById(R.id.layout); 
                                // Where "layout" is the RelativeLayout's id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function which accepts some values as char array[] parameters. These values
Okay, I've created an array in c# and want to access the values in
how can an array be created that randomizes results from 3 different values 0,1,2
Im trying to create a method that take 2 int array as the input
When I use new[] to create an array of my classes: int count =
I create a 2D dynamic array: a = (int**)calloc(n-1, sizeof(int)); for(i = 0; i
I have two arrays that I create like this: public int GameBoard[][] = new
i have created Array of Linkbutton and when user click on link button it
I have created an array square(40) as picturebox. I have to assign 40 picture
I have an array created with this code: var widthRange = new Array(); widthRange[46]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.