List <class> </class>上的空指针异常

时间:2013-09-21 17:18:42

标签: android listview android-sqlite

所以我有以下类,代码如下:

private List<Medicines> meds = new ArrayList<Medicines>();//for loading the list 
private List<Medicines> medDummy = new ArrayList<Medicines>();//for loading the list to a class
DatabaseHandler db;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.medicine_list);
    populateMedicines();// Null Pointer Exception
    populateListView();
}
private void populateMedicines(){
    meds = db.getOnQuery("post", "SY");// Null Pointer Exception
    for(Medicines medList: meds){
        medDummy.add(new Medicines(medList.getBrand(), medList.getDescription(), medList.getCategory()));
    }
}

public void populateListView(){
    ArrayAdapter<Medicines> adapter = new MyListAdapter();
    ListView list = (ListView)findViewById(R.id.medicineList);
    list.setAdapter(adapter);
}

public class MyListAdapter extends ArrayAdapter<Medicines>{

    public MyListAdapter(){
        super(TabsandCapsList.this, R.layout.med_layout, medDummy);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View itemView = convertView;
        if(itemView == null){
            itemView = getLayoutInflater().inflate(R.layout.med_layout, parent, false);
        }
        Medicines listMed = medDummy.get(position);
        //textview
        TextView header = (TextView)itemView.findViewById(R.id.list_header);
        header.setText(listMed.getBrand());
        TextView description = (TextView)itemView.findViewById(R.id.list_description);
        description.setText(listMed.getDescription());
        TextView category = (TextView)itemView.findViewById(R.id.list_category);
        category.setText(listMed.getCategory());
        return itemView;
    }


}

方法getOnQuery:

//Getting on query medical entries
    public List<Medicines> getOnQuery(String query, String value){
        List<Medicines> medList = new ArrayList<Medicines>();

        //Query
        String queryString = "SELECT * FROM "+TABLE_MEDICINE+" WHERE "+query+" = "+"'"+value+"'";
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor c = db.rawQuery(queryString, null);

        if(c.moveToFirst()){
            do{
                Medicines med = new Medicines();
                med.setID(Integer.parseInt(c.getString(0)));
                med.setBrand(c.getString(1));
                med.setDescription(c.getString(2));
                med.setCategory(c.getString(3));
                med.setPost(c.getString(4));
                medList.add(med);
            }while(c.moveToNext());
        }
        return medList;
    }

我正在尝试使用我创建的内部数据库填充ListView。该数据库包含以下列: 一个。 ID 湾品牌 C。类别 d。描述 即交

上面的函数返回List中的onQuery结果。这个函数适用于我正在使用的其他类。但是在上面显示的类中,下面给出了一个NPE:

09-21 23:01:25.474: E/AndroidRuntime(29321): FATAL EXCEPTION: main
09-21 23:01:25.474: E/AndroidRuntime(29321): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.geneticalabs/com.geneticalabs.TabsandCapsList}: java.lang.NullPointerException
09-21 23:01:25.474: E/AndroidRuntime(29321):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
09-21 23:01:25.474: E/AndroidRuntime(29321):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
09-21 23:01:25.474: E/AndroidRuntime(29321):    at android.app.ActivityThread.access$600(ActivityThread.java:149)
09-21 23:01:25.474: E/AndroidRuntime(29321):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
09-21 23:01:25.474: E/AndroidRuntime(29321):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-21 23:01:25.474: E/AndroidRuntime(29321):    at android.os.Looper.loop(Looper.java:153)
09-21 23:01:25.474: E/AndroidRuntime(29321):    at android.app.ActivityThread.main(ActivityThread.java:4987)
09-21 23:01:25.474: E/AndroidRuntime(29321):    at java.lang.reflect.Method.invokeNative(Native Method)
09-21 23:01:25.474: E/AndroidRuntime(29321):    at java.lang.reflect.Method.invoke(Method.java:511)
09-21 23:01:25.474: E/AndroidRuntime(29321):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
09-21 23:01:25.474: E/AndroidRuntime(29321):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
09-21 23:01:25.474: E/AndroidRuntime(29321):    at dalvik.system.NativeStart.main(Native Method)
09-21 23:01:25.474: E/AndroidRuntime(29321): Caused by: java.lang.NullPointerException
09-21 23:01:25.474: E/AndroidRuntime(29321):    at com.geneticalabs.TabsandCapsList.populateMedicines(TabsandCapsList.java:75)
09-21 23:01:25.474: E/AndroidRuntime(29321):    at com.geneticalabs.TabsandCapsList.onCreate(TabsandCapsList.java:37)
09-21 23:01:25.474: E/AndroidRuntime(29321):    at android.app.Activity.performCreate(Activity.java:5020)
09-21 23:01:25.474: E/AndroidRuntime(29321):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-21 23:01:25.474: E/AndroidRuntime(29321):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
09-21 23:01:25.474: E/AndroidRuntime(29321):    ... 11 more

第37行:

populateMedicines();

第75行:

meds = db.getOnQuery("post", "SY");

1 个答案:

答案 0 :(得分:2)

您的db未初始化

DatabaseHandler db;

onCreate

db = new DatabaseHandler(ActivityName.this); // initialize db