Null object in array - Android Base adapter

0

Hello I was doing a gallery, I created the Adapter, it worked normally, then proceed to the step of listing files in a directory and then put it 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 has already been created, so how do you create an array of objects to add to an array list?

    
asked by anonymous 18.01.2018 / 15:23

1 answer

2

When you use GalleryItem item[] = new GalleryItem[files.length]; you just allocated array space, did not fill it with anything, then item[i] is empty.

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

    
18.01.2018 / 17:31