user4794598
user4794598

Reputation:

Calling a function in main class from different class in java

I am trying to call

public synchronized void refreshGPIOPinState()

in package com.rpi.rpi_gpio_controller; (this is main)

from package com.rpi.GPIOController; with

mApplication.refreshGPIOPinState();

getting a error java.lang.NullPointerException with about line

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);
}
// ***************************************

 }

When the Interrupt happens to call refreshGPIOPinState to change state of a image.

Upvotes: 0

Views: 105

Answers (1)

Amila
Amila

Reputation: 5213

You have declared the field:

private Rpi_gpio_controllerApplicationUI mApplication;

But you have not assigned anything to it. So the reference mApplication doesn't point to any Object, hence the NullPointerException.

You need to create/obtain an instance of Rpi_gpio_controllerApplicationUI and assign it to mApplication (In constructor, for example).

Upvotes: 0

Related Questions