Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,729
    Thanks
    41
    Thanked 191 Times in 190 Posts

    int[] can only be null

    I have the following
    Code:
    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
    Code:
    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
    Last edited by alykins; 12-07-2011 at 12:18 PM.

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    octets itself is only declared as an int[] type, but has not been initialized.
    PHP Code:
    int[] octets = new int[4]; 
    Its been awhile, but does C# not work the same way?

    This won't work consistently as well:
    PHP Code:
            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:
    PHP Code:
            String s = new String("");
            if (
    == ""// 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.
    PHP Code:
            try
            {
                
    InetAddress ip InetAddress.getByName("127.0.0.1");
            }
            catch (
    UnknownHostException e)
            {
                
    e.printStackTrace();
            } 
    If it throws, its not valid.

  • #3
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,729
    Thanks
    41
    Thanked 191 Times in 190 Posts
    kk cool thanx for that I guess I'm in for a lot of trial and error
    Code:
    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 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

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •