我正在使用公共类将2条信息保存到数组中(因此我可以轻松获取名称和ID)。但是由于某种原因,它似乎并不喜欢它!

我在线路上收到以下错误:-

AssetDetails.ID = f.getString(f.getColumnIndex("AssetObsID"));

Null pointer access: The variable details can only be null at this location


我的代码有错误是:

public static List<clsNameID> assetHelperTypes(){
    Log.e("Asset Helper Types:", "Started");
    clsNameID AssetDetails = null;
    List<clsNameID> mHelperNames = new ArrayList<clsNameID>();
    File dbfile = new File(Global.currentDBfull);

    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
    Cursor f = db.rawQuery("select * from assetobservationtypes", null);
    Log.e("Asset Helper Types:", "Cursor run");
        if(f.getCount() != 0) {
         f.moveToFirst();
            while(!f.isAfterLast()) {
                Log.e("Asset Helper Types:", "Finding Items");

                AssetDetails.ID = f.getString(f.getColumnIndex("AssetObsID"));
                AssetDetails.Name = f.getString(f.getColumnIndex("Observation"));

                mHelperNames.add(AssetDetails);
                Log.e("Asset Helper Types:", "Added Items");
            }
        }
    f.close();

    return mHelperNames;
}


类clsNameID:-

package com.directenquiries.assessment.tool;

public class clsNameID {
    public String Name;
    public String ID;

}


我试图用它来称呼它:

 public void addCondition(View view){


        List<clsNameID> mHelperNames = DBFunctions.assetHelperTypes();


        final List<Integer> mSelectedItems = new ArrayList<Integer>();

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("My Title")
                .setMultiChoiceItems(mHelperNames.toArray(new CharSequence[mHelperNames.size()]), null,
                        new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which, boolean isChecked) {
                                if (isChecked) {

                                    mSelectedItems.add(which);
                                } else if (mSelectedItems.contains(which)) {

                                    mSelectedItems.remove(Integer
                                            .valueOf(which));
                                }
                            }
                        })

               .setPositiveButton("Save", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {
                        //Create onlcick method
                   }
               })
               .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {
                        //Create onlcick method
                   }
               });
        builder.show();

}

最佳答案

您不是通过调用clsNameID运算符来创建new的对象的,这就是您的对象AssetDetails为空的原因。我刚刚添加了该行以创建对象。.用我的注释“添加此行。”来检出该行。

public static List<clsNameID> assetHelperTypes(){
    Log.e("Asset Helper Types:", "Started");
    clsNameID AssetDetails = null;
    List<clsNameID> mHelperNames = new ArrayList<clsNameID>();
    File dbfile = new File(Global.currentDBfull);

    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
    Cursor f = db.rawQuery("select * from assetobservationtypes", null);
    Log.e("Asset Helper Types:", "Cursor run");
        if(f.getCount() != 0) {
         f.moveToFirst();
            while(!f.isAfterLast()) {
                          //add this line....
                          AssetDetails     = new clsNameID();
                Log.e("Asset Helper Types:", "Finding Items");

                AssetDetails.ID = f.getString(f.getColumnIndex("AssetObsID"));
                AssetDetails.Name = f.getString(f.getColumnIndex("Observation"));

                mHelperNames.add(AssetDetails);
                Log.e("Asset Helper Types:", "Added Items");
            }
        }
    f.close();

    return mHelperNames;
}


希望能帮助到你...

07-27 21:00