4

I am trying to envelop my data(Longitude, Latititude, route,timeStamp) in JSON format with the help of GSON library to be sent to the server after 60 seconds. At the moment I am getting the following error java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference. I am getting these variables(pLong,pLat, route, formatted) displayed in the xml file.

I appreciate any help.

Error:

04-21 23:15:02.893: E/AndroidRuntime(685): FATAL EXCEPTION: main
04-21 23:15:02.893: E/AndroidRuntime(685): Process: com.bustracker, PID: 685
04-21 23:15:02.893: E/AndroidRuntime(685): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS flg=0x4000010 } in com.bustracker.MainActivity$BroadcastReceiverListener@2d9405c7
04-21 23:15:02.893: E/AndroidRuntime(685):  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:933)
04-21 23:15:02.893: E/AndroidRuntime(685):  at android.os.Handler.handleCallback(Handler.java:739)
04-21 23:15:02.893: E/AndroidRuntime(685):  at android.os.Handler.dispatchMessage(Handler.java:95)
04-21 23:15:02.893: E/AndroidRuntime(685):  at android.os.Looper.loop(Looper.java:145)
04-21 23:15:02.893: E/AndroidRuntime(685):  at android.app.ActivityThread.main(ActivityThread.java:5944)
04-21 23:15:02.893: E/AndroidRuntime(685):  at java.lang.reflect.Method.invoke(Native Method)
04-21 23:15:02.893: E/AndroidRuntime(685):  at java.lang.reflect.Method.invoke(Method.java:372)
04-21 23:15:02.893: E/AndroidRuntime(685):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
04-21 23:15:02.893: E/AndroidRuntime(685):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
04-21 23:15:02.893: E/AndroidRuntime(685): Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
04-21 23:15:02.893: E/AndroidRuntime(685):  at com.bustracker.MainActivity$BroadcastReceiverListener.parse_SSID(MainActivity.java:218)
04-21 23:15:02.893: E/AndroidRuntime(685):  at com.bustracker.MainActivity$BroadcastReceiverListener.onReceive(MainActivity.java:172)
04-21 23:15:02.893: E/AndroidRuntime(685):  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:923)
04-21 23:15:02.893: E/AndroidRuntime(685):  ... 8 more

Data class:

public class Data {
    double longitude;
    double latitude;
    //int speed;
    String time;
    int route;

    public Data(double longitude, double latitude, String time,
            int route) {
        super();
        this.longitude = longitude;
        latitude = latitude;
        //this.speed = speed;
        this.time = time;
        this.route = route;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        latitude = latitude;
    }

//  public int getSpeed() {
//      return speed;
//  }
//
//  public void setSpeed(int speed) {
//      this.speed = speed;
//  }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public int getRoute() {
        return route;
    }

    public void setRoute(int route) {
        this.route = route;
    }

}

DataSerializer class:

import java.lang.reflect.Type;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class DataSerializer implements JsonSerializer<Data> {

    @Override
    public JsonElement serialize(Data data, Type arg1,
            JsonSerializationContext arg2) {
         JsonObject result = new JsonObject();
         result.add("longitude", new JsonPrimitive(data.getLongitude()));
         result.add("latitude", new JsonPrimitive(data.getLatitude()));
        // result.add("speed", new JsonPrimitive(data.getSpeed()));
         result.add("formatted", new JsonPrimitive(data.getTime()));
         result.add("route", new JsonPrimitive(data.getRoute()));
        return result;
    }

}

This part is in the MainActivity:

public class MainActivity extends ActionBarActivity {
    double pLong;
    double pLat;
    String formatted;
    int route_number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Data d = new Data(pLong, pLat, formatted,route_number);
        Gson gson = new GsonBuilder().registerTypeAdapter(Data.class, new DataSerializer()).create();
        System.out.println(gson.toJson(d));
  }
}
2
  • Could you add a command to line 79 (at the onCreate method)? Commented Apr 21, 2015 at 21:04
  • @moffeltje: that is this line"System.out.println(gson.toJson(d));" in the onCreate method. Commented Apr 21, 2015 at 21:07

2 Answers 2

3

Your time variable is null. You should initialize it in case if you put it in JsonObject in your DataSerializer

formatted = "13.10.2015";
Data d = new Data(pLong, pLat, formatted,route_number);
Sign up to request clarification or add additional context in comments.

1 Comment

The problem was I forgot to turn the GPS on since the time is being created in the onLocationChanged method location.getTime() but I am getting now the following error: "Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference" I have changed the trace error.
0

You create the following data object:

Data d = new Data(pLong, pLat, formatted,route_number);

Are you sure that the variable formatted is not null?

Because the exception appears after you add

result.add("formatted", new JsonPrimitive(data.getTime()));

that means your data.getTime() returns a null value and puts these null value into the json object which throws a nullpointer exception.

So either check the value before adding the value or initialize the value correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.