Android Force关闭JSON,NullPointerException

时间:2013-01-27 09:28:18

标签: android json nullpointerexception forceclose

当我尝试执行此代码时出现此错误。当我尝试访问此活动时,它会变为强制关闭。我正在使用这个类来解析我的数据库中的数据来显示它。仅供参考我只在我的数据库中显示关于个人数据的1行。

这是我的java文件

package com.bucheuw.gollege.portal;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.bucheuw.gollege.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class DataPribadiActivity extends Activity {

    public String nim;
    JSONArray str_login = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.panel_datapribadi);

        Bundle b = getIntent().getExtras();
        nim = b.getString("par_kode");

        String link_url = "http://10.0.2.2/gollege/datapribadi.php?kode="+nim;
        //String link_url = "http://10.0.2.2/siakad-andro/detail-kal.php?kode=1";
        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.AmbilJson(link_url);

        try {
            str_login = json.getJSONArray("dtpr");
            String datp = "";
            TextView isi = (TextView) findViewById(R.id.tdatapribadi);
            for(int i = 0; i < str_login.length(); i=0){
                JSONObject ar = str_login.getJSONObject(i);
                datp += ar.getString("nim")+"\n"+
                "Nama : "+ar.getString("nama")+"\n"+
                "Email : "+ar.getString("email")+"\n"+      
                "Kelas : "+ar.getString("kelas")+"\n"+
                "Prodi : "+ar.getString("prodi")+"\n"+
                "Alamat : "+ar.getString("alamat")+"\n"+
                "Telepon : "+ar.getString("telp")+"\n"+
                "Tempat Tanggal Lahir : "+ar.getString("ttl")+"\n\n";
            }
            isi.setText(datp);


        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

}

这是logcat

01-27 15:48:24.803: E/AndroidRuntime(881): FATAL EXCEPTION: main
01-27 15:48:24.803: E/AndroidRuntime(881): java.lang.RuntimeException: Unable to start           activity ComponentInfo{com.bucheuw.gollege/com.bucheuw.gollege.portal.DataPribadiActivity}:    java.lang.NullPointerException
01-27 15:48:24.803: E/AndroidRuntime(881):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-27 15:48:24.803: E/AndroidRuntime(881):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-27 15:48:24.803: E/AndroidRuntime(881):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-27 15:48:24.803: E/AndroidRuntime(881):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-27 15:48:24.803: E/AndroidRuntime(881):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 15:48:24.803: E/AndroidRuntime(881):  at android.os.Looper.loop(Looper.java:123) 
01-27 15:48:24.803: E/AndroidRuntime(881):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-27 15:48:24.803: E/AndroidRuntime(881):  at java.lang.reflect.Method.invokeNative(Native Method)
01-27 15:48:24.803: E/AndroidRuntime(881):  at java.lang.reflect.Method.invoke(Method.java:507)
01-27 15:48:24.803: E/AndroidRuntime(881):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-27 15:48:24.803: E/AndroidRuntime(881):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-27 15:48:24.803: E/AndroidRuntime(881):  at dalvik.system.NativeStart.main(Native Method)
01-27 15:48:24.803: E/AndroidRuntime(881): Caused by: java.lang.NullPointerException
01-27 15:48:24.803: E/AndroidRuntime(881):  at com.bucheuw.gollege.portal.DataPribadiActivity.onCreate(DataPribadiActivity.java:34) 
01-27 15:48:24.803: E/AndroidRuntime(881):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-27 15:48:24.803: E/AndroidRuntime(881):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-27 15:48:24.803: E/AndroidRuntime(881):  ... 11 more

任何人都可以解决我的错误吗? 谢谢你..

1 个答案:

答案 0 :(得分:0)

我认为这一行存在问题

Bundle b = getIntent().getExtras();
 nim = b.getString("par_kode");

您没有检查b null值,因此如果您不向此活动发送任何内容,则会获得NullPointerException