Raspberry PI GPIO Java 代码错误

Raspberry PI GPIO Java Code Error

这是控制台日志

root@raspberrypi:/home/pi# java -jar /home/pi/keypad.jar
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.NullPointerException
        at me.Andrew.Keypad.GPIO.GPIOMethords.setupPins(GPIOMethords.java:33)
        at me.Andrew.Keypad.Listener.ButtonInput.ButtonListener(ButtonInput.java:45)
        at me.Andrew.Keypad.Listener.ButtonInput.start(ButtonInput.java:22)
        at me.Andrew.Keypad.Main.main(Main.java:15)
        ... 5 more
root@raspberrypi:/home/pi#
 unavailable)

这是代码

GPIO 方法

public class GPIOMethords {
public Main MA;
public Settings settings = new Settings();
final GpioController gpio = GpioFactory.getInstance();

public HashMap<String,GpioPinDigitalInput> ColPins = new HashMap<String,GpioPinDigitalInput>();
public HashMap<String,GpioPinDigitalOutput> RowPins = new HashMap<String,GpioPinDigitalOutput>();

public void setupPins(){
    if(ColPins.isEmpty()){
        ColPins.put("1",gpio.provisionDigitalInputPin(settings.Col1));
        ColPins.put("2",gpio.provisionDigitalInputPin(settings.Col2));
        ColPins.put("3",gpio.provisionDigitalInputPin(settings.Col3));
        ColPins.put("4",gpio.provisionDigitalInputPin(settings.Col4));
    }
    if(RowPins.isEmpty()){
        RowPins.put("1",gpio.provisionDigitalOutputPin(settings.Row1));
        RowPins.put("2",gpio.provisionDigitalOutputPin(settings.Row2));
        RowPins.put("3",gpio.provisionDigitalOutputPin(settings.Row3));
        RowPins.put("4",gpio.provisionDigitalOutputPin(settings.Row4));
    }
    MA.store.ColPins = ColPins; // line 33
    MA.store.RowPins = RowPins;
}


}

主要

public class Main {
    private static Main instance = null;

    public Settings set = new Settings();
    public static Methords Meths = new Methords();
    public GPIOMethords GPIOMeths = new GPIOMethords();
    public Storage store = new Storage();

    public static void main(String[] args) {
            Meths.buttonIn.start();

    }
    public static Main getInstance() {
          if(instance == null) {
             instance = new Main();
          }
          return instance;
       }

}

设置

public class Settings {
public String Code = "ABCD";

//Define Row Pins!
public Pin Row1 = RaspiPin.GPIO_00;
public Pin Row2 = RaspiPin.GPIO_01;
public Pin Row3 = RaspiPin.GPIO_02;
public Pin Row4 = RaspiPin.GPIO_03;

//Define Col Pins!
public Pin Col1 = RaspiPin.GPIO_04;
public Pin Col2 = RaspiPin.GPIO_05;
public Pin Col3 = RaspiPin.GPIO_06;
public Pin Col4 = RaspiPin.GPIO_07;

}

你真的不应该为你的 Main class 传递静态变量或使用单例,但你没有初始化它,所以它是空的。

改变

public class GPIOMethords {
    public Main MA;

public class GPIOMethords {
    public Main MA = Main.getInstance();