0

i use value object as the json which return to android client such that

public class StationVO implements Serializable{

    private static final long serialVersionUID = 1692896969987787366L;

    private String stationName;

    private Long stationID;

    private Double stationLng;

    private Double stationLat;  
}

but my partner let me change as

public class StationVO implements Serializable{

    private static final long serialVersionUID = 1692896969987787366L;

    private String stationName="";

    private long stationID;

    private double stationLng;

    private double stationLat;
    }

because he think if you use object,the json my be have null,it can cause client application error,even down,but i think if i use double the nullexception may be hidden,but i cant find the error. besides if i use double,the original null change 0,could cause the unreal data. so how can i avoid return null and i can find the nullexceptin or others to locate the exception?

Cœur
  • 37,241
  • 25
  • 195
  • 267
王奕然
  • 3,891
  • 6
  • 41
  • 62

1 Answers1

0

Your question is almost unintelligible, so I will restrict myself to some advice:

Don't declare "scalar" fields with wrapper types unless your application requires the distinct meaning provided by null. It is simpler and more efficient to use the primitive type for your scalars ... and you won't run into problems with NPEs.

In your example, you should only use Double for the longitude and latitude if you need to support stations with "unknown" or "unspecified" long/lat coordinates. If you simply want to have a default value, then initialize the fields to the (numeric) default value; e.g. 0.0, 0,0.

If you do need it to be possible for the long / lat fields to be null, then your code has to cope with this.

For detailed examples on debugging NPEs, read my Answer to this question:

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216