What is IndexOutOfBoundsException? How can I fix it? [duplicate]












6
















This question already has an answer here:




  • What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

    23 answers




This is my code:



    private void bringData() {
final TextView mTextView = (TextView) findViewById(R.id.textView);

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://192.168.4.1:8080/";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}


It's the default given from the android documentation. I only changed the url.



This is my error message:




java.lang.StringIndexOutOfBoundsException: length=28; regionStart=1;
regionLength=499
at java.lang.String.substring(String.java:1931)
at com.example.my.app.MainActivity$2.onResponse(MainActivity.java:50)
at com.example.my.app.MainActivity$2.onResponse(MainActivity.java:46)
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:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)




During debugging I see that at mTextView.setText("Response is: "+ response.substring(0,500)); my message is delivered to me but the textview is never updated and the app crashes.



Specifically, it crashes here inside the Looper.Java file:



finally {
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}


traceTag is 0.



I read that some string bounds are wrong but I cannot find out how to fix it.










share|improve this question















marked as duplicate by Nilesh Rathod android
Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Sep 18 '18 at 5:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • read the documentation for StringIndexOutOfBoundsException. Then read the error you receive.

    – njzk2
    Oct 12 '16 at 18:59











  • Related: What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

    – Dukeling
    Jan 8 '18 at 11:20
















6
















This question already has an answer here:




  • What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

    23 answers




This is my code:



    private void bringData() {
final TextView mTextView = (TextView) findViewById(R.id.textView);

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://192.168.4.1:8080/";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}


It's the default given from the android documentation. I only changed the url.



This is my error message:




java.lang.StringIndexOutOfBoundsException: length=28; regionStart=1;
regionLength=499
at java.lang.String.substring(String.java:1931)
at com.example.my.app.MainActivity$2.onResponse(MainActivity.java:50)
at com.example.my.app.MainActivity$2.onResponse(MainActivity.java:46)
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:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)




During debugging I see that at mTextView.setText("Response is: "+ response.substring(0,500)); my message is delivered to me but the textview is never updated and the app crashes.



Specifically, it crashes here inside the Looper.Java file:



finally {
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}


traceTag is 0.



I read that some string bounds are wrong but I cannot find out how to fix it.










share|improve this question















marked as duplicate by Nilesh Rathod android
Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Sep 18 '18 at 5:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • read the documentation for StringIndexOutOfBoundsException. Then read the error you receive.

    – njzk2
    Oct 12 '16 at 18:59











  • Related: What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

    – Dukeling
    Jan 8 '18 at 11:20














6












6








6


2







This question already has an answer here:




  • What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

    23 answers




This is my code:



    private void bringData() {
final TextView mTextView = (TextView) findViewById(R.id.textView);

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://192.168.4.1:8080/";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}


It's the default given from the android documentation. I only changed the url.



This is my error message:




java.lang.StringIndexOutOfBoundsException: length=28; regionStart=1;
regionLength=499
at java.lang.String.substring(String.java:1931)
at com.example.my.app.MainActivity$2.onResponse(MainActivity.java:50)
at com.example.my.app.MainActivity$2.onResponse(MainActivity.java:46)
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:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)




During debugging I see that at mTextView.setText("Response is: "+ response.substring(0,500)); my message is delivered to me but the textview is never updated and the app crashes.



Specifically, it crashes here inside the Looper.Java file:



finally {
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}


traceTag is 0.



I read that some string bounds are wrong but I cannot find out how to fix it.










share|improve this question

















This question already has an answer here:




  • What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

    23 answers




This is my code:



    private void bringData() {
final TextView mTextView = (TextView) findViewById(R.id.textView);

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://192.168.4.1:8080/";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}


It's the default given from the android documentation. I only changed the url.



This is my error message:




java.lang.StringIndexOutOfBoundsException: length=28; regionStart=1;
regionLength=499
at java.lang.String.substring(String.java:1931)
at com.example.my.app.MainActivity$2.onResponse(MainActivity.java:50)
at com.example.my.app.MainActivity$2.onResponse(MainActivity.java:46)
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:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)




During debugging I see that at mTextView.setText("Response is: "+ response.substring(0,500)); my message is delivered to me but the textview is never updated and the app crashes.



Specifically, it crashes here inside the Looper.Java file:



finally {
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}


traceTag is 0.



I read that some string bounds are wrong but I cannot find out how to fix it.





This question already has an answer here:




  • What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

    23 answers








java android android-volley






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 '18 at 17:04









Mark Rotteveel

60.3k1477121




60.3k1477121










asked Oct 12 '16 at 18:56









travellertraveller

72118




72118




marked as duplicate by Nilesh Rathod android
Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Sep 18 '18 at 5:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Nilesh Rathod android
Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Sep 18 '18 at 5:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • read the documentation for StringIndexOutOfBoundsException. Then read the error you receive.

    – njzk2
    Oct 12 '16 at 18:59











  • Related: What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

    – Dukeling
    Jan 8 '18 at 11:20



















  • read the documentation for StringIndexOutOfBoundsException. Then read the error you receive.

    – njzk2
    Oct 12 '16 at 18:59











  • Related: What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

    – Dukeling
    Jan 8 '18 at 11:20

















read the documentation for StringIndexOutOfBoundsException. Then read the error you receive.

