简体   繁体   English

Android:使用属性时出现空指针异常

[英]Android: Null Pointer exception when using property

I'm using a public class to save 2 pieces of information to an array (so I can easily get the name and the ID). 我正在使用公共类将2条信息保存到数组中(因此我可以轻松获取名称和ID)。 However for some reason it doesn't seem to like it! 但是由于某种原因,它似乎并不喜欢它!

I'm getting the following error on the line :- 我在网上遇到以下错误:-

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

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

My code that is having the error is: 我的代码有错误是:

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;
}

Class clsNameID :- 类clsNameID:-

package com.directenquiries.assessment.tool;

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

}

I'm trying to call it with: 我试图用它来称呼它:

 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();

}

You are not creating the object of the clsNameID by calling the new operator that is why your object AssetDetails was null. 您不会通过调用new运算符来创建clsNameID的对象,这就是您的对象AssetDetails为null的原因。 I just added the line to create the object.. check out the line with my comment "add this line..". 我刚刚添加了该行以创建对象。.签出带有我的注释的行“添加此行..”。

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;
}

Hope it helps... 希望能帮助到你...

Firstly, class names in Java should start with a capital letter, so your custom class should be called ClsNameID . 首先,Java中的类名称应以大写字母开头,因此您的自定义类应称为ClsNameID Also, variables should start with a lowercase letter, so the reference you have called AssetDetails should be assetDetails , and similarly for ID and Name . 另外,变量应以小写字母开头,因此您称为AssetDetails的引用应为assetDetails ,并且对于IDName也是类似的。

Once you have made these changes then it is more obvious that when you are accessing the ID and Name fields that it is being performed on an instance of the class, rather than on static properties of the class. 进行了这些更改之后,很明显,当您访问IDName字段时,它是在类的实例上执行的,而不是在类的static属性上执行的。 Compare the two pieces of code: 比较两段代码:

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

and

ClsNameID assetDetails = null;
...
assetDetails.id = f.getString(f.getColumnIndex("AssetObsID"));
assetDetails.name = f.getString(f.getColumnIndex("Observation"));

It is much easier to see that an error has been made writing the second set of code, as assetDetails is clearly supposed to be an instance, whereas the first set of code is ambiguous and confusing. 很容易看到编写第二组代码时assetDetails ,因为assetDetails显然应该是实例,而第一组代码却模棱两可且令人困惑。

To resolve your issue, you should do as the other posters state and create an instance of your class before you attempt to use it: 要解决您的问题,应按照其他发布者的说明进行操作,并在尝试使用它之前创建类的实例:

ClsNameID assetDetails = new ClsNameID();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM