1

I am trying to send data from an Android Mobile App to Retrofit Rest API.

I took reference from this:

The Android APP https://github.com/MiraLak/AccelerometerAndroidApp

Retrofit Rest API (server, handles the data sent from the android app) https://github.com/MiraLak/accelerometer-rest-to-cassandra

the server then forwards the data to Cassandra.

i did some modification in the android app to send my custom data (battery info) instead of the accelerometer data as in the project i took as template.

Here is my SendAccelerationAsyncTask Class

 private class SendAccelerationAsyncTask extends AsyncTask<Collection, Void, Void> {

        protected void onPreExecute() {

        }

        @Override
        protected Void doInBackground(Collection... params) {
            try {
                SampleData training = new SampleData();
                training.setCollection(params[0]);
                cassandraRestApi.sendTrainingAccelerationValues(training);
            } catch (Exception e) {

                e.printStackTrace();
            }
            return null;
}

My problem is that when i try to send to data, the AsyncTask crashes with error:

04-13 08:55:43.120 4750-4818/com.miralak.basicaccelerometer W/System.err: java.lang.NullPointerException
04-13 08:55:43.121 4750-4818/com.miralak.basicaccelerometer W/System.err:     at com.miralak.basicaccelerometer.activity.CollectDataActivity$SendAccelerationAsyncTask.doInBackground(CollectDataActivity.java:459)
04-13 08:55:43.122 4750-4818/com.miralak.basicaccelerometer W/System.err:     at com.miralak.basicaccelerometer.activity.CollectDataActivity$SendAccelerationAsyncTask.doInBackground(CollectDataActivity.java:422)
04-13 08:55:43.122 4750-4818/com.miralak.basicaccelerometer W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-13 08:55:43.123 4750-4818/com.miralak.basicaccelerometer W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-13 08:55:43.123 4750-4818/com.miralak.basicaccelerometer W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-13 08:55:43.123 4750-4818/com.miralak.basicaccelerometer W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-13 08:55:43.123 4750-4818/com.miralak.basicaccelerometer W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-13 08:55:43.123 4750-4818/com.miralak.basicaccelerometer W/System.err:     at java.lang.Thread.run(Thread.java:838)

I checked and confirmed that the variable being send is not null but I am getting null pointer exception?

Thanks

update

NOTE: line 459 is cassandraRestApi.sendTrainingAccelerationValues(training);

i checked the values in training by:

Log.v("test", "" + training.getCollection().getStatus());
Log.v("test", "" + training.getCollection().getTechnology());
Log.v("test", "" + training.getCollection().getTemperature());

and so on..

here is the sendTrainingAccelerationValues method:

public interface CassandraRestApi {
    @POST("/test")
    Response sendTrainingAccelerationValues(@Body SampleData testing);
}
Keshav.Jo
  • 19
  • 2
  • Where is your `cassandraRestApi` being initialised? Coudl you post the code at line number 459. I realise it's already there but I can't see the line numbers – StuStirling Dec 09 '16 at 08:14
  • which line is 459 here ? – Vinodh Dec 09 '16 at 08:22
  • this is line 459: cassandraRestApi.sendTrainingAccelerationValues(training); i already check if `training` has values by: Log.v("test", "" + training.getCollection().getStatus()); Log.v("test", "" + training.getCollection().getTechnology()); and so on.. i can post the `sendTrainingAccelerationValues` method if needed! – Keshav.Jo Dec 09 '16 at 08:51
  • So `cassandraRestApi` is null. – Mike M. Dec 09 '16 at 08:53
  • @MikeM. i tested `cassandraRestApi` for null, and its not null! – Keshav.Jo Dec 12 '16 at 05:34
  • Then use the stack trace, log prints, debugger, etc., to figure out what is null. It's a basic programming skill that you need to learn sooner or later. We can't really debug your app for you, and the given info is not enough for a definite answer. – Mike M. Dec 12 '16 at 05:52

0 Answers0