– njzk2
Oct 12 '16 at 18:59





read the documentation for StringIndexOutOfBoundsException. Then read the error you receive.

– njzk2
Oct 12 '16 at 18:59













Related: What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

– Dukeling
Jan 8 '18 at 11:20





Related: What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

– Dukeling
Jan 8 '18 at 11:20












1 Answer
1






active

oldest

votes


















10














Error Message:
java.lang.StringIndexOutOfBoundsException: length=28; regionStart=1;
regionLength=499 at java.lang.String.substring(String.java:1931) at
com.example.my.app.MainActivity$2.onResponse(MainActivity.java:50) at
com.example.my.app.MainActivity$2.onResponse(MainActivity.java:46) 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:751) at
android.os.Handler.dispatchMessage(Handler.java:95) at
android.os.Looper.loop(Looper.java:154) at
android.app.ActivityThread.main(ActivityThread.java:6077) at
java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)


Error Description



There is an IndexOutOfBound exception occurred in your MainActivity class 
Inside second inner class's OnResponse function as shown MainActivity$2onResponse
on line 46 which basically occurred during substring operation in String.java line 1931
which was invoked from StringRequest.deliverResponse at line 60,
which was invoked from StringRequest.deliverResponse at line 30,
which was invoked from ExecutorDelivery.java at line 99,
which intially started from ZygoteInit$MethodAndArgsCaller's run function
and reached up-to main thread of ActivityThread.main=>looper=>handler


Actual Reason



Your code trying to create a substring with



starting index = 0
ending index = 500


though your actual response string length is = 28, so String length is not long enough to create a substring of 500 characters.



Solutions :





  1. Validate length using ternary operator ?:



    mTextView.setText("Response is: "+ 
    ((response.length()>499) ? response.substring(0,500) : "length is too short"));


    Note : Ternary operator (?:) is a short expression of if else but it is not a statement mean it cannot occur as an atomic statement as this is INVALID because there is no assignment



    ((someString.length()>499) ? someString.substring(0,500):"Invalid length");



  2. if-else enhances the visibility



    String msg="Invalid Response";
    if(response.length()>499){
    msg=response.substring(0,500);
    }
    mTextView.setText("Response is: "+msg);

    //or mTextView.setText("Response is: "+response);



What is IndexOutOfBoundsException?




IndexOutOfBoundsException is a subclass of RuntimeException mean
it is an unchecked exception which is thrown to indicate that an index
of some sort (such as to an array, to a string, or to a vector) is out
of range.e.g using List.




as shown in the Documentation




List<String> ls=new ArrayList<>();
ls.add("a");
ls.add("b");
ls.get(3); // will throw IndexOutOfBoundsException , list length is 2



Prevention :



String str = "";
int index =3;
if(index < ls.size()) // check, list size must be greater than index
str = ls.get(index);
else
// print invalid index or other stuff


Class Constructor usage with index or string message



public IndexOutOfBoundsException() {
super();
}

public IndexOutOfBoundsException(String s) {
super(s);
}


Which are other variations/sub-classes of IndexOutOfBoundsException?





  • ArrayIndexOutOfBoundsException : This indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array for e.g




    int arr = {1,2,3}
    int error = arr[-1]; // no negative index allowed
    int error2 = arr[4]; // arr length is 3 as index range is 0-2




Prevention :



int num = "";
int index=4;
if(index < arr.length) // check, array length must be greater than index
num = arr[index];
else
// print invalid index or other stuff




  • StringIndexOutOfBoundsException : This is thrown by String methods to indicate that an index is either negative or greater than the size of the string. For some methods such as the charAt method, this exception also is thrown when the index is equal to the size of the string.




    String str = "foobar";       // length = 6
    char error = str.charAt(7); // index input should be less than or equal to length-1
    char error = str.charAt(-1); // cannot use negative indexes




Prevention :



String name = "FooBar";
int index = 7;
char holder = '';
if(index < name.length()) // check, String length must be greater than index
holder = name.charAt(index) ;
else
// print invalid index or other stuff


Note: length() is a function of String class and length is a associative field of an array.



Why these exception occur?




  • Usage of Negative index with arrays or charAt , substring functions

  • BeginIndex is less than 0 or endIndex is greater than the length of input string to create substring or beginIndex is larger than the endIndex

  • When endIndex - beginIndex result is less than 0

  • When input string/array is empty


INFO : It is job of JVM to create the object of appropriate exception and pass it to the place of , where it occurred using throw keyword like or you can also do it manually using throw keyword too.



if (s == null) {
throw new IndexOutOfBoundsException("null");
}


How can I fix this ?




  1. Analyzing StackTrace

  2. Validating input string against nullity , length or valid indexes

  3. Using Debugging or Logs

  4. Using Generic Exception catch block


1.) Analyzing StackTrace



As shown at the beginning of this post , stacktrace provides the necessary information in the initial messages about where it happen , why it happen so you can simply trace that code and apply the required solution .



for e.g the reason StringIndexOutOfBoundsException and then look for your package name indicating your class file then go to that line and keeping the reason in mind , simply apply the solution



It's a head start if you study about the exception and its cause as well in documentation.



2.) Validating input string against nullity , length or valid indexes



