С помощью приведенного ниже кода я попытался проанализировать текст JSON с URL, используя Volly.но вместо того, чтобы давать результат, он дает исключение
2019-03-21 09:47:29.138 8603-8603/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
2019-03-21 09:57:20.919 8603-8603/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
2019-03-21 09:57:21.522 8603-8603/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
2019-03-21 09:57:25.578 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.widget.ImageView.getDrawable()' on a null object reference
2019-03-21 09:57:32.832 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
2019-03-21 09:57:32.879 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.widget.ImageView.getDrawable()' on a null object reference
2019-03-21 09:58:09.997 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
2019-03-21 09:58:10.084 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.widget.ImageView.getDrawable()' on a null object reference
2019-03-21 09:58:12.130 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
2019-03-21 09:58:12.156 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.widget.ImageView.getDrawable()' on a null object reference
2019-03-21 09:58:13.473 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
2019-03-21 09:58:13.508 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.widget.ImageView.getDrawable()' on a null object reference
2019-03-21 09:58:16.968 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
2019-03-21 09:58:16.993 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.widget.ImageView.getDrawable()' on a null object reference
2019-03-21 09:58:33.128 8603-8603/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
2019-03-21 10:02:29.501 8603-8603/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
ниже - структура json.
{"ip":"111.125.204.140","type":"ipv4","continent_code":"AS","continent_name":"Asia"}
сверху текст json, я хочу получить значение типа.ниже мой код.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://api.ipstack.com/111.125.204.140?access_key=##########";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject json = null;
try {
json = new JSONObject(response);
} catch (JSONException e) {
e.printStackTrace();
}
String cityName = json.optString("type");// here iam not getting any value.
txtJson.setText(cityName);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
txtJson.setText("That didn't work!");
}
});
queue.add(stringRequest);