这是我的代码:
try {
String route = mItems.get(position);
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
String tripString = mapper.writeValueAsString(tempTrip);
Intent i = new Intent(activity, PSRouteDetailsActivity.class);
i.putExtra("route", route);
i.putExtra("trip", tripString);
i.putExtra("address", address);
if (null != completedTripDepartureTime) {
i.putExtra("completedTripDepartureTime", completedTripDepartureTime);
} else if (mustChooseDepartureTime) {
i.putExtra("mustChooseDepartureTime", true);
}
if (checkout) {
i.putExtra(PSRouteDetailsActivity.SHOULD_CHECKOUT_EXTRA_KEY, true);
}
activity.startActivity(i);
} catch (Exception e) {
Utils.appendLog("pressed on details Container" + e.getMessage(), "E", Constants.PLANNER);
}
tempTrip是一个PSTrip
对象。我创建空,然后在里面添加所需的数据。
崩溃是这样的:
com.fasterxml.jackson.databind.JsonMappingException: Attempt to invoke virtual method 'boolean java.lang.String.contentEquals(java.lang.CharSequence)' on a null object reference (through reference chain: nl.hgrams.passenger.model.trip.PSTrip["active"])
当我尝试从对象创建String时,会发生这种情况:
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
String tripString = mapper.writeValueAsString(tempTrip);
为什么会这样?我已经设置了不包含NULL对象
这是我的PSTrip对象的外观(没有方法和getter设置器)
@RealmClass
public class PSTrip extends RealmObject implements Serializable {
private static String TAG = PSTrip.class.getName();
private int update_interval;
private String travel_mode;
@PrimaryKey
private String id;
private String icon;
private String status;
private String notes;
private Destination destination;
private Destination origin;
private Long departure_time, arrival_time;
private Route route;
private Owner_data owner;
private String vehicle;
private Flight flight;
private double last_updated;
private MileageExpenses mileage_expenses;
private RealmList<TripStep> steps;
private RealmList<TripStop> pendingTripStops = new RealmList<>();
private RealmList<Tag> tags = new RealmList<>();
private RealmList<RealmInt> usedVehicles = new RealmList<>();
private Planned_Route planned_route;
private Group group;
private Stats stats;
private String icon_small;
private Integer revised;
private double avgspeed;
private float delay;
private boolean deleted = false;
public final static String STATUS_ACTIVE = "active";
public final static String STATUS_COMPLETED = "completed";
public static boolean isCheckingOut = false, isAddingPitstop = false, isUpdatingLocation = false, isAborting = false;
public static Float kPSMinRoundTripLength = 250f, kPSInterPitStopDistance = 150f, kPSMinDistanceCheckout = 100f;
public static Long kPSLongTimeForCheckout = 900l, kPSShortTimeForCheckout = 300l;
public static final Double kPSminTripLength = 100d;
public static final int kPSFrequentCheckoutPlace = 5;
public static double minDistanceToDestination = 999999999;
}