In case of uncertainty when you don't know about the actual input like response is coming from server (or maybe it's an error or nothing) or user then it's always better to cover all the unexpected cases though believe me few users always like to push the limits of testing so use input!=null && input.length()>0 or for indexes, you can use the ternary operator or if-else boundary check conditions



3.) Using Debugging or Logs



You can test the running environment of your project in debug mode by adding break-points in your project and system will stop there to wait for your next action and meanwhile you can look into the values of variables and other details.



Logs are just like check-points so when your control cross this point they generate details , basically they are informative messages given by wither system or user can also put logging messages using either Logs or Println messages



4.) Using Generic Exception catch block



Try-catch blocks are always useful to handle RuntimeExceptions so you can use multiple catch block along to handle your possible issues and to give appropriate details



try {
mTextView.setText("Response is: "+ response.substring(0,500));
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
System.out,println("Invalid indexes or empty string");
}
catch (NullPointerException e) { // mTextView or response can be null
e.printStackTrace();
System.out,println("Something went wrong ,missed initialization");
}
catch (Exception e) {
e.printStackTrace();
System.out,println("Something unexpected happened , move on or can see stacktrace ");
}


Further References



What is a NullPointerException, and how do I fix it?






share|improve this answer


























  • Thanks but now it only displays the "whatever you want to display". So the response.length() is not more than 499.

    – traveller
    Oct 12 '16 at 19:12











  • @traveller because as i said in my answer, the length of your response is 28 so you can't create a string of 500 characters and about "whatever you want to display" , it is just a string , so whatever you give here will be displayed

    – Pavneet_Singh
    Oct 12 '16 at 19:14













  • @traveller check the updated answer for clarity and can try commented code too

    – Pavneet_Singh
    Oct 12 '16 at 19:21






  • 1





    I think I get it now. My string is only 28 characters and I am trying to create a substring of length of 500, obviously I can't but I would be able if my string was longer than 500 characters.

    – traveller
    Oct 12 '16 at 19:27











  • I need to ask, why 500 characters? Is this the maximum length of a textview?

    – traveller
    Oct 12 '16 at 19:27


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









10














Error Message:
java.lang.StringIndexOutOfBoundsException: length=28; regionStart=1;
regionLength=499 at java.lang.String.substring(String.java:1931) at
com.example.my.app.MainActivity$2.onResponse(MainActivity.java:50) at
com.example.my.app.MainActivity$2.onResponse(MainActivity.java:46) 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:751) at
android.os.Handler.dispatchMessage(Handler.java:95) at
android.os.Looper.loop(Looper.java:154) at
android.app.ActivityThread.main(ActivityThread.java:6077) at
java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)


Error Description



There is an IndexOutOfBound exception occurred in your MainActivity class 
Inside second inner class's OnResponse function as shown MainActivity$2onResponse
on line 46 which basically occurred during substring operation in String.java line 1931
which was invoked from StringRequest.deliverResponse at line 60,
which was invoked from StringRequest.deliverResponse at line 30,
which was invoked from ExecutorDelivery.java at line 99,
which intially started from ZygoteInit$MethodAndArgsCaller's run function
and reached up-to main thread of ActivityThread.main=>looper=>handler


Actual Reason



Your code trying to create a substring with



starting index = 0
ending index = 500


though your actual response string length is = 28, so String length is not long enough to create a substring of 500 characters.



Solutions :





  1. Validate length using ternary operator ?:



    mTextView.setText("Response is: "+ 
    ((response.length()>499) ? response.substring(0,500) : "length is too short"));


    Note : Ternary operator (?:) is a short expression of if else but it is not a statement mean it cannot occur as an atomic statement as this is INVALID because there is no assignment



    ((someString.length()>499) ? someString.substring(0,500):"Invalid length");



  2. if-else enhances the visibility



    String msg="Invalid Response";
    if(response.length()>499){
    msg=response.substring(0,500);
    }
    mTextView.setText("Response is: "+msg);

    //or mTextView.setText("Response is: "+response);



What is IndexOutOfBoundsException?




IndexOutOfBoundsException is a subclass of RuntimeException mean
it is an unchecked exception which is thrown to indicate that an index
of some sort (such as to an array, to a string, or to a vector) is out
of range.e.g using List.




as shown in the Documentation




List<String> ls=new ArrayList<>();
ls.add("a");
ls.add("b");
ls.get(3); // will throw IndexOutOfBoundsException , list length is 2



Prevention :



String str = "";
int index =3;
if(index < ls.size()) // check, list size must be greater than index
str = ls.get(index);
else
// print invalid index or other stuff


Class Constructor usage with index or string message



public IndexOutOfBoundsException() {
super();
}

public IndexOutOfBoundsException(String s) {
super(s);
}


Which are other variations/sub-classes of IndexOutOfBoundsException?





  • ArrayIndexOutOfBoundsException : This indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array for e.g




    int arr = {1,2,3}
    int error = arr[-1]; // no negative index allowed
    int error2 = arr[4]; // arr length is 3 as index range is 0-2




Prevention :



int num = "";
int index=4;
if(index < arr.length) // check, array length must be greater than index
num = arr[index];
else
// print invalid index or other stuff




  • StringIndexOutOfBoundsException : This is thrown by String methods to indicate that an index is either negative or greater than the size of the string. For some methods such as the charAt method, this exception also is thrown when the index is equal to the size of the string.




    String str = "foobar";       // length = 6
    char error = str.charAt(7); // index input should be less than or equal to length-1
    char error = str.charAt(-1); // cannot use negative indexes




