-2

I have a DataHandaling class that is used to do the logic, it is also suppose to be an easier way to retrieve an ArrayList from multiple activities, But when I start a new activity the ArrayList is null

(NOTE: The array is not empty because I have used the same array multiple times in the main activity, this problem is only a case when I start a new activity)

The code from DataHandaling

public class DataHandaling {
    EnviroClass enviroClass = new EnviroClass();
    private ArrayList<Event> eventArray = new ArrayList<Event>();

    public ArrayList<Event> getEventArray() {
        return eventArray;
    }

    public void getEvents() {
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(enviroClass.url() + "/api/events/" + 7)
                .build();

        client.newCall(request)
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        try {
                            JSONObject eventObject = new JSONObject(response.body().string());
                            JSONArray eventJsonArray = eventObject.getJSONArray("events");
                            for (int i = 0; i<eventJsonArray.length(); i++) {
                                eventObject = eventJsonArray.getJSONObject(i);
                                eventObject = eventObject.getJSONObject("event");
                                eventArray.add(new Event(eventObject.getString("name"), eventObject.getString("address"), eventObject.getString("image"), "100" ,eventObject.getString("start_date"), eventObject.getString("end_date"), eventObject.getInt("id")));
                            }
                            EventListAdapter adapter = new EventListAdapter(null, null);
                            adapter.swap(eventArray);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

}

This is where it is breaking in the new activity

textView.setText(dh.getEventArray().get(0).getName());

The interaction listener to open the new activity

@Override
public void onListFragmentInteraction(int id) {
    Intent intent = new Intent(MainActivity.this, ProfileActivity.class);
    intent.putExtra("id", id);
    MainActivity.this.startActivity(intent);
}

The activity where it is breaking

public class ProfileActivity extends AppCompatActivity {

    DataHandaling dh = new DataHandaling();

    int eventArrayId;

    TextView eventDescription;
    TextView eventName;
    ImageView eventImage;

    ArrayList<Event> eventArray = new ArrayList<Event>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        getSupportActionBar().setHomeButtonEnabled(true);

        Intent intent = getIntent();
        eventArrayId = intent.getIntExtra("id", 0);

        eventName = (TextView) findViewById(R.id.eventNameTextView);

        eventName.setText(dh.getEventArray().get(0).getName());

    }



}

Stack Trace:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.gb.socilizer_app/com.app.gb.socilizer.Activities.ProfileActivity}: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                      at android.os.Looper.loop(Looper.java:154)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                                   Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
                                                                                      at java.util.ArrayList.get(ArrayList.java:411)
                                                                                      at com.app.garethbecker.socializer.Activities.ProfileActivity.onCreate(ProfileActivity.java:39)
                                                                                      at android.app.Activity.performCreate(Activity.java:6679)
                                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                      at android.os.Looper.loop(Looper.java:154) 
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

 

gprathour
  • 14,813
  • 5
  • 66
  • 90
TheFairywarrior
  • 718
  • 1
  • 8
  • 16
  • Do you want to pass this list between different Activities?? or just want to use it inside a single activity?? – sumit Jun 09 '17 at 11:12
  • I'm trying to use it between 2 activities, The information in it is gotten from the server, So I didn't want to call 10 times for information coming from the same table in the database. – TheFairywarrior Jun 09 '17 at 11:28
  • provide the code for your DataHandaling class. – Nabin Bhandari Jun 09 '17 at 11:30
  • There we go, Though there are just the 2 methods, I know that the array isn't empty because I used that array in a `ListView` – TheFairywarrior Jun 09 '17 at 11:35
  • You cannot simply get reference of the object creating new object. You need to write a code to use same instance everywhere. Learn the basics of objects and classes first. You are wrong at ProfileActivity first line: DataHandaling dh = new DataHandaling(); – Nabin Bhandari Jun 09 '17 at 11:38

3 Answers3

1

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.

gprathour
  • 14,813
  • 5
  • 66
  • 90
0

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.

Asew
  • 374
  • 2
  • 13
  • No I was just typing out the code here instead of copying and pasting it xD, That would of helped me a lot if it was just that xD – TheFairywarrior Jun 09 '17 at 11:07
  • Ahah okay. So what error message are you getting ? – Asew Jun 09 '17 at 11:08
  • `java.lang.IndexOutOfBoundsException` which is weird because I used the same array a couple other times without a problem – TheFairywarrior Jun 09 '17 at 11:10
  • 1
    Can you edit your post and add the full stack trace ? Also can you provide the code section, and not just one line. The only thing I can tell you right now with the current information, is that your array is empty. – Asew Jun 09 '17 at 11:11
  • @TheMysticalWarrior Updated my answer. – Asew Jun 09 '17 at 11:34
0

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>.

Hasnain Ali Bohra
  • 2,130
  • 2
  • 11
  • 25