Android:使用属性时出现Null Pointer异常

时间:2012-11-22 11:12:41

标签: java android class nullpointerexception

我正在使用公共类将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();

}

2 个答案:

答案 0 :(得分:1)

您没有通过调用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;
}

希望它有所帮助...

答案 1 :(得分:0)

首先,Java中的类名应以大写字母开头,因此您的自定义类应该被称为ClsNameID。此外,变量应以小写字母开头,因此您调用AssetDetails的引用应为assetDetailsIDName类似。

完成这些更改之后,当您访问正在类的实例上执行的IDName字段时,而不是{{1该类的属性。比较两段代码:

static

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

更容易看到编写第二组代码时出错,因为ClsNameID assetDetails = null; ... assetDetails.id = f.getString(f.getColumnIndex("AssetObsID")); assetDetails.name = f.getString(f.getColumnIndex("Observation")); 显然应该是一个实例,而第一组代码是模糊和混乱的。

要解决您的问题,您应该像其他海报一样说明并在尝试使用它之前创建一个类的实例:

assetDetails