我正在开发一个应用程序,我从服务器获取数据,我使用getter和setter函数来设置这些.
这是我的代码......
JSONArray arr1 = new JSONArray(strServerResponse);
JSONObject jsonObj1 = arr.getJSONObject(0);
pojo = new Pojo();
empid = jsonObj1.optString("empid");
pojo.setId(empid);
Run Code Online (Sandbox Code Playgroud)
而我正在使用getter函数
Pojo pojo = new Pojo();
String id = pojo.getId();
Run Code Online (Sandbox Code Playgroud)
这是我的setter和getter函数
public class Pojo {
private String empid;
public void setId(String empid) {
this.empid = empid;
}
public String getId() {
return empid;
}
}
Run Code Online (Sandbox Code Playgroud)
我在使用getter函数的地方得到Null Pointer Exception.我做错了吗?谁能帮帮我吗.
如果您要从pojo一次创建一个对象,则不必另外创建一个对象,get因此删除Pojo pojo = new Pojo();并放置:
String id=pojo.getId();
Run Code Online (Sandbox Code Playgroud)
你的代码应该是这样的:
JSONArray arr1 = new JSONArray(strServerResponse);
JSONObject jsonObj1 = arr.getJSONObject(0);
pojo = new Pojo();
empid = jsonObj1.optString("empid");
pojo.setId(empid);
String id = pojo.getId();
Run Code Online (Sandbox Code Playgroud)
然后使用相同的对象你将拥有你的身份.
| 归档时间: |
|
| 查看次数: |
781 次 |
| 最近记录: |