我有一个问题,我有一个可序列化的类,可以设置和获取多个列表。我在其中添加了一个列表,但是当我想从getter方法获取该列表时,它返回NullPointerException。这就是为什么我在这里提到错误和Setter和Getter方法来解释问题以及我如何检索它。我不知道为什么并且不知道它将如何被删除?任何人都可以帮我解决这个问题吗?
错误堆栈:
09-24 13:04:18.923: ERROR/AndroidRuntime(529): FATAL EXCEPTION: main
09-24 13:04:18.923: ERROR/AndroidRuntime(529): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.equinix.android.common/com.equinix.android.showmyorders.ShowMyOrders}: java.lang.NullPointerException
09-24 13:04:18.923: ERROR/AndroidRuntime(529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-24 13:04:18.923: ERROR/AndroidRuntime(529): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-24 13:04:18.923: ERROR/AndroidRuntime(529): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-24 13:04:18.923: ERROR/AndroidRuntime(529): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-24 13:04:18.923: ERROR/AndroidRuntime(529): at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 13:04:18.923: ERROR/AndroidRuntime(529): at android.os.Looper.loop(Looper.java:123)
09-24 13:04:18.923: ERROR/AndroidRuntime(529): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-24 13:04:18.923: ERROR/AndroidRuntime(529): at java.lang.reflect.Method.invokeNative(Native Method)
09-24 13:04:18.923: ERROR/AndroidRuntime(529): at java.lang.reflect.Method.invoke(Method.java:521)
09-24 13:04:18.923: ERROR/AndroidRuntime(529): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-24 13:04:18.923: ERROR/AndroidRuntime(529): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-24 13:04:18.923: ERROR/AndroidRuntime(529): at dalvik.system.NativeStart.main(Native Method)
09-24 13:04:18.923: ERROR/AndroidRuntime(529): Caused by: java.lang.NullPointerException
09-24 13:04:18.923: ERROR/AndroidRuntime(529): at com.equinix.android.showmyorders.ShowMyOrders.onCreate(ShowMyOrders.java:87)
09-24 13:04:18.923: ERROR/AndroidRuntime(529): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-24 13:04:18.923: ERROR/AndroidRuntime(529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-24 13:04:18.923: ERROR/AndroidRuntime(529): ... 11 more
设置和获取列表的类:
package com.equinix.android.model;
import java.io.Serializable;
import java.util.List;
public class BaseIBXValues implements Serializable{
private static final long serialVersionUID = 7526472295622776147L;
private List<String> IBXId;
private List<String> IBXName;
private List<String> IBXNaturalKey;
public List<String> getIBXId()
{
return IBXId;
}
public void setIBXId(List<String> IBXId)
{
this.IBXId = IBXId;
}
public List<String> getIBXName()
{
return IBXName;
}
public void setIBXName(List<String> IBXName)
{
this.IBXName = IBXName;
}
public List<String> getIBXNaturalKey()
{
return IBXNaturalKey;
}
public void setIBXNaturalKey(List<String> IBXNaturalKey)
{
this.IBXNaturalKey = IBXNaturalKey;
}
}
**Code for Setting the value of arraylist:**
public class Parse_Json {
String json_String;
List<String> IBXId;
BaseIBXValues ibx;------> It is the class where setter and getter mentioned
public Parse_Json(String json_String)
{
this.json_String = json_String;
}
public void parse()
{
ibx = new BaseIBXValues();
try{
JSONObject ja = new JSONObject(json_String);
JSONArray jo = ja.getJSONArray("ibx");
for(int i=0;i<jo.length();i++)
{
JSONObject j_data = jo.getJSONObject(i);
System.out.println("The id is:"+j_data.getString("id"));
IBXId.add(j_data.getString("id"));
LoginScreen.id.add(j_data.getString("id"));
System.out.println("The Name is:"+j_data.getString("name"));
LoginScreen.name.add(j_data.getString("name"));
System.out.println("The naturalKey is:"+j_data.getString("naturalKey"));
LoginScreen.naturalKey.add(j_data.getString("naturalKey"));
}
}catch(Exception e)
{
e.printStackTrace();
}
ibx.setIBXId(IBXId);------------> set the list
}
从Getter获取列表:
ibx = new BaseIBXValues();
ibId = ibx.getIBXId();
for(int i=0;i<6;i++)
{
System.out.println("The IBX ID:"+ibId.get(i));
}
答案 0 :(得分:0)
很少有事情需要注意:
在Parse_Json
课程中:
List<String> IBXId;
IBXID
永远不会被提升,因此它是null
。
稍后当它尝试将值添加到IBXId
时:
IBXId.add(j_data.getString("id"));
这将抛出NullPointerException。
但如果您的JSONArray jo = ja.getJSONArray("ibx");
返回jo
大小0
,则该行
ibx.setIBXId(IBXId);
会将IBXId
设为null
在以下代码中:
ibId = ibx.getIBXId();
for(int i=0;i<6;i++)
{
System.out.println("The IBX ID:"+ibId.get(i));
}
ibId
为null
,因为在BaseIBXValues
类中,该行
private List<String> IBXId;
没有初始化IBXId
,所以它的值是null
所以调用ibId.get(i)
会导致NullPointerException
修改强>
请参阅此处修改的代码:
public class Parse_Json {
String json_String;
List<String> IBXId = new ArrayList<String>();
BaseIBXValues ibx;------> It is the class where setter and getter mentioned
但是当你打电话给吸气剂时
ibId = ibx.getIBXId();
for(int i=0;i<6;i++)
{
System.out.println("The IBX ID:"+ibId.get(i));
}
,您可以确保您具有非空值
ibId = ibx.getIBXId();
If(ibId != null) {
for(String index : ibId)
{
System.out.println("The IBX ID:"+index);
}
}
<强>被修改强>
List<String> ibId=Collections.emptyList();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.showmyorders);
ibx = new BaseIBXValues();
ibId = ibx.getIBXId();
if(ibId != null) {
for(String index : ibId)
{
System.out.println("The IBX ID:"+index);
}
}
修改强> 这应该是:
public class BaseIBXValues implements Serializable{
private static final long serialVersionUID = 7526472295622776147L;
private List<String> IBXId = new ArrayList<String>(); // initialize to empty array of string
private List<String> IBXName;
创建新的BaseIBXValues
对象时,
ibx = new BaseIBXValues();
ibId = ibx.getIBXId();
ibId
应该是一个空字符串数组