Prevention :



String name = "FooBar";
int index = 7;
char holder = '';
if(index < name.length()) // check, String length must be greater than index
holder = name.charAt(index) ;
else
// print invalid index or other stuff


Note: length() is a function of String class and length is a associative field of an array.



Why these exception occur?




  • Usage of Negative index with arrays or charAt , substring functions

  • BeginIndex is less than 0 or endIndex is greater than the length of input string to create substring or beginIndex is larger than the endIndex

  • When endIndex - beginIndex result is less than 0

  • When input string/array is empty


INFO : It is job of JVM to create the object of appropriate exception and pass it to the place of , where it occurred using throw keyword like or you can also do it manually using throw keyword too.



if (s == null) {
throw new IndexOutOfBoundsException("null");
}


How can I fix this ?




  1. Analyzing StackTrace

  2. Validating input string against nullity , length or valid indexes

  3. Using Debugging or Logs

  4. Using Generic Exception catch block


1.) Analyzing StackTrace



As shown at the beginning of this post , stacktrace provides the necessary information in the initial messages about where it happen , why it happen so you can simply trace that code and apply the required solution .



for e.g the reason StringIndexOutOfBoundsException and then look for your package name indicating your class file then go to that line and keeping the reason in mind , simply apply the solution



It's a head start if you study about the exception and its cause as well in documentation.



2.) Validating input string against nullity , length or valid indexes



In case of uncertainty when you don't know about the actual input like response is coming from server (or maybe it's an error or nothing) or user then it's always better to cover all the unexpected cases though believe me few users always like to push the limits of testing so use input!=null && input.length()>0 or for indexes, you can use the ternary operator or if-else boundary check conditions



3.) Using Debugging or Logs



You can test the running environment of your project in debug mode by adding break-points in your project and system will stop there to wait for your next action and meanwhile you can look into the values of variables and other details.



Logs are just like check-points so when your control cross this point they generate details , basically they are informative messages given by wither system or user can also put logging messages using either Logs or Println messages



4.) Using Generic Exception catch block



Try-catch blocks are always useful to handle RuntimeExceptions so you can use multiple catch block along to handle your possible issues and to give appropriate details



try {
mTextView.setText("Response is: "+ response.substring(0,500));
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
System.out,println("Invalid indexes or empty string");
}
catch (NullPointerException e) { // mTextView or response can be null
e.printStackTrace();
System.out,println("Something went wrong ,missed initialization");
}
catch (Exception e) {
e.printStackTrace();
System.out,println("Something unexpected happened , move on or can see stacktrace ");
}


Further References



What is a NullPointerException, and how do I fix it?






share|improve this answer


























  • Thanks but now it only displays the "whatever you want to display". So the response.length() is not more than 499.

    – traveller
    Oct 12 '16 at 19:12











  • @traveller because as i said in my answer, the length of your response is 28 so you can't create a string of 500 characters and about "whatever you want to display" , it is just a string , so whatever you give here will be displayed

    – Pavneet_Singh
    Oct 12 '16 at 19:14













  • @traveller check the updated answer for clarity and can try commented code too

    – Pavneet_Singh
    Oct 12 '16 at 19:21






  • 1





    I think I get it now. My string is only 28 characters and I am trying to create a substring of length of 500, obviously I can't but I would be able if my string was longer than 500 characters.

    – traveller
    Oct 12 '16 at 19:27











  • I need to ask, why 500 characters? Is this the maximum length of a textview?

    – traveller
    Oct 12 '16 at 19:27
















10














Error Message:
java.lang.StringIndexOutOfBoundsException: length=28; regionStart=1;
regionLength=499 at java.lang.String.substring(String.java:1931) at
com.example.my.app.MainActivity$2.onResponse(MainActivity.java:50) at
com.example.my.app.MainActivity$2.onResponse(MainActivity.java:46) 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:751) at
android.os.Handler.dispatchMessage(Handler.java:95) at
android.os.Looper.loop(Looper.java:154) at
android.app.ActivityThread.main(ActivityThread.java:6077) at
java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)


Error Description



There is an IndexOutOfBound exception occurred in your MainActivity class 
Inside second inner class's OnResponse function as shown MainActivity$2onResponse
on line 46 which basically occurred during substring operation in String.java line 1931
which was invoked from StringRequest.deliverResponse at line 60,
which was invoked from StringRequest.deliverResponse at line 30,
which was invoked from ExecutorDelivery.java at line 99,
which intially started from ZygoteInit$MethodAndArgsCaller's run function
and reached up-to main thread of ActivityThread.main=>looper=>handler


Actual Reason



Your code trying to create a substring with



starting index = 0
ending index = 500


though your actual response string length is = 28, so String length is not long enough to create a substring of 500 characters.



