'Local hidden variable field and null pointer exception

I'm getting a warning in my IDE (Java NetBeans) and an error when running, and I don't understand what I'm doing wrong.

The warning is about a hidden field by a local variable and I just want to create a boolean array of 65536 bits.

public class Main
{
    private static boolean[] BusyDevices ;
    
    /* main entry point */
    public static void main(String argv[])
    {

       boolean BusyDevices[]=new boolean[65536];//<-Here the warning
       
    }
       
    public static boolean isDeviceBusy(String deviceIDx)
    {
        if(deviceIDx.length()>4 || deviceIDx.length()<4)
        {
            return false;
        }
       
        try
        {
            return BusyDevices[Integer.parseInt(deviceIDx, 16)];
        }
        catch(Exception e)            
        {
            Print.logException("Error deviceIDx:" + deviceIDx, e);           
            return false;
        }       
    }     
}

I think that I'm doing something wrong when resizing the boolean array; Or can be a conversion error? Plus I need to start with all bits in false state, That's the default state right?.



Solution 1:[1]

You have two variables with the same name BusyDevices because of which this issue is happening, Since the class level variable is conflicting with the main method variable.

Try to change the variable name in the below line:

 boolean BusyDevices[]=new boolean[65536];//<-Here the warning

as

 BusyDevices=new boolean[65536];//No duplicate variable now, variable type declation is not required

Solution 2:[2]

Here you are declaring a static field of the class Main called BusyDevices

private static boolean[] BusyDevices ;

Here you are declaring a local variable with the same name in the static method main of the class Main

boolean BusyDevices[]=new boolean[65536];

In such a case the local variable declaration takes precedence and "hides" or "shadows" the field of the class

You probably intended to do this, to initialize the field (instead of a local variable)

BusyDevices = new boolean[65536];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Manos Nikolaidis