java.lang.NullPointerException:尝试调用虚方法' java.lang.Class java.lang.Object.getClass()'在null对象引用上

时间:2015-04-21 21:00:21

标签: android json gson

我试图在GSSON库的帮助下以JSON格式封装我的数据(Longitude,Latititude,route,timeStamp),以便在60秒后发送到服务器。目前我收到以下错误java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference。我正在获取xml文件中显示的这些变量(pLong,pLat,route,formatted)。

我感谢任何帮助。

错误:

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

数据类:

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类:

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;
    }

}

这部分在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 个答案:

答案 0 :(得分:3)

您的time变量为空。如果您将JsonObject放入DataSerializer

,则应将其初始化
formatted = "13.10.2015";
Data d = new Data(pLong, pLat, formatted,route_number);

答案 1 :(得分:0)

您创建以下数据对象:

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

您确定变量formatted不为空吗?

因为添加

后会出现异常
result.add("formatted", new JsonPrimitive(data.getTime()));

这意味着您的data.getTime()返回一个空值并将这些值设置为null 值为json对象,抛出一个nullpointer异常。

因此,在添加值之前检查值或正确初始化值。