Solutions :





  1. Validate length using ternary operator ?:



    mTextView.setText("Response is: "+ 
    ((response.length()>499) ? response.substring(0,500) : "length is too short"));


    Note : Ternary operator (?:) is a short expression of if else but it is not a statement mean it cannot occur as an atomic statement as this is INVALID because there is no assignment



    ((someString.length()>499) ? someString.substring(0,500):"Invalid length");



  2. if-else enhances the visibility



    String msg="Invalid Response";
    if(response.length()>499){
    msg=response.substring(0,500);
    }
    mTextView.setText("Response is: "+msg);

    //or mTextView.setText("Response is: "+response);



What is IndexOutOfBoundsException?




IndexOutOfBoundsException is a subclass of RuntimeException mean
it is an unchecked exception which is thrown to indicate that an index
of some sort (such as to an array, to a string, or to a vector) is out
of range.e.g using List.




as shown in the Documentation




List<String> ls=new ArrayList<>();
ls.add("a");
ls.add("b");
ls.get(3); // will throw IndexOutOfBoundsException , list length is 2



Prevention :



String str = "";
int index =3;
if(index < ls.size()) // check, list size must be greater than index
str = ls.get(index);
else
// print invalid index or other stuff


Class Constructor usage with index or string message



public IndexOutOfBoundsException() {
super();
}

public IndexOutOfBoundsException(String s) {
super(s);
}


Which are other variations/sub-classes of IndexOutOfBoundsException?





  • ArrayIndexOutOfBoundsException : This indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array for e.g




    int arr = {1,2,3}
    int error = arr[-1]; // no negative index allowed
    int error2 = arr[4]; // arr length is 3 as index range is 0-2




Prevention :



int num = "";
int index=4;
if(index < arr.length) // check, array length must be greater than index
num = arr[index];
else
// print invalid index or other stuff




  • StringIndexOutOfBoundsException : This is thrown by String methods to indicate that an index is either negative or greater than the size of the string. For some methods such as the charAt method, this exception also is thrown when the index is equal to the size of the string.




    String str = "foobar";       // length = 6
    char error = str.charAt(7); // index input should be less than or equal to length-1
    char error = str.charAt(-1); // cannot use negative indexes




Prevention :



String name = "FooBar";
int index = 7;
char holder = '';
if(index < name.length()) // check, String length must be greater than index
holder = name.charAt(index) ;
else
// print invalid index or other stuff


Note: length() is a function of String class and length is a associative field of an array.



Why these exception occur?




  • Usage of Negative index with arrays or charAt , substring functions

  • BeginIndex is less than 0 or endIndex is greater than the length of input string to create substring or beginIndex is larger than the endIndex

  • When endIndex - beginIndex result is less than 0

  • When input string/array is empty


INFO : It is job of JVM to create the object of appropriate exception and pass it to the place of , where it occurred using throw keyword like or you can also do it manually using throw keyword too.



if (s == null) {
throw new IndexOutOfBoundsException("null");
}


How can I fix this ?




  1. Analyzing StackTrace

  2. Validating input string against nullity , length or valid indexes

  3. Using Debugging or Logs

  4. Using Generic Exception catch block


1.) Analyzing StackTrace



As shown at the beginning of this post , stacktrace provides the necessary information in the initial messages about where it happen , why it happen so you can simply trace that code and apply the required solution .



for e.g the reason StringIndexOutOfBoundsException and then look for your package name indicating your class file then go to that line and keeping the reason in mind , simply apply the solution



It's a head start if you study about the exception and its cause as well in documentation.



2.) Validating input string against nullity , length or valid indexes



In case of uncertainty when you don't know about the actual input like response is coming from server (or maybe it's an error or nothing) or user then it's always better to cover all the unexpected cases though believe me few users always like to push the limits of testing so use input!=null && input.length()>0 or for indexes, you can use the ternary operator or if-else boundary check conditions



3.) Using Debugging or Logs



You can test the running environment of your project in debug mode by adding break-points in your project and system will stop there to wait for your next action and meanwhile you can look into the values of variables and other details.



Logs are just like check-points so when your control cross this point they generate details , basically they are informative messages given by wither system or user can also put logging messages using either Logs or Println messages



4.) Using Generic Exception catch block



Try-catch blocks are always useful to handle RuntimeExceptions so you can use multiple catch block along to handle your possible issues and to give appropriate details



try {
mTextView.setText("Response is: "+ response.substring(0,500));
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
System.out,println("Invalid indexes or empty string");
}
catch (NullPointerException e) { // mTextView or response can be null
e.printStackTrace();
System.out,println("Something went wrong ,missed initialization");
}
catch (Exception e) {
e.printStackTrace();
System.out,println("Something unexpected happened , move on or can see stacktrace ");
}


Further References



What is a NullPointerException, and how do I fix it?






share|improve this answer


























  • Thanks but now it only displays the "whatever you want to display". So the response.length() is not more than 499.

    – traveller
    Oct 12 '16 at 19:12











  • @traveller because as i said in my answer, the length of your response is 28 so you can't create a string of 500 characters and about "whatever you want to display" , it is just a string , so whatever you give here will be displayed

    – Pavneet_Singh
    Oct 12 '16 at 19:14













  • @traveller check the updated answer for clarity and can try commented code too

    – Pavneet_Singh
    Oct 12 '16 at 19:21






  • 1





    I think I get it now. My string is only 28 characters and I am trying to create a substring of length of 500, obviously I can't but I would be able if my string was longer than 500 characters.

    – traveller
    Oct 12 '16 at 19:27











  • I need to ask, why 500 characters? Is this the maximum length of a textview?

    – traveller
    Oct 12 '16 at 19:27














10












10








10







Error Message:
java.lang.StringIndexOutOfBoundsException: length=28; regionStart=1;
regionLength=499 at java.lang.String.substring(String.java:1931) at
com.example.my.app.MainActivity$2.onResponse(MainActivity.java:50) at
com.example.my.app.MainActivity$2.onResponse(MainActivity.java:46) 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:751) at
android.os.Handler.dispatchMessage(Handler.java:95) at
android.os.Looper.loop(Looper.java:154) at
android.app.ActivityThread.main(ActivityThread.java:6077) at
java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)


Error Description



There is an IndexOutOfBound exception occurred in your MainActivity class 
Inside second inner class's OnResponse function as shown MainActivity$2onResponse
on line 46 which basically occurred during substring operation in String.java line 1931
which was invoked from StringRequest.deliverResponse at line 60,
which was invoked from StringRequest.deliverResponse at line 30,
which was invoked from ExecutorDelivery.java at line 99,
which intially started from ZygoteInit$MethodAndArgsCaller's run function
and reached up-to main thread of ActivityThread.main=>looper=>handler


Actual Reason



Your code trying to create a substring with



starting index = 0
ending index = 500


though your actual response string length is = 28, so String length is not long enough to create a substring of 500 characters.



Solutions :





  1. Validate length using ternary operator ?:



    mTextView.setText("Response is: "+ 
    ((response.length()>499) ? response.substring(0,500) : "length is too short"));


    Note : Ternary operator (?:) is a short expression of if else but it is not a statement mean it cannot occur as an atomic statement as this is INVALID because there is no assignment



    ((someString.length()>499) ? someString.substring(0,500):"Invalid length");



  2. if-else enhances the visibility



    String msg="Invalid Response";
    if(response.length()>499){
    msg=response.substring(0,500);
    }
    mTextView.setText("Response is: "+msg);

    //or mTextView.setText("Response is: "+response);



What is IndexOutOfBoundsException?




IndexOutOfBoundsException is a subclass of RuntimeException mean
it is an unchecked exception which is thrown to indicate that an index
of some sort (such as to an array, to a string, or to a vector) is out
of range.e.g using List.




as shown in the Documentation




List<String> ls=new ArrayList<>();
ls.add("a");
ls.add("b");
ls.get(3); // will throw IndexOutOfBoundsException , list length is 2



Prevention :



String str = "";
int index =3;
if(index < ls.size()) // check, list size must be greater than index
str = ls.get(index);
else
// print invalid index or other stuff


Class Constructor usage with index or string message



public IndexOutOfBoundsException() {
super();
}

public IndexOutOfBoundsException(String s) {
super(s);
}


Which are other variations/sub-classes of IndexOutOfBoundsException?





  • ArrayIndexOutOfBoundsException : This indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array for e.g




    int arr = {1,2,3}
    int error = arr[-1]; // no negative index allowed
    int error2 = arr[4]; // arr length is 3 as index range is 0-2




Prevention :



int num = "";
int index=4;
if(index < arr.length) // check, array length must be greater than index
num = arr[index];
else
// print invalid index or other stuff




  • StringIndexOutOfBoundsException : This is thrown by String methods to indicate that an index is either negative or greater than the size of the string. For some methods such as the charAt method, this exception also is thrown when the index is equal to the size of the string.




    String str = "foobar";       // length = 6
    char error = str.charAt(7); // index input should be less than or equal to length-1
    char error = str.charAt(-1); // cannot use negative indexes




Prevention :



String name = "FooBar";
int index = 7;
char holder = '';
if(index < name.length()) // check, String length must be greater than index
holder = name.charAt(index) ;
else
// print invalid index or other stuff


Note: length() is a function of String class and length is a associative field of an array.



Why these exception occur?




  • Usage of Negative index with arrays or charAt , substring functions

  • BeginIndex is less than 0 or endIndex is greater than the length of input string to create substring or beginIndex is larger than the endIndex

  • When endIndex - beginIndex result is less than 0

  • When input string/array is empty


INFO : It is job of JVM to create the object of appropriate exception and pass it to the place of , where it occurred using throw keyword like or you can also do it manually using throw keyword too.



if (s == null) {
throw new IndexOutOfBoundsException("null");
}


How can I fix this ?




  1. Analyzing StackTrace

  2. Validating input string against nullity , length or valid indexes

  3. Using Debugging or Logs

  4. Using Generic Exception catch block


1.) Analyzing StackTrace



As shown at the beginning of this post , stacktrace provides the necessary information in the initial messages about where it happen , why it happen so you can simply trace that code and apply the required solution .



for e.g the reason StringIndexOutOfBoundsException and then look for your package name indicating your class file then go to that line and keeping the reason in mind , simply apply the solution



It's a head start if you study about the exception and its cause as well in documentation.



2.) Validating input string against nullity , length or valid indexes



In case of uncertainty when you don't know about the actual input like response is coming from server (or maybe it's an error or nothing) or user then it's always better to cover all the unexpected cases though believe me few users always like to push the limits of testing so use input!=null && input.length()>0 or for indexes, you can use the ternary operator or if-else boundary check conditions



3.) Using Debugging or Logs



