I got this simple code:
String ip = "1.2.3.4";
String[] ipArray = ip.split(".");
System.out.println(ipArray[1]);
And ipArray is null by the time it hits System.out.println (throws null pointer exception).
My question is why does ipArray stay null even though I’m setting it to split on each of ip’s .s?
Use
ip.split("\\.");and your problems will be solved. The problem is that String#split receives a regular expression, the dot (.) symbol is a special character in regular expressions, so you need to escape it for it to be interpreted as a plain dot, also as the backslash is an escape character in Java, you have to escape it too.