Error in custom java code

0
I have written below method to filter the IP addresses in IPv6 format and show the IP addresses from ONLY IPv4 format. There is no error while compilation. But, when I run the project, I get error   com.mendix.webui.WebUIException: Exception while executing runtime operation    at com.mendix.webui.actions.client.RuntimeOperationAction.$anonfun$apply$3(RuntimeOperationAction.scala:60) Caused by: com.mendix.modules.microflowengine.MicroflowException: com.mendix.systemwideinterfaces.MendixRuntimeException: java.lang.NullPointerException: Cannot invoke "java.net.Inet4Address.toString()" because "<local2>[<local5>]" is null     I cannot test this in local because I don't get IPv6 addresses from localhost. I only get them in higher env like ACCP, but I cannot debug the java code remotely.   @java.lang.Override public java.lang.String executeAction() throws Exception { // BEGIN USER CODE try {             //InetAddress host = InetAddress.getByName(InputHostName); //return host.getHostAddress();             //InetAddress[] ipAddress = DNSNameService.lookupAllHostAddr(InputHostName); //InetAddress[] inetAddressArray = InetAddress.getAllByName(InputHostName); InetAddress[] inetAddresses = InetAddress.getAllByName(InputHostName);   if(inetAddresses==null) return ""; java.net.Inet4Address[] inetAddressArray = new java.net.Inet4Address[inetAddresses.length]; int ipv4Count = 0; for(InetAddress address : inetAddresses) {   if(address instanceof java.net.Inet4Address) {     inetAddressArray[ipv4Count++] = (java.net.Inet4Address) address;   } } String ipaddresslist = new String(); for(int i=0;i<inetAddressArray.length;++i) { String str = inetAddressArray[i].toString(); str=str.substring(str.indexOf('/')+1, str.length()); ipaddresslist= ipaddresslist+str+","; } ipaddresslist=ipaddresslist.substring(0, ipaddresslist.length()-1);             return ipaddresslist;                      } catch (UnknownHostException ex) {             return "";         } // END USER CODE }
asked
2 answers
0

​Hi Satya,

The error you're seeing:

java.lang.NullPointerException: Cannot invoke "java.net.Inet4Address.toString()" because "<local2>[<local5>]" is null

...indicates that your inetAddressArray contains null values. This happens because you're pre-allocating an array of size inetAddresses.length and only partially filling it when the address is IPv4. So, unused slots remain null, and later, you attempt to call .toString() on them.

 

 

as for the solution,

You should not pre-allocate the array and instead use a dynamic structure like a List to store only the non-null IPv4 addresses.

you can use a code something like this (sample code, you can make changes accordingly),

@java.lang.Overridepublic java.lang.String executeAction() throws Exception {    // BEGIN USER CODE    try {        InetAddress[] inetAddresses = InetAddress.getAllByName(InputHostName);          if (inetAddresses == null || inetAddresses.length == 0) {            return "";        }

        List<String> ipv4Addresses = new ArrayList<>();

        for (InetAddress address : inetAddresses) {            if (address instanceof java.net.Inet4Address) {                String ip = address.getHostAddress(); // safer and cleaner than toString + substring                ipv4Addresses.add(ip);            }        }

        if (ipv4Addresses.isEmpty()) {            return ""; // or return "No IPv4 address found"        }

        return String.join(",", ipv4Addresses);

    } catch (UnknownHostException ex) {        return "";    }    // END USER CODE}

 

Let me know, if you have any issues,

Hope it helps!

answered
0

Is your for loop correct? It looks like you are incrementing variable i before using it (++i instead of i++), so it will be one more than the size of your array on its last iteration, giving you the null error. 

answered