You can test the running environment of your project in debug mode by adding break-points in your project and system will stop there to wait for your next action and meanwhile you can look into the values of variables and other details.



Logs are just like check-points so when your control cross this point they generate details , basically they are informative messages given by wither system or user can also put logging messages using either Logs or Println messages



4.) Using Generic Exception catch block



Try-catch blocks are always useful to handle RuntimeExceptions so you can use multiple catch block along to handle your possible issues and to give appropriate details



try {
mTextView.setText("Response is: "+ response.substring(0,500));
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
System.out,println("Invalid indexes or empty string");
}
catch (NullPointerException e) { // mTextView or response can be null
e.printStackTrace();
System.out,println("Something went wrong ,missed initialization");
}
catch (Exception e) {
e.printStackTrace();
System.out,println("Something unexpected happened , move on or can see stacktrace ");
}


Further References



What is a NullPointerException, and how do I fix it?






share|improve this answer















Error Message:
java.lang.StringIndexOutOfBoundsException: length=28; regionStart=1;
regionLength=499 at java.lang.String.substring(String.java:1931) at
com.example.my.app.MainActivity$2.onResponse(MainActivity.java:50) at
com.example.my.app.MainActivity$2.onResponse(MainActivity.java:46) 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:751) at
android.os.Handler.dispatchMessage(Handler.java:95) at
android.os.Looper.loop(Looper.java:154) at
android.app.ActivityThread.main(ActivityThread.java:6077) at
java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)


Error Description



There is an IndexOutOfBound exception occurred in your MainActivity class 
Inside second inner class's OnResponse function as shown MainActivity$2onResponse
on line 46 which basically occurred during substring operation in String.java line 1931
which was invoked from StringRequest.deliverResponse at line 60,
which was invoked from StringRequest.deliverResponse at line 30,
which was invoked from ExecutorDelivery.java at line 99,
which intially started from ZygoteInit$MethodAndArgsCaller's run function
and reached up-to main thread of ActivityThread.main=>looper=>handler


Actual Reason



Your code trying to create a substring with



starting index = 0
ending index = 500


though your actual response string length is = 28, so String length is not long enough to create a substring of 500 characters.



Solutions :





  1. Validate length using ternary operator ?:



    mTextView.setText("Response is: "+ 
    ((response.length()>499) ? response.substring(0,500) : "length is too short"));


    Note : Ternary operator (?:) is a short expression of if else but it is not a statement mean it cannot occur as an atomic statement as this is INVALID because there is no assignment



    ((someString.length()>499) ? someString.substring(0,500):"Invalid length");



  2. if-else enhances the visibility



    String msg="Invalid Response";
    if(response.length()>499){
    msg=response.substring(0,500);
    }
    mTextView.setText("Response is: "+msg);

    //or mTextView.setText("Response is: "+response);



What is IndexOutOfBoundsException?




IndexOutOfBoundsException is a subclass of RuntimeException mean
it is an unchecked exception which is thrown to indicate that an index
of some sort (such as to an array, to a string, or to a vector) is out
of range.e.g using List.




as shown in the Documentation




List<String> ls=new ArrayList<>();
ls.add("a");
ls.add("b");
ls.get(3); // will throw IndexOutOfBoundsException , list length is 2



Prevention :



String str = "";
int index =3;
if(index < ls.size()) // check, list size must be greater than index
str = ls.get(index);
else
// print invalid index or other stuff


Class Constructor usage with index or string message



public IndexOutOfBoundsException() {
super();
}

public IndexOutOfBoundsException(String s) {
super(s);
}


Which are other variations/sub-classes of IndexOutOfBoundsException?





  • ArrayIndexOutOfBoundsException : This indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array for e.g




    int arr = {1,2,3}
    int error = arr[-1]; // no negative index allowed
    int error2 = arr[4]; // arr length is 3 as index range is 0-2




Prevention :



int num = "";
int index=4;
if(index < arr.length) // check, array length must be greater than index
num = arr[index];
else
// print invalid index or other stuff




  • StringIndexOutOfBoundsException : This is thrown by String methods to indicate that an index is either negative or greater than the size of the string. For some methods such as the charAt method, this exception also is thrown when the index is equal to the size of the string.




    String str = "foobar";       // length = 6
    char error = str.charAt(7); // index input should be less than or equal to length-1
    char error = str.charAt(-1); // cannot use negative indexes




Prevention :



String name = "FooBar";
int index = 7;
char holder = '';
if(index < name.length()) // check, String length must be greater than index
holder = name.charAt(index) ;
else
// print invalid index or other stuff


Note: length() is a function of String class and length is a associative field of an array.



Why these exception occur?




  • Usage of Negative index with arrays or charAt , substring functions

  • BeginIndex is less than 0 or endIndex is greater than the length of input string to create substring or beginIndex is larger than the endIndex

  • When endIndex - beginIndex result is less than 0

  • When input string/array is empty


INFO : It is job of JVM to create the object of appropriate exception and pass it to the place of , where it occurred using throw keyword like or you can also do it manually using throw keyword too.



if (s == null) {
throw new IndexOutOfBoundsException("null");
}


How can I fix this ?




  1. Analyzing StackTrace

  2. Validating input string against nullity , length or valid indexes

  3. Using Debugging or Logs

  4. Using Generic Exception catch block


