Problems in inizializing classes with GPIO inputs

39 views
Skip to first unread message

Mario Cuollo Conforti

unread,
Nov 3, 2015, 5:46:51 AM11/3/15
to Pi4J
Hello everyone,
I'm getting some problems to initialize Classes.
I've a easy class Sensor.class, and its constructor which needs the pin to be initialized on.

public class Sensor {
public static Pin pin;
final static GpioController gpio = GpioFactory.getInstance();
private static final Logger logger = LogManager.getLogger(Sensor.class);
public static int stato;
final static GpioPinDigitalInput mySensor = gpio.provisionDigitalInputPin(pin);
public Sensor (Pin pinAssegnato) { //Constructor
pin=pinAssegnato;
}
public static void getStatoSensore() throws InterruptedException {
// create and register gpio pin listener
        mySensor.addListener(new GpioPinListenerDigital() {
            @Override
            public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
                // display pin state on console
                System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = " + event.getState());
            
            }
            
        });

}

Then I call this class from another class in this way. I make this thing because I need to initialize more than one Sensor Object at the time, every one with its own pinDelSensore, so i pass it through the constructor.

String pinDelSensore = "GPIO 25";
Sensor sensore = new Sensor(RaspiPin.getPinByName(pinDelSensore));


At this point I get this exception

Exception in thread "main" java.lang.reflect.InvocationTargetException

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:483)

at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

Caused by: java.lang.ExceptionInInitializerError

at besmart_park.PostoAuto.<init>(PostoAuto.java:27)

at besmart_park.Start.main(Start.java:56)

... 5 more

Caused by: java.lang.NullPointerException

at com.pi4j.io.gpio.impl.GpioControllerImpl.provisionPin(GpioControllerImpl.java:491)

at com.pi4j.io.gpio.impl.GpioControllerImpl.provisionDigitalInputPin(GpioControllerImpl.java:597)

at com.pi4j.io.gpio.impl.GpioControllerImpl.provisionDigitalInputPin(GpioControllerImpl.java:607)

at besmart_park.Sensor.<clinit>(Sensor.java:28)

... 7 more



PostoAuto.java line 27 is 

Sensor sensore = new Sensor(RaspiPin.getPinByName(pinDelSensore));


Sensor..java 28 is 

final static GpioPinDigitalInput mySensor = gpio.provisionDigitalInputPin(pin);



What I'm doing wrong?

Thanks
Reply all
Reply to author
Forward
0 new messages