如何解决java.lang.NullPointerException?

时间:2013-11-30 08:38:11

标签: android mysql json

我制作一个Android应用程序,用户可以从数据库mysql访问数据(我使用localhost phpmyadmin)并使用listview显示数据,我在教程中找到了这段代码。但是显示错误java.lang.NullPointerException。我不知道如何修复错误原因实际上我还是一个新手。这里是view_sektor.java:

package com.project;


 import java.util.ArrayList;

 import java.util.HashMap;


 import org.json.JSONArray;

 import org.json.JSONException;

 import org.json.JSONObject;

 import android.app.ListActivity;

 import android.content.Intent;

 import android.os.Bundle; 
 import android.view.View;

 import android.widget.AdapterView;

 import android.widget.AdapterView.OnItemClickListener;

 import android.widget.ListAdapter;

 import android.widget.ListView;

 import android.widget.SimpleAdapter;

 import android.widget.TextView;


 public class view_sektor extends ListActivity {


 private static String link_url = "http://10.0.2.2/android/check-sektor.php";


private static final String AR_ID = "id_sektor";

private static final String AR_NS = "nama_sektor";


JSONArray sektor = null;

ArrayList<HashMap<String, String>> daftar_sektor = new ArrayList<HashMap<String,   String>>();


@Override

      public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.view_sektor);



    JSONParser jParser = new JSONParser();


    JSONObject json = jParser.AmbilJson(link_url);


    try {

        sektor = json.getJSONArray("sektor");


        for(int i = 0; i < sektor.length(); i++){

            JSONObject ar = sektor.getJSONObject(i);


            String id_sektor = ar.getString(AR_ID);

            String nama_sektor = ar.getString(AR_NS).substring(0,100)+"...(baca selengkapnya)";


            HashMap<String, String> map = new HashMap<String, String>();


            map.put(AR_ID, id_sektor);

            map.put(AR_NS, nama_sektor);

            daftar_sektor.add(map);

        }
    } catch (JSONException e) {

        e.printStackTrace();

    }
    this.adapter_listview();

}

public void adapter_listview() {


    ListAdapter adapter = new SimpleAdapter(this, daftar_sektor,

            R.layout.list_sektor,

            new String[] { AR_ID, AR_NS}, new int[] {

                    R.id.id_sektor, R.id.nama_sektor});


    setListAdapter(adapter);

    ListView lv = getListView();


}
 }

这里整个logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project/com.project.komoditas}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project/com.project.view_sektor}: java.lang.NullPointerException
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
 at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project/com.project.view_sektor}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:2023)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
at android.widget.TabHost.setCurrentTab(TabHost.java:413)
at android.widget.TabHost.addTab(TabHost.java:240)
at com.project.komoditas.onCreate(komoditas.java:26)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
  Caused by: java.lang.NullPointerException
at com.project.view_sektor.onCreate(view_sektor.java:70)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

3 个答案:

答案 0 :(得分:0)

检查你的数据是否是json数组和sektor = json.getJSONArray(“sektor”); sektor ia是否可用

答案 1 :(得分:0)

试试这个..你必须启动 JSONObject

JSONObject json = new JSONObject();  

json = jParser.AmbilJson(link_url);

编辑1:

JSONParser jParser = new JSONParser();

JSONObject json = new JSONObject();  

json = jParser.AmbilJson(link_url);

Log.v("json",""+json.toString());

答案 2 :(得分:0)

试试这种方式 初始化列表视图

set Adapter方法调用list view并在apdater方法中创建一个类似listmaeth的新类,如listmaeth(this,values)

class listmaeth extends BaseAdapter{
//display what are the values you got
}