1.) Analyzing StackTrace



As shown at the beginning of this post , stacktrace provides the necessary information in the initial messages about where it happen , why it happen so you can simply trace that code and apply the required solution .



for e.g the reason StringIndexOutOfBoundsException and then look for your package name indicating your class file then go to that line and keeping the reason in mind , simply apply the solution



It's a head start if you study about the exception and its cause as well in documentation.



2.) Validating input string against nullity , length or valid indexes



In case of uncertainty when you don't know about the actual input like response is coming from server (or maybe it's an error or nothing) or user then it's always better to cover all the unexpected cases though believe me few users always like to push the limits of testing so use input!=null && input.length()>0 or for indexes, you can use the ternary operator or if-else boundary check conditions



3.) Using Debugging or Logs



You can test the running environment of your project in debug mode by adding break-points in your project and system will stop there to wait for your next action and meanwhile you can look into the values of variables and other details.



Logs are just like check-points so when your control cross this point they generate details , basically they are informative messages given by wither system or user can also put logging messages using either Logs or Println messages



4.) Using Generic Exception catch block



Try-catch blocks are always useful to handle RuntimeExceptions so you can use multiple catch block along to handle your possible issues and to give appropriate details



try {
mTextView.setText("Response is: "+ response.substring(0,500));
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
System.out,println("Invalid indexes or empty string");
}
catch (NullPointerException e) { // mTextView or response can be null
e.printStackTrace();
System.out,println("Something went wrong ,missed initialization");
}
catch (Exception e) {
e.printStackTrace();
System.out,println("Something unexpected happened , move on or can see stacktrace ");
}


Further References



What is a NullPointerException, and how do I fix it?







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 8 '18 at 17:09









Mark Rotteveel

60.3k1477121




60.3k1477121










answered Oct 12 '16 at 19:00









Pavneet_SinghPavneet_Singh

26.5k42544




26.5k42544













  • Thanks but now it only displays the "whatever you want to display". So the response.length() is not more than 499.

    – traveller
    Oct 12 '16 at 19:12











  • @traveller because as i said in my answer, the length of your response is 28 so you can't create a string of 500 characters and about "whatever you want to display" , it is just a string , so whatever you give here will be displayed

    – Pavneet_Singh
    Oct 12 '16 at 19:14













  • @traveller check the updated answer for clarity and can try commented code too

    – Pavneet_Singh
    Oct 12 '16 at 19:21






  • 1





    I think I get it now. My string is only 28 characters and I am trying to create a substring of length of 500, obviously I can't but I would be able if my string was longer than 500 characters.

    – traveller
    Oct 12 '16 at 19:27











  • I need to ask, why 500 characters? Is this the maximum length of a textview?

    – traveller
    Oct 12 '16 at 19:27



















  • Thanks but now it only displays the "whatever you want to display". So the response.length() is not more than 499.

    – traveller
    Oct 12 '16 at 19:12











  • @traveller because as i said in my answer, the length of your response is 28 so you can't create a string of 500 characters and about "whatever you want to display" , it is just a string , so whatever you give here will be displayed

    – Pavneet_Singh
    Oct 12 '16 at 19:14













  • @traveller check the updated answer for clarity and can try commented code too

    – Pavneet_Singh
    Oct 12 '16 at 19:21






  • 1





    I think I get it now. My string is only 28 characters and I am trying to create a substring of length of 500, obviously I can't but I would be able if my string was longer than 500 characters.

    – traveller
    Oct 12 '16 at 19:27











  • I need to ask, why 500 characters? Is this the maximum length of a textview?

    – traveller
    Oct 12 '16 at 19:27

















Thanks but now it only displays the "whatever you want to display". So the response.length() is not more than 499.

– traveller
Oct 12 '16 at 19:12





Thanks but now it only displays the "whatever you want to display". So the response.length() is not more than 499.

– traveller
Oct 12 '16 at 19:12













@traveller because as i said in my answer, the length of your response is 28 so you can't create a string of 500 characters and about "whatever you want to display" , it is just a string , so whatever you give here will be displayed

– Pavneet_Singh
Oct 12 '16 at 19:14







@traveller because as i said in my answer, the length of your response is 28 so you can't create a string of 500 characters and about "whatever you want to display" , it is just a string , so whatever you give here will be displayed

– Pavneet_Singh
Oct 12 '16 at 19:14















@traveller check the updated answer for clarity and can try commented code too

– Pavneet_Singh
Oct 12 '16 at 19:21





@traveller check the updated answer for clarity and can try commented code too

– Pavneet_Singh
Oct 12 '16 at 19:21




1




1





I think I get it now. My string is only 28 characters and I am trying to create a substring of length of 500, obviously I can't but I would be able if my string was longer than 500 characters.

– traveller
Oct 12 '16 at 19:27





I think I get it now. My string is only 28 characters and I am trying to create a substring of length of 500, obviously I can't but I would be able if my string was longer than 500 characters.

– traveller
Oct 12 '16 at 19:27













I need to ask, why 500 characters? Is this the maximum length of a textview?

– traveller
Oct 12 '16 at 19:27





I need to ask, why 500 characters? Is this the maximum length of a textview?

– traveller
Oct 12 '16 at 19:27



Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)