sideshowbob
sideshowbob

Reputation: 41

String split returns null pointer exception

I'm trying to remove ".txt" extension from my file name yet I'm getting a NullPointerException on line 2.

Filename equals "myfilename.txt" and It's a string, I'd tried printing it

String token[]=fileName.split(".");
File JSONFile = new File(rootFile.getPath()+"\\"+token[0]+ ".json");

Upvotes: 0

Views: 58

Answers (1)

Hülya
Hülya

Reputation: 3433

. is a special character so you need to escape it with backslash \\

Try this:

String token[] = fileName.split("\\.");
File JSONFile = new File(rootFile.getPath()+"\\"+token[0]+ ".json");

Upvotes: 2

Related Questions