Java 空数组
Java empty array
目前我正在尝试使用 Volley 库(使用 http 请求)从 mysql 数据库接收信息。但是当我想在我的 android 应用程序中显示信息时,我收到了很多错误。
10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: FATAL EXCEPTION: main
10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: Process: com.example.jamie.fysio, PID: 11718
10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: java.lang.NullPointerException: Attempt to read from null array
10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at com.example.jamie.fysio.Home.showJSON(Home.java:79)
10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at com.example.jamie.fysio.Home.access[=13=]0(Home.java:28)
10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at com.example.jamie.fysio.Home.onResponse(Home.java:55)
10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at com.example.jamie.fysio.Home.onResponse(Home.java:52)
10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6872)
10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
这是我解析 JSON:
的方法
public class parseJSON {
public static String[] adres;
public static final String JSON_ARRAY = "result";
public static final String ADRES = "adres";
private JSONArray users = null;
private String json;
public parseJSON(String json){
this.json = json;
}
protected void parseJSONMeth(){
JSONObject jsonObject=null;
try {
jsonObject = new JSONObject(json);
users = jsonObject.getJSONArray(JSON_ARRAY);
adres = new String[users.length()];
for(int i=0;i<users.length();i++){
JSONObject jo = users.getJSONObject(i);
adres[i] = jo.getString(ADRES);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
这是我的发送请求:
private void sendRequest(){
StringRequest stringRequest = new StringRequest(Request.Method.POST,JSON_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Home.this, error.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put(KEY_USERNAME, gebruikersnaam);
return map;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String json) {
parseJSON pj = new parseJSON(json);
pj.parseJSONMeth();
adres.setText(parseJSON.adres[0]);
}
}
我对 volley 库不是很熟悉我做错了什么?
您从 onResponse(String response)
获得的 response
似乎没有通过有效的 JSONObject
,这导致 JSONException
在 adres
之前被捕获已分配。
检查你的JSON_URL
。
顺便说一句 adres
不应该是静态的,应该通过 pj
.
访问
目前我正在尝试使用 Volley 库(使用 http 请求)从 mysql 数据库接收信息。但是当我想在我的 android 应用程序中显示信息时,我收到了很多错误。
10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: FATAL EXCEPTION: main 10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: Process: com.example.jamie.fysio, PID: 11718 10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: java.lang.NullPointerException: Attempt to read from null array 10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at com.example.jamie.fysio.Home.showJSON(Home.java:79) 10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at com.example.jamie.fysio.Home.access[=13=]0(Home.java:28) 10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at com.example.jamie.fysio.Home.onResponse(Home.java:55) 10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at com.example.jamie.fysio.Home.onResponse(Home.java:52) 10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60) 10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30) 10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739) 10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95) 10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145) 10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6872) 10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372) 10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 10-07 20:51:27.205 11718-11718/com.example.jamie.fysio E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
这是我解析 JSON:
的方法public class parseJSON {
public static String[] adres;
public static final String JSON_ARRAY = "result";
public static final String ADRES = "adres";
private JSONArray users = null;
private String json;
public parseJSON(String json){
this.json = json;
}
protected void parseJSONMeth(){
JSONObject jsonObject=null;
try {
jsonObject = new JSONObject(json);
users = jsonObject.getJSONArray(JSON_ARRAY);
adres = new String[users.length()];
for(int i=0;i<users.length();i++){
JSONObject jo = users.getJSONObject(i);
adres[i] = jo.getString(ADRES);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
这是我的发送请求:
private void sendRequest(){
StringRequest stringRequest = new StringRequest(Request.Method.POST,JSON_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Home.this, error.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put(KEY_USERNAME, gebruikersnaam);
return map;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String json) {
parseJSON pj = new parseJSON(json);
pj.parseJSONMeth();
adres.setText(parseJSON.adres[0]);
}
}
我对 volley 库不是很熟悉我做错了什么?
您从 onResponse(String response)
获得的 response
似乎没有通过有效的 JSONObject
,这导致 JSONException
在 adres
之前被捕获已分配。
检查你的JSON_URL
。
顺便说一句 adres
不应该是静态的,应该通过 pj
.