我想在我的代碼中使用getJSON和parseJSON,並getJSON正常工作,但解析沒有。我得到異常:getJSON的作品,但parseJSON doesnt
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);
}
這裏是ac lass 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
我想在這裏你得到一些異常extractJSON(),通過把日誌或東西檢查 – Rahul