Potentiometer and analogue channels

Hey guys, our code returns a NullPointerException because it was already allocated to that channel, but we are 100% sure that it was not. We have code specifically designed to prevent this here:

if(potentiometerOn) {
            if(potentiometer instanceof AnalogChannel) {
                potentiometerEnabled = true;
            }else {
                potentiometerEnabled = false;
                potentiometer = new AnalogChannel(potentiometerChannel);
                if(potentiometer instanceof AnalogChannel) {
                    potentiometerEnabled = true;
                }
            }
        }

Is it that the variable potentiometer is not registered as an instance of AnalogueChannel (A weird class or something?) Maybe if we used potentiometerEnabled as our qualifier to create the new instance it would work? We are a bit confused at this.

If the code says it was enabled somewhere, it was enabled somewhere (I think the channel used to read the battery voltage is exposed on the header pins - you might be trying to use it).

However, since that exception throwing is mostly to keep you from mixing up what you are using channels for (if, for example, you happen to double-allocate a channel), you can bypass it and get the reading for that channel number with:

AnalogModule.getInstance(slot).getVoltage(chan);

Optionally storing the instance of the AnalogModule in your own variable.

Yeah I ended up removing all potentiometer code and starting from scratch on it. Havn’t tested yet but it should work. Otherwise, I’ll just set up a singleton like you said. Thanks :slight_smile: