doumao6048 2014-05-08 13:12
浏览 140
已采纳

使用JSONParser的java.lang.Nullpointerexception

I am using MySQL database, and I'm selecting my data from database using JSONParser.class I found on the internet, but the value always showing Null, please help, i'm new at this

Here's my main activity class:

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);
    }
}

And here's my php file:

<?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);
?>

here's my logcat error:

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条回答 默认 最新

  • douquanzhan0315 2014-05-08 13:25
    关注

    You are passing parameter from client app with name "kode" in line

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

    But you are accepting with name "kdanggota" in line

    $kd=$_POST['kdanggota'];
    

    Therefore server is sending empty array and you are not checking either array is empty or not. Please use same name of parameter while sending and receiving data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?