データベースから取得したjsonデータをlistviewで印刷して、appユーザーにとって判読可能な形式にしたいと考えました。しかし、次のエラーが得られる:json listview throwing errorのコード
public class DisplayListView extends AppCompatActivity {
String json_string;
JSONObject jsonObject;
JSONArray jsonArray;
dataAdapter dataAdapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.displaylistview_layout);
listView=(ListView) findViewById(R.id.listview);
listView.setAdapter(dataAdapter);
dataAdapter=new dataAdapter(this,R.layout.row_layout);
json_string=getIntent().getExtras().getString("json_data");
Log.w("var32", json_string);
try {
jsonArray = new JSONArray(json_string);
jsonArray=jsonObject.getJSONArray("_id");
int count=0;
String LabLocation,RackLocation,ShelfLocation,fourBid,Cluster,fourBookingName,SoftwareVersion,HardwareType,AssetNo,SerialNo;
while (count<jsonObject.length()){
JSONObject JO=jsonArray.getJSONObject(count);
LabLocation=JO.getString("LabLocation");
RackLocation=JO.getString("RackLocation");
ShelfLocation=JO.getString("ShelfLocation");
fourBid=JO.getString("fourBid");
Cluster=JO.getString("Cluster");
fourBookingName=JO.getString("fourBookingName");
SoftwareVersion=JO.getString("SoftwareVersion");
HardwareType=JO.getString("HardwareType");
AssetNo=JO.getString("AssetNo");
SerialNo=JO.getString("SerialNo");
data data=new data(LabLocation,RackLocation,ShelfLocation,fourBid,Cluster,fourBookingName,SoftwareVersion,HardwareType,AssetNo,SerialNo);
dataAdapter.add(data);
count++;
}
}catch (JSONException e){
Log.w("var33", "error JSON exception");
e.printStackTrace();
}
}
}
デバッグに助けてください:
07-20 18:22:09.541 29095-29095/com.example.manv.nokialabs I/Timeline: Timeline: Activity_launch_request id:com.example.manv.nokialabs time:36794948
--------- beginning of system
07-20 18:22:09.591 29095-29095/com.example.manv.nokialabs W/var32: [{ "_id" : { "$oid" : "5784818bcb30b4918964b50f"} , "LabLocation" : "LSD-SRN-BLR-U02" , "RackLocation" : "SRN-Radio-B01" , "ShelfLocation" : "SH-01" , "fourBid" : "908762" , "Cluster" : "Radio" , "fourBookingName" : "BTS01-Coral-SRN-BLR" , "SoftwareVersion" : "LN6.0" , "HardwareType" : "FRGP" , "AssetNo" : "40824873" , "SerialNO" : "RY114208612" , "Location" : "zzccg" , "Uname" : "addtvI"}]
07-20 18:22:09.591 29095-29095/com.example.manv.nokialabs D/AndroidRuntime: Shutting down VM
--------- beginning of crash
07-20 18:22:09.592 29095-29095/com.example.manv.nokialabs E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.manv.nokialabs, PID: 29095
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.manv.nokialabs/com.example.albbaby.nokialabs.DisplayListView}: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONArray org.json.JSONObject.getJSONArray(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONArray org.json.JSONObject.getJSONArray(java.lang.String)' on a null object reference
at com.example.albbaby.nokialabs.DisplayListView.onCreate(DisplayListView.java:32)
at android.app.Activity.performCreate(Activity.java:6285)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) )
は、ここに私のコードです。それがあるべき
jsonArray = new JSONArray(json_string);
: おかげ
に動作しますが、あなたが戻って取得しているエラーがorg.json.JSONArray org.json.JSONObject.getJSONArray」コールするトリングれます(java.lang.String) "を参照してください。コードを再確認し、すべてのオブジェクトが初期化されていることを確認してください。 –