4

This makes no sense to me, I am getting an index out of bounds exception from some code that fetches and splits a response from the server to determine a users account details and account type.

Crash Report (Reason):

07-06 12:11:30.602 2376-2376/lsa1314.com.zip E/AndroidRuntime: FATAL EXCEPTION: main
Process: lsa1314.com.zip, PID: 2376
                                                                   java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at lsa1314.com.zip.loggedInDefault$2.onResponse(loggedInDefault.java:130)
at lsa1314.com.zip.loggedInDefault$2.onResponse(loggedInDefault.java:120)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Now obviously that is telling me based on looking at the lines around 130 as stated in the report above that I am getting an array index out of bounds exception. This appears to point to when splitting the line of the response from server.

//Split lines to separate response
String[] responseSplitter = response.split(":");
String firstName = responseSplitter[0];
String surname = responseSplitter[1];
String accType = responseSplitter[2];

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
prefs.edit().putString("first_name", firstName).commit();
prefs.edit().putString("surname", surname).commit();
prefs.edit().putString("account_type", accType).commit();

This error is usually caused when there is no index [1,2,3,etc] and I am trying to access it. The only thing that is confusing me is that said index works just fine on a mobile phone but not on a tablet??

I try this in 2 nexus emulators and the phone works but not the tablet. The above error is what the tablet produces.

I doubt it is relevant but I will include what the PHP is returning to the device on the server side incase it is needed:

echo json_encode($firstName . ":" . $surname . ":" . $account_type);
Dhruv
  • 1,862
  • 3
  • 20
  • 38
Luke Swain
  • 223
  • 2
  • 7
  • 1
    As you have shown very less code, I can suggest you to put if condition to check size of array just after the line of splitting response, so you will get idea as you are getting or not response, also try to toast or print your response so you can check what you get in response – Vickyexpert Jul 06 '16 at 04:51
  • 1
    Why don't you print/debug log the `response` variable so you can see the difference on the tablet? – fdsa Jul 06 '16 at 04:53
  • I will try this now. – Luke Swain Jul 06 '16 at 04:54
  • Why don't you debug the code and see for yourself? It should be a simple matter of setting a breakpoint at the `String[] responseSplitter = response.split(":");`, checking the content of `response` and `responseSplitter` and debugging the code on a physical/emulator tablet. – EvilTak Jul 06 '16 at 04:55
  • The response is normal on the phone and blank on the tablet, so I now know why the error is being thrown but cannot figure out why it is blank on one device but not the other? – Luke Swain Jul 06 '16 at 04:56
  • What resource do you access on the server that returns the response? Is it same for both Mobile and PC (the server may be redirecting you to the mobile version on a phone which returns the correct response, but on a tablet it may be sending you to the PC version of the resource which may not be returning the correct result)? Do you get the same response on a desktop as you get on a mobile phone? – EvilTak Jul 06 '16 at 05:06
  • It is using volley. Just a simple stringRequest. They both return the same string but for some reason the tablet gets a blank message? – Luke Swain Jul 06 '16 at 05:09
  • check your network connection is working or not properly on tablet, then check the API level of tablet – Vickyexpert Jul 06 '16 at 05:10
  • The app checks for connection before trying this and throws an error if it fails. (Already tested) They are both on API 23 (Marshmallow) – Luke Swain Jul 06 '16 at 05:12
  • then check for permission at this site : https://developer.android.com/training/permissions/requesting.html – Vickyexpert Jul 06 '16 at 05:17
  • Vicky - It would not work on either device if it was a permission issue? – Luke Swain Jul 06 '16 at 05:20
  • then now show your code for sending request – Vickyexpert Jul 06 '16 at 05:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116534/discussion-between-luke-swain-and-vickyexpert). – Luke Swain Jul 06 '16 at 05:24

0 Answers0