使用JSONParser的java.lang.Nullpointerexception

时间:2014-05-08 13:12:14

标签: java php android mysql json

我正在使用MySQL数据库,并且我使用在互联网上找到的JSONParser.class从数据库中选择我的数据,但是值总是显示Null,请帮忙,我是新来的

这是我的主要活动课程:

public class Profil extends Activity{
    private static String url = "http://10.0.2.2/koperasidb/tampil_profil.php";
    private static final String TAG_ANGGOTA = "anggota";
    private JSONParser jsonParser;
    private JSONObject json;
    private JSONArray jArray;

    String kodeanggota;
    private Session session;//global variable 
    private String no_ba,nm,jk,agama,tmptlahir,tgllahir,blnlahir,thnlahir,status,alamat,notelp,email;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        session = new Session(this); //onCreate Session class
        //mengambil string kdanggota
        kodeanggota = session.getkdanggota();

        jsonParser = new JSONParser();
        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("kode", kodeanggota));
        //profil = new ArrayList<HashMap<String, String>>();
        try {
            json = jsonParser.makeHttpRequest(url, "POST", postParameters);
            jArray = json.getJSONArray(TAG_ANGGOTA);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject job = jArray.getJSONObject(i);
                no_ba = job.getString("no_ba");
                nm = job.getString("nama");
                jk = job.getString("jk");
                agama = job.getString("agama");
                tmptlahir = job.getString("tmptlhr");
                tgllahir = job.getString("tgllhr");
                blnlahir = job.getString("blnlhr");
                thnlahir = job.getString("thnlhr");
                status = job.getString("status");
                alamat = job.getString("alamat");
                notelp = job.getString("notelp");
                email = job.getString("email"); 

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

    private void tampilkanprofil() {
        TextView profilnama = (TextView) findViewById(R.id.profilnamaanggota);
        profilnama.setText(nm);
        TextView profiljk = (TextView) findViewById(R.id.profiljk);
        profiljk.setText(jk);
        TextView profilnoba = (TextView) findViewById(R.id.profilkodeba);
        profilnoba.setText(no_ba);
        TextView profilagama = (TextView) findViewById(R.id.profilagama);
        profilagama.setText(agama);
        TextView profilttl = (TextView) findViewById(R.id.profilttl);
        profilttl.setText(tmptlahir + ", " + tgllahir + " " + blnlahir + " " + thnlahir);
        TextView profilstatus = (TextView) findViewById(R.id.profilstatus);
        profilstatus.setText(status);
        TextView profilalamat = (TextView) findViewById(R.id.profilalamat);
        profilalamat.setText(alamat);
        TextView profilnotelp = (TextView) findViewById(R.id.profilnotelp);
        profilnotelp.setText(notelp);
        TextView profilemail = (TextView) findViewById(R.id.profilemail);
        profilemail.setText(email);
    }
}

这是我的php文件:

<?php
include ("koneksi.php");

$response = array();
$kd=$_POST['kdanggota'];
$result = mysql_query("SELECT * FROM anggota_baru WHERE kd_anggota ='$kd' ") or die(mysql_error());

$response["anggota"] = array();
while($row = mysql_fetch_array($result)){
    $profil = array();
    $profil["no_ba"] = $row["no_ba"];
    $profil["nama"] = $row["nama_lengkap"];
    $profil["jk"] = $row["kelamin"];
    $profil["agama"] = $row["agama"];
    $profil["tmptlhr"] = $row["tempat_lahir"];
    $profil["ttgllhr"] = $row["tgl_lahir"];
    $profil["blnlhr"] = $row["bln_lahir"];
    $profil["thnlhr"] = $row["thn_lahir"];
    $profil["status"] = $row["status_nikah"];
    $profil["alamat"] = $row["alamat"];
    $profil["notelp"] = $row["no_telp"];
    $profil["email"] = $row["email"];

    array_push($response["anggota"],$profil);
}
echo json_encode($response);
?>

这是我的logcat错误:

05-08 20:20:26.246: WARN/System.err(871): org.json.JSONException: No value for no_ba
05-08 20:20:26.256: WARN/System.err(871):     at org.json.JSONObject.get(JSONObject.java:354)
05-08 20:20:26.256: WARN/System.err(871):     at org.json.JSONObject.getString(JSONObject.java:510)
05-08 20:20:26.256: WARN/System.err(871):     at com.randy.koperasidb.Profil.onCreate(Profil.java:55)
05-08 20:20:26.256: WARN/System.err(871):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-08 20:20:26.266: WARN/System.err(871):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
05-08 20:20:26.266: WARN/System.err(871):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
05-08 20:20:26.266: WARN/System.err(871):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-08 20:20:26.286: WARN/System.err(871):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
05-08 20:20:26.286: WARN/System.err(871):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 20:20:26.296: WARN/System.err(871):     at android.os.Looper.loop(Looper.java:123)
05-08 20:20:26.296: WARN/System.err(871):     at android.app.ActivityThread.main(ActivityThread.java:3647)
05-08 20:20:26.306: WARN/System.err(871):     at java.lang.reflect.Method.invokeNative(Native Method)
05-08 20:20:26.306: WARN/System.err(871):     at java.lang.reflect.Method.invoke(Method.java:507)
05-08 20:20:26.316: WARN/System.err(871):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-08 20:20:26.316: WARN/System.err(871):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-08 20:20:26.327: WARN/System.err(871):     at dalvik.system.NativeStart.main(Native Method)
05-08 20:20:26.346: DEBUG/AndroidRuntime(871): Shutting down VM
05-08 20:20:26.346: WARN/dalvikvm(871): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-08 20:20:26.366: ERROR/AndroidRuntime(871): FATAL EXCEPTION: main
05-08 20:20:26.366: ERROR/AndroidRuntime(871): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.randy.koperasidb/com.randy.koperasidb.Profil}: java.lang.NullPointerException
05-08 20:20:26.366: ERROR/AndroidRuntime(871):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
05-08 20:20:26.366: ERROR/AndroidRuntime(871):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
05-08 20:20:26.366: ERROR/AndroidRuntime(871):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-08 20:20:26.366: ERROR/AndroidRuntime(871):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
05-08 20:20:26.366: ERROR/AndroidRuntime(871):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 20:20:26.366: ERROR/AndroidRuntime(871):     at android.os.Looper.loop(Looper.java:123)
05-08 20:20:26.366: ERROR/AndroidRuntime(871):     at android.app.ActivityThread.main(ActivityThread.java:3647)
05-08 20:20:26.366: ERROR/AndroidRuntime(871):     at java.lang.reflect.Method.invokeNative(Native Method)
05-08 20:20:26.366: ERROR/AndroidRuntime(871):     at java.lang.reflect.Method.invoke(Method.java:507)
05-08 20:20:26.366: ERROR/AndroidRuntime(871):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-08 20:20:26.366: ERROR/AndroidRuntime(871):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-08 20:20:26.366: ERROR/AndroidRuntime(871):     at dalvik.system.NativeStart.main(Native Method)
05-08 20:20:26.366: ERROR/AndroidRuntime(871): Caused by: java.lang.NullPointerException
05-08 20:20:26.366: ERROR/AndroidRuntime(871):     at com.randy.koperasidb.Profil.tampilkanprofil(Profil.java:78)
05-08 20:20:26.366: ERROR/AndroidRuntime(871):     at com.randy.koperasidb.Profil.onCreate(Profil.java:72)
05-08 20:20:26.366: ERROR/AndroidRuntime(871):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-08 20:20:26.366: ERROR/AndroidRuntime(871):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
05-08 20:20:26.366: ERROR/AndroidRuntime(871):     ... 11 more
05-08 20:20:26.396: WARN/ActivityManager(61):   Force finishing activity com.randy.koperasidb/.Profil
05-08 20:20:26.416: WARN/ActivityManager(61):   Force finishing activity com.randy.koperasidb/.Home
05-08 20:20:26.927: WARN/ActivityManager(61): Activity pause timeout for HistoryRecord{40693400 com.randy.koperasidb/.Profil}
05-08 20:20:28.177: INFO/Process(871): Sending signal. PID: 871 SIG: 9
05-08 20:20:28.207: INFO/ActivityManager(61): Process com.randy.koperasidb (pid 871) has died.
05-08 20:20:28.217: INFO/WindowManager(61): WIN DEATH: Window{406a6770 com.randy.koperasidb/com.randy.koperasidb.login paused=false}
05-08 20:20:28.257: ERROR/InputDispatcher(61): channel '40774e40 com.randy.koperasidb/com.randy.koperasidb.Home (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
05-08 20:20:28.257: ERROR/InputDispatcher(61): channel '40774e40 com.randy.koperasidb/com.randy.koperasidb.Home (server)' ~ Channel is unrecoverably broken and will be disposed!
05-08 20:20:28.327: INFO/WindowManager(61): WIN DEATH: Window{40774e40 com.randy.koperasidb/com.randy.koperasidb.Home paused=true}
05-08 20:20:29.027: WARN/InputManagerService(61): Got RemoteException sending setActive(false) notification to pid 871 uid 10041

2 个答案:

答案 0 :(得分:1)

您正在从名为&#34; kode&#34;在行

postParameters.add(new BasicNameValuePair("kode", kodeanggota));

但你接受了名字&#34; kdanggota&#34;在行

$kd=$_POST['kdanggota'];

因此,服务器正在发送空数组,而您没有检查数组是否为空。发送和接收数据时请使用相同的参数名称。

答案 1 :(得分:1)

我可以看到一个没有logcat的错误,如下所示;

1 - 当您尝试在kode中找不到BasicNameValuePair中不存在的'kdanggota'时,您已在$_POST中分配了密钥`$kd=$_POST['kode'];` //You need to write kode here because that is what is the key for your pair. 。所以改变php的第四行如下

{{1}}

请发布logcat以获取更多信息