Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse String to Integer throws NullpointerException

Tags:

java

android

Hy!

I want to parse a String to a Integer. The String is like the format for series: SXXEXXX

The Code should increase the episode.

Like: S01E01 --> S01E02 Also: S01E100 --> S01E101

Code:

String s = episodes.get(episodes.size()-1);
Log.e("DBManager",s);
if (s.split("E").length <= 2) {
    int i = Integer.getInteger(s.split("E")[1].split(" ")[0]); //NullPointerEx
    return s.split("E")[0]+"E"+String.valueOf(i++);
}

Log:

10-26 15:56:34.635: E/DBManager(932): S00E01
like image 477
test123123 Avatar asked Nov 28 '25 23:11

test123123


1 Answers

Integer.getInteger() is not the correct method to use.

You should be using Integer.valueOf()

Integer.getInteger(String s) will return the integer system property with the key s.

The null pointer occurs because this method can't find the property with the key you supply and returns null. Java then tries to unbox this to an int and null pointers.

like image 92
Jim Avatar answered Dec 01 '25 13:12

Jim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!