Null object array - Android Base Adapter

Asked

Viewed 95 times

0

Hello I was making a gallery, I created the Adapter, worked normally, so proceed to the step of listing files in a directory and then put in the list:

        ArrayList<GalleryItem>items = new ArrayList<>();

        File path = new File(getPath() + "//VideoPlayerPlus//");
        File files[] = path.listFiles();


        GalleryItem item[] = new GalleryItem[files.length];

        for(int i = 0; i < files.length; i++){

            item[i].setVideoTitle(files[i].getName());

            items.add(i, item[i]);

        }


        lvItems.setAdapter(new GalleryAdapter(this, items)); 

I am able to list the files normally, the problem is the array of objects, when I start the application, the following error appears in the gallery activity:

01-18 11:14:41.461 25583-25583/com.samuelives.videoplayer E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.samuelives.videoplayer, PID: 25583
                                                                            java.lang.RuntimeException: Unable to start activity ComponentInfo{com.samuelives.videoplayer/com.samuelives.videoplayer.Gallery}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.samuelives.videoplayer.gallery.GalleryItem.setVideoTitle(java.lang.String)' on a null object reference
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3190)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3300)
                                                                                at android.app.ActivityThread.access$1000(ActivityThread.java:211)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1705)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:145)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6946)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
                                                                             Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.samuelives.videoplayer.gallery.GalleryItem.setVideoTitle(java.lang.String)' on a null object reference
                                                                                at com.samuelives.videoplayer.Gallery.onCreate(Gallery.java:62)
                                                                                at android.app.Activity.performCreate(Activity.java:6575)
                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3143)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3300) 
                                                                                at android.app.ActivityThread.access$1000(ActivityThread.java:211) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1705) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                at android.os.Looper.loop(Looper.java:145) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6946)

It indicates a null object when it was already created, so how to create an array of objects to add to an array list?

1 answer

2


When you use GalleryItem item[] = new GalleryItem[files.length]; you just allocated the space of the array, didn’t fill it with anything, so item[i] is empty.

To initialize an array space, you need to use item[i] = new AlgumaCoisa(), usually within a for, to fill everything.

  • Thanks, I thought new Object[] besides allocating position in array, also served as constructor automatically .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.