o19830015 2013-01-10 02:53 采纳率: 12.5%
浏览 2097

在列表中保存对象出现exception

创建了名为Placeslist的类,想对其列表,在里面保存对象,但是报出异常:**Android.java.lang.NullPointerException**

placeslist class:

public class Placeslist {

     @Key
     public String status;

     @Key
        public String id;
        @Key
        public String name;
        @Key
        public String reference;

        @Key
        public double lat;

        @Key
        public double lon;

     public Placeslist(String status,String id,String name,String reference,double lat,double lon) {
        // TODO Auto-generated constructor stub
            this.status=status;
            this.id=id;
            this.name=name;
            this.reference=reference;
            this.lat=lat;
            this.lon=lon;

    }

在列表中保存对象:

Placeslist placeslist=null;

            String data = EntityUtils.toString(response.getEntity());
            JSONObject jsonObj = new JSONObject(data);
            List<Placeslist>places=null;
            JSONArray results = jsonObj.getJSONArray("results");
            for (int i = 0; i < results.length(); i++) {
                JSONObject result = results.getJSONObject(i);

                String name = result.getString("name");
                String id = result.getString("id");
                String reference = result.getString("reference");
                JSONObject latitudes = result.getJSONObject("geometry")
                        .getJSONObject("location");
                double lon = latitudes.getDouble("lng");
                double lat = latitudes.getDouble("lat");
                placeslist=new Placeslist("OK", id, name, reference, lat, lon);
                places.add(placeslist);

            }
  • 写回答

1条回答

  • ce_la_vie 2013-01-10 05:10
    关注

    你忘了

    places = new ArrayList<PlacesList>();
    

    或者

    places = new LinkedList<PlacesList>();
    

    List是一个接口interface ,不能直接用来创建对象,可以换成子类实现。

    评论

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功