Failed to retrieve ArrayList from a separate class

This question already has an answer here:

  • Initialization of an ArrayList in one line 32 answers

  • You are creating new object of DataHandling in the activity. So it will not be the same object that you had previously used and which has data in it.


    First :- If your ArrayList<Event> is empty and if you get 0th index element then you'll get NullPointerException while Executing this line :- dh.getEventArray().get(0).getName() so it is replaced by null.getName() which will break.

    So if you want to get index basis Element form collection firsat checkl whether the Collection is empty or not. Like this:-

        //if the ArrayList is not Empty then only iterate over it.
        if(!dh.getEventArray().isEmpty())
        {
            textView.setText(dh.getEventArray().get(0).getName());
        }
    

    Second:- Check the logic for adding the Event In ArrayList<Event> .


    Your method is called getArray() , yet you're invoking getEventArray() . Maybe it is the reason.

    EDIT: Now that your message is edit we have something to work on.

    DataHandaling dh = new DataHandaling();
    

    This is a new DataHandaling object, and never in your code you're updating him. So I guess that its ArrayList is empty by default.

    链接地址: http://www.djcxy.com/p/27722.html

    上一篇: 为什么Java不提供运算符重载?

    下一篇: 无法从单独的类中检索ArrayList