Java 截击方法之外的NullPointerException
当我试图在这段代码之后访问分类数组时,它会给我nullpointerexception。 我将把这个数组返回给其他类。所以它也会产生nullpointerexceptionJava 截击方法之外的NullPointerException,java,android,nullpointerexception,android-volley,Java,Android,Nullpointerexception,Android Volley,当我试图在这段代码之后访问分类数组时,它会给我nullpointerexception。 我将把这个数组返回给其他类。所以它也会产生nullpointerexception JsonObjectRequest req = new JsonObjectRequest(url, new JSONObject(), new Response.Listener<JSONObject>() { @Override
JsonObjectRequest req = new JsonObjectRequest(url, new JSONObject(),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// handle response
try {
JSONArray jarray=(JSONArray)response.get(item);
catagory=new String[jarray.length()][2];
//catagory_name=new String[jarray.length()];
for(int i=0;i<jarray.length();i++){
JSONObject jobj = (JSONObject) jarray
.get(i);
catagory[i][1]=jobj.getString(sectionurl);
catagory[i][0]=jobj.getString(name);
}
}
catch (JSONException je){
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// handle error
}
})
String str=catagory[0][0];//AT THIS LINE AN EXCEPTION IS THROWN
JsonObjectRequest req=new-JsonObjectRequest(url,new-JSONObject(),
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
//处理响应
试一试{
JSONArray jarray=(JSONArray)response.get(item);
catagory=新字符串[jarray.length()][2];
//catagory_name=新字符串[jarray.length()];
对于(inti=0;i,正如Selvin所说,您应该理解线程是如何工作的
第行String str=catagory[0][0];
catagory为空
只有在public void onResponse(JSONObject response){…}
之后,它才会有值,因为它是一个关联操作。Show stacktrace.Show line出现此异常的地方。我已经更新了问题。..Onlycatagory==null | | catagory[0]==null
可能导致此处出现NPE…请了解线程、回调和基本代码流…但我的分类参考变量(数组变量)是Globaland?您根本不了解线程,请检查我上一条注释中的代码段,并尝试了解它如何应用于您的代码。。。