java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.substring(int, int) Fatal Exception main
Asked
Active
Viewed 1,482 times
-3
-
please add your code – Sami Kanafani Jun 28 '17 at 17:54
-
@SamiKanafani this is not necessary, it is a duplicate anyhow. – Henry Jun 28 '17 at 17:56
1 Answers
0
This exception is thrown when you are trying to use the substring method on a null string, try checking the value of the string you are invoking the substring method. If you want to avoid the crash you can use the following code
if(str!=null){
//where the str is the string you are invoking the method on
substr = str.substring(7, 17);
}

Sami Kanafani
- 14,244
- 6
- 45
- 41
-
Since this is more often than not a programming error, the crash is preferable over some strange misbehaviour due to substr not being set. (Fail fast). – Henry Jun 28 '17 at 18:15