I am new to Android programming (just started for the sake of my Final Year Project) and I am having trouble with my current task on hand. I am trying to access my Sphero's IMU and output the data into a text file. Streaming the data onto my android device was not much of an issue after I learn how to create an android app and playing around with the Samples I got from https://github.com/orbotix/Sphero-Android-SDK.

However I had been stucked on the output data to text phase which I could not seems to solve. I manage to figure out how to output text file for string messages and arrays with DataOutputStream and other outputstream method(buffer, bytes etc). But when it comes to outputting data from the sphero accelerometer streaming, I could not output any file as the app keep crashing on error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList com.orbotix.async.DeviceSensorAsyncMessage.getAsyncData()' on a null object reference

Here are the output part which I believe is causing the problem:

public void write_data() {
    double accelX = ((DeviceSensorAsyncMessage) asyncMessage).getAsyncData().get(0).getAccelerometerData().getFilteredAcceleration().x;
    ArrayList arrayList = new ArrayList();
    while (running == true) {
        arrayList.add(accelX);

        Toast.makeText(getApplicationContext(), "File Open", Toast.LENGTH_LONG).show();
        int sz = arrayList.size();
        for (int i = 0; i < sz; i++) {
            try {
                DataOutputStream dos = new DataOutputStream(new FileOutputStream(output_file, true));
                dos.writeUTF("" + arrayList);
                dos.writeChars("\n");
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(), "Unable to copy data", Toast.LENGTH_LONG).show();
                //e.printStackTrace();
            }
        }
        try {
            dos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I am not sure as to the fact that the output method is wrong as I still do not understand the NullPointException Error from past issues. Can someone please advice. Thank you in advance and I would really appreciate any advice.

1

There are 1 best solutions below

0
Lyon Gan On

For those who happened to be seeking the same answer, I am not good at the explanation of coding as I am new but I managed to resolve the issues by restructuring actions.

Here is the new code that I wrote which finally work as I desired:

public void handleAsyncMessage(AsyncMessage asyncMessage, Robot robot) {
    if( asyncMessage == null )
        return;

    //Check the asyncMessage type to see if it is a DeviceSensor message
    if( asyncMessage instanceof DeviceSensorAsyncMessage ) {
        DeviceSensorAsyncMessage message = (DeviceSensorAsyncMessage) asyncMessage;

        if( message.getAsyncData() == null
                || message.getAsyncData().isEmpty()
                || message.getAsyncData().get( 0 ) == null )
            return;

        //Retrieve DeviceSensorsData from the async message
        DeviceSensorsData data = message.getAsyncData().get( 0 );

        //Extract the accelerometer data from the sensor data
        displayAccelerometer(data.getAccelerometerData());

        //Extract attitude data (yaw, roll, pitch) from the sensor data
        displayAttitude(data.getAttitudeData());

        //Extract gyroscope data from the sensor data
        displayGyroscope( data.getGyroData() );

        writeAccelerometer(data.getAccelerometerData());
    }
}


public void writeAccelerometer(AccelerometerData accelerometer) {
    if (running == true) {
        try {
            dos.writeBytes(String.format("%.4f", accelerometer.getFilteredAcceleration().x));
            dos.writeChars("\n");
            dos.writeBytes(String.format("%.4f", accelerometer.getFilteredAcceleration().y));
            dos.writeChars("\n");
            dos.writeBytes(String.format("%.4f", accelerometer.getFilteredAcceleration().z));
            dos.writeChars("\n");
            System.out.println("Write Successful");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

That is all I have to share and hope it helps.