-5

I am working on android application. I want to get specific index of id , but it returns always first no of index id or application crashes .please help me how to solve error. Thanks

here is my code:

 protected Void doInBackground(Void... arg0)
    {
        HttpService httpService = new HttpService("http://192.168.0.103/GetActiveGroupMember.php");
        try
        {
            httpService.ExecutePostRequest();

            if(httpService.getResponseCode() == 200)
            {
                result = httpService.getResponse();
                ArrayList<Integer> m_idlist = null;

                listid = new ArrayList<>();
                listid.add(result);
               // System.out.println("ListID...  "+listid+" ListId Size "+listid.size());
                System.out.println("ListID...  "+listid+" ListId Size "+listid.size());
             //   Log.d("Result", result);
                System.out.println("Result "+result);
                if(result != null)
                {
                    JSONArray jsonArray = null;
                    try {
                        jsonArray = new JSONArray(result);

                        JSONObject object;
                        JSONArray array;

                        member m;
                        members = new ArrayList<member>();

                        for(int i=0; i<jsonArray.length(); i++)
                        {
                            m= new member();
                            object = jsonArray.getJSONObject(i);

                          //  m.username = object.getString("username");
                            m.id = object.getInt("id");

                            members.add(m);

                            int m_id  = m.id;
                            System.out.println("mem_id  = "+m_id);

                            m_idlist = new ArrayList<Integer>();
                            m_idlist.add(i);

                          System.out.println("five no id = "+m_idlist.get(i)); // TO print all Id

                        }
                         if(m_idlist.size()>5){
                            System.out.println("five no id = "+m_idlist.get(5));
                           }
                     //   System.out.println("Total member  = "+members.size());

                    }

Error:

W/System.err: java.lang.IndexOutOfBoundsException: Invalid index 5, size is 1
W/System.err:     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
W/System.err:     at java.util.ArrayList.get(ArrayList.java:308)
W/System.err:     at com.example.moraya.adminmodule.GetActiveGroupMemberACtivity$GetHttpResponse.doInBackground(GetActiveGroupMemberACtivity.java:121)
W/System.err:     at com.example.moraya.adminmodule.GetActiveGroupMemberACtivity$GetHttpResponse.doInBackground(GetActiveGroupMemberACtivity.java:56)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err:     at java.lang.Thread.run(Thread.java:841)
W/EGL_genymotion: eglSurfaceAttrib not implemented

Thank you so much.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
gaikwad
  • 1
  • 3
  • Your list only contains **one** item, so there is no item with index 5. – Denny Apr 20 '17 at 10:16
  • 2
    System.out.println("five no id = "+m_idlist.get(5)); you're pushing the system at index 5 when it only have 1 item in list – jace Apr 20 '17 at 10:17
  • 1
    `m_idlist.get(5)` if your list is not having 6 elements in total than it will always throw this error. Add a check for list size and put this code in that condition. – Vivek Mishra Apr 20 '17 at 10:17
  • what is the value of listid.size() & m_idlist.size()? – bhaumiksoni Apr 20 '17 at 10:17
  • Agreed with everybody else, you need to have a check statement ( a simple if statement ) to check if the element exists – Ryan Turnbull Apr 20 '17 at 10:18
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Manoj Perumarath Apr 20 '17 at 11:28

2 Answers2

0
System.out.println("five no id = "+m_idlist.get(5));

You probably have no element in the list with index 5, add a if statement to check your index with the size of the list.

In your case:

if (5 < m_idlist.size()) { ... }
Zezima
  • 17
  • 4
  • k.sir, Actually I have 10 id in int m_id = m.id; and i am adding m_id into Arraylistlist as m_idlist.add(m_id); and after that I am getting the id of 5th no position by using System.out.println("five no id = "+m_idlist.get(5)); Then it returns 1st no of position id..please help me . – gaikwad Apr 20 '17 at 10:48
  • No, it is said that there is only 1 element `Invalid index 5, size is 1` – AxelH Apr 20 '17 at 11:10
  • Then what are the println's printing as @bhaumiksoni asked? – Zezima Apr 20 '17 at 11:21
0

you have to declare this line m_idlist = new ArrayList<Integer>(); above of for loop

Bhupat Bheda
  • 1,968
  • 1
  • 8
  • 13