在java中从不同的类调用主类中的函数

时间:2015-09-06 06:24:02

标签: java

我正打算致电

public synchronized void refreshGPIOPinState()
包com.rpi.rpi_gpio_controller中的

; (这是主要的)

来自com.rpi.GPIOController包; 与

mApplication.refreshGPIOPinState();

使用about line

获取java.lang.NullPointerException错误
package com.rpi.GPIOController;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.wiringpi.GpioInterrupt;
import com.pi4j.wiringpi.GpioInterruptEvent;
import com.pi4j.wiringpi.GpioInterruptListener;
import com.pi4j.wiringpi.Gpio;
import com.pi4j.wiringpi.GpioUtil;
import com.rpi.Utils.Utils;
import com.rpi.rpi_gpio_controller.Rpi_gpio_controllerApplicationUI;

    public enum GPIOController {

INSTANCE(true);
final GpioController gpioController = GpioFactory.getInstance();
private Rpi_gpio_controllerApplicationUI mApplication ;
// The used GPIO pins
public GpioPinDigitalOutput[] mGPIOPins = new GpioPinDigitalOutput[1];

// ***************************************
// Constuctor
private GPIOController(boolean setLowOnExit) {

    mGPIOPins[0] = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_00, "GPIO_Pin_" + 0, PinState.LOW);
    if (setLowOnExit) {
        // Set shutdown behavior for all pins
        mGPIOPins[0].setShutdownOptions(true, PinState.LOW);
    }
}
// ***************************************

// ***************************************
public void GetInput(int trigerpin, int outputpin) throws InterruptedException {
    Utils.Output_WriteDebug(true, " GPIO Trigger ... started on " + trigerpin + " OutPutPin " + outputpin);
    GpioInterrupt.addListener(new GpioInterruptListener() {
        @Override
        public void pinStateChange(GpioInterruptEvent event) {
            Utils.Output_WriteDebug(true, "Raspberry Pi PIN [" + trigerpin + "] is in STATE [" + event.getState() + "]");
            try {
                mGPIOPins[outputpin].toggle();
                mApplication.refreshGPIOPinState();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    // setup wiring pi
    if (Gpio.wiringPiSetup() == -1) {
        Utils.Output_WriteDebug(true, " ==>> GPIO SETUP FAILED");
    }

    // export all the GPIO pins that we will be using
    GpioUtil.export(trigerpin, GpioUtil.DIRECTION_IN);

    // set the edge state on the pins we will be listening for
    GpioUtil.setEdgeDetection(trigerpin, GpioUtil.EDGE_BOTH);

    // configure GPIO x as an INPUT pin; enable it for callbacks
    Gpio.pinMode(trigerpin, Gpio.INPUT);
    Gpio.pullUpDnControl(trigerpin, Gpio.PUD_DOWN);
    GpioInterrupt.enablePinStateChangeCallback(trigerpin);
}
// ***************************************

 }

当Interrupt碰巧调用refreshGPIOPinState来改变图像的状态时。

1 个答案:

答案 0 :(得分:1)

您已宣布该字段:

private Rpi_gpio_controllerApplicationUI mApplication;

但你还没有分配任何东西。因此引用mApplication不指向任何Object,因此NullPointerException。

您需要创建/获取Rpi_gpio_controllerApplicationUI的实例并将其分配给mApplication(例如,在构造函数中)。