0

In the loop, 'm' works from 0 to 4 but then crashes, instead of going from 0 to 6.

ArrayList<ItemTeacher> results = new ArrayList<ItemTeacher>();
String resultteacher="";
String[] binku2=null;

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try {
    HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost("http://"+localip+":8080/Apj/Teacher_Servlet");

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();

    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
         sb.append(line);
    }
    is.close();
    resultteacher = sb.toString();
    System.out.println("RESULT is : -**********"+resultteacher+"********-");


    String[] binku = resultteacher.split("@");

    System.out.println("LENGTH OF BINKU = "+binku.length);

    for(int m=0;m<=binku.length-1;m++)
    {

        binku2 = binku[m].split("#");
        System.out.println("Record number"+binku2[m]);
        drawable = LoadImageFromWebOperations("http://"+localip+":8080/Apj/TeacherImage/"+binku2[4]+".png");
        System.out.println("IMAGE DATA DISPLAY ="+drawable);

        ItemTeacher item_teacher = new ItemTeacher();

        item_teacher.setname(binku2[0]);
            item_teacher.setinfo(binku2[1]);
            item_teacher.setimage(drawable);
            String binku3[] = binku2[2].split("~");
            String binku4[] = binku2[3].split("~");

            String time="";
            String timeroom="";
            time =("Lecture Timings - Classroom");
            for(int i=0;i<=8;i++)
            {
            //  time +=("[Timings and Room]");
                timeroom +=("["+binku3[i]+" - "+binku4[i]+"]"+"      ");
                //System.out.println("Record number"+binku3[i]);
            }
            item_teacher.settime(time);
            item_teacher.setroom(timeroom);
            System.out.println("m value is ="+m);

            results.add(item_teacher);

    }
}


catch (Exception e) {
     System.out.println("Error parsing data " + e.toString());
}

return results;
}

Logcat details:

02-12 21:33:36.772: I/System.out(358): RESULT is : -**********Mrs Roopali Sood#M.C.M (Head IT Dept)#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#F8~Lab 1~Lab 3~F7~F8~S9~Free~S8~F8~#1@Mr Jagmohan Mago#M.C.A (IT Dept).#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#Free~S2~F8~S1~F9~Free~Lab 2~S9~Lab1~#2@Mr Munish Gupta#M.C.A (IT Dept).#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#S9~F8~S1~Lab 1~F1~Free~F8~Lab 3~S5~#3@Mrs Pallavi Chopra#M.C.A., M.Phil. (IT Dept).#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#Lab 2~Free~S2~F8~Free~Free~Lab 2~Free~Lab1~#4@Mr Abhejeet Singh#M.C.A (IT Dept).#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#Lab 3~Lab 1~Lab 3~S7~F8~Lab 2~Free~S8~S9~#5@Vinay#M.C.A (IT Dept).#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#Free~S2~F8~S1~F9~Free~Lab 2~S9~Lab1~#6@Bharti#Pata nai#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#S9~F8~S1~Lab 1~F1~Free~F8~Lab 3~S5~#7@********-
02-12 21:33:36.784: I/System.out(358): LENGTH OF BINKU = 7
02-12 21:33:36.784: I/System.out(358): Record numberMrs Roopali Sood
02-12 21:33:37.082: I/System.out(358): IMAGE DATA DISPLAY =android.graphics.drawable.BitmapDrawable@4618b990
02-12 21:33:37.102: I/System.out(358): m value is =0
02-12 21:33:37.102: I/System.out(358): Record numberM.C.A (IT Dept).
02-12 21:33:37.512: I/System.out(358): IMAGE DATA DISPLAY =android.graphics.drawable.BitmapDrawable@4619c2a8
02-12 21:33:37.522: I/System.out(358): m value is =1
02-12 21:33:37.522: I/System.out(358): Record number9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~
02-12 21:33:37.772: I/System.out(358): IMAGE DATA DISPLAY =android.graphics.drawable.BitmapDrawable@461ac610
02-12 21:33:37.782: I/System.out(358): m value is =2
02-12 21:33:37.792: I/System.out(358): Record numberLab 2~Free~S2~F8~Free~Free~Lab 2~Free~Lab1~
02-12 21:33:38.102: I/System.out(358): IMAGE DATA DISPLAY =android.graphics.drawable.BitmapDrawable@461bca98
02-12 21:33:38.112: I/System.out(358): m value is =3
02-12 21:33:38.112: I/System.out(358): Record number5
02-12 21:33:38.492: I/System.out(358): IMAGE DATA DISPLAY =android.graphics.drawable.BitmapDrawable@461ccc80
02-12 21:33:38.492: I/System.out(358): m value is =4
02-12 21:33:38.505: I/System.out(358): Error parsing data java.lang.ArrayIndexOutOfBoundsException

2 Answers 2

2

This is almost certainly not what you are intending:

binku2 = binku[m].split("#");
System.out.println("Record number"+binku2[m]);

You seem to operate everywhere as though the binku2 array will be of length 4, but then you attempt to access an index with the value of m. Thus, when you hit the 5th iteration of the loop, m is 4 and you throw the exception. Probably you meant to access binku here:

binku2 = binku[m].split("#");
System.out.println("Record number"+binku[m]);
Sign up to request clarification or add additional context in comments.

7 Comments

yes may be out of loop but i don't know how ? from database there are 7 values coming and it is showing only 5 i.e 0 to 4
There are 7 values in binku, not in binku2. Re-read my post: it looks like you're accessing the wrong array at that line.
no binku2 is for spliting the values of binku but i access binku for running loop from 0 to 7-1 but it is running 0 to 4 Data Mrs Roopali Sood#M.C.M (Head IT Dept)#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#F8~Lab 1~Lab 3~F7~F8~S9~Free~S8~F8~#1@
You're still not understanding what I'm saying. How many items to you expect binku2 to have? Right now you're accessing it at index m, which is based on the length of binku not the length of binku2. What happens if binku has 100 items? Then m will increase to 99 and you'll try to read a value at binku2[99]. Will that work? Your code seems to indicate "no".
i binku is 100 times the 100 values of data entry will show in list
|
1

You are setting yourself up for errors by hard coding your loops like this:

for(int i=0;i<=8;i++)

You should always use dynamic values like m<=binku.length-1 or the simpler version m < binku.length. And when you are working with more than one array it is safest to check each array's length:

for(int i = 0; i < binku3.length && i < binku4.length; i++)

4 Comments

It's actually reading from binku3 and binku4 at that point.
length of binku is 7 ie 0 to 6 (7-1) it works from 0 to 4 only
@eldarerathis Indeed, I think there is a wealth of errors to be found, I'll amend my answer but leave it up as general advice.
Yes, either way your point is entirely valid (and may very well cause an exception itself)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.