我正在尝试在我的代码中使用getJSON和parseJSON,并且getJSON正常工作但解析不行。我得到例外:
java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONObject org.json.JSONArray.getJSONObject(int)' on a null object reference
在MainActivity中我有:
public void onCellLocationChanged(CellLocation lokacija) {
super.onCellLocationChanged(lokacija);
location = (GsmCellLocation) Phone.getCellLocation();
textCellId.setText(String.valueOf(location.getCid() % 65536));
textCellLac.setText(String.valueOf(location.getLac()));
String JSON_URL_string=JSON_URL + "?cellid=" + String.valueOf(location.getCid()%65636);
getJSON(JSON_URL_string);
ParseJSON();
}
getJSON返回如下内容:
{"result":[{"cellname":"BG_ACB1","celllat":"44.81125","celllon":"20.39933","celldir":"0","cellattr":"gsm900"},{"cellname":"SOMB5W1","celllat":"45.783055","celllon":"19.120833","celldir":"100","cellattr":"umts900"}]}
在看到getJSON后,我调用了ParseJSON:
private void ParseJSON() {
Intent intent = new Intent(MainActivity.this,ParseJSON.class);
intent.putExtra(MY_JSON,textCellNameSakriven.getText().toString());
startActivity(intent);
}
这是一个ParseJSON类。如果我运行没有ParseJSON的代码,它可以正常工作:
public class ParseJSON extends ActionBarActivity {
private String myJSONString;
private static final String JSON_ARRAY ="result";
private static final String CELLNAME = "cellname";
private JSONArray users = null;
private int TRACK = 0;
private TextView textCellName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
myJSONString = intent.getStringExtra(MainActivity.MY_JSON);
textCellName=(TextView) findViewById(R.id.textCellName);
extractJSON();
showData();
}
private void extractJSON(){
try {
JSONObject jsonObject = new JSONObject(myJSONString);
users = jsonObject.getJSONArray(JSON_ARRAY);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void showData(){
try {
JSONObject jsonObject = users.getJSONObject(TRACK);
textCellName.setText(jsonObject.getString(CELLNAME));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
最后,这是一个日志:
FATAL EXCEPTION: main
Process: com.zelenigrad.bsavkovic.myapplication, PID: 9862
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zelenigrad.bsavkovic.myapplication/com.zelenigrad.bsavkovic.myapplication.ParseJSON}: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONObject org.json.JSONArray.getJSONObject(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5272)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONObject org.json.JSONArray.getJSONObject(int)' on a null object reference
at com.zelenigrad.bsavkovic.myapplication.ParseJSON.showData(ParseJSON.java:59)
at com.zelenigrad.bsavkovic.myapplication.ParseJSON.onCreate(ParseJSON.java:43)
at android.app.Activity.performCreate(Activity.java:5977)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5272)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
感谢您的努力。
RGDS
答案 0 :(得分:0)
你不能使用这个
JSONObject jsonObject = users.getJSONObject(TRACK);
在你的响应中没有像这样的json对象,它给你NullPointerException。
,对象名称必须为String always
。您正在使用int值TRACK。