...

View Full Version : Resolved int[] can only be null



alykins
12-07-2011, 01:55 AM
I have the following


public class getIP {

public String getMyIP(String args)
{
String _rtnString = "";
String[] ValidateThis = null;
int[] octets = null;
String spliton = ".";
ValidateThis = args.split(spliton);
if(ValidateThis[4] != "") _rtnString = "Invalid IP!";
else {
try{
for (int i=0; i<4; i++) {
octets[i] = Integer.parseInt(ValidateThis[i]);
}
_rtnString = args;

}

catch(Exception ex){
_rtnString = "Invalid IP!";
}
}
return _rtnString;
}

}


but my "intelli-sense" is throwing a warning @ line


octets[i] = Integer.parseInt(ValidateThis[i]);




"octets can only be null at this location"


? why

*for reference: I know there are better ways to validate IP's but since it is such a simple thing to do "the long way" and it forces one to use different techniques I am using it to play with android OS dev... normally I would figure out Java's equivalent to C# IPAddress.AddressFamily.InterNetwork

Fou-Lu
12-07-2011, 05:10 AM
octets itself is only declared as an int[] type, but has not been initialized.


int[] octets = new int[4];

Its been awhile, but does C# not work the same way?

This won't work consistently as well:


ValidateThis = args.split(spliton);
if(ValidateThis[4] != "") _rtnString = "Invalid IP!";

Strings in java are immutable. There is no guarantee that ValidateThis[4] equaling "" is equal to "" (as funny as that sounds :P). You can simply use ValidateThis[4].isEmpty() to detect if its empty. Here's a quick test to show this:


String s = new String("");
if (s == "") // This is inconsistent; constant "" != new String("") (and new String("") != new String(""))
{
System.out.println("s = \"\"");
}
if (s.equals(""))
{
System.out.println("s.equals(\"\")");
}
if (s.isEmpty())
{
System.out.println("s.isEmpty()");
}


Java's addresses are handled through the java.net package, and the classes InetAddress, Inet4Address, and Inet6Address.


try
{
InetAddress ip = InetAddress.getByName("127.0.0.1");
}
catch (UnknownHostException e)
{
e.printStackTrace();
}

If it throws, its not valid.

alykins
12-07-2011, 01:18 PM
kk cool thanx for that :) I guess I'm in for a lot of trial and error :D



int[] octets = new int[4];
does C# not work the same way?


yeah it is different apparently- If I take that same line in C# I would get octets[4] after the split so in reality if octets[4] even existed there would be an error thrown (which the catch would throw the appropriate string value)... I guess I should stop worrying about "can I make Java work" because I think I can- I should focus more on getting the android interface to work :p idk if you've done anything with it but it's a pain- and I really don't like the Eclipse IDE-

there are nice features about it (like "quick solutions" that VS does not have but I really like VS's intelli-sense) I guess this will also force me to stop being lazy :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum