上的Null指针异常

上的Null指针异常

所以我有以下课程,代码如下:

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
b。品牌
C。类别
d。描述
e。发布

上面的函数在列表中返回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");

最佳答案

您的db未初始化

DatabaseHandler db;


在您的onCreate

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

关于android - List <Class>上的Null指针异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18935391/

10-11 19:58