MissCoder87
MissCoder87

Reputation: 2669

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). 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 :-

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

}

Upvotes: 0

Views: 326

Answers (2)

dave.c
dave.c

Reputation: 10908

Firstly, class names in Java should start with a capital letter, so your custom class should be called 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.

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. 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.

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

Upvotes: 0

Praful Bhatnagar
Praful Bhatnagar

Reputation: 7435

You are not creating the object of the clsNameID by calling the new operator that is why your object AssetDetails was 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...

Upvotes: 1

Related Questions