我正在使用公共类将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();
}
答案 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
的引用应为assetDetails
,ID
和Name
类似。
完成这些更改之后,当您访问正在类的实例上执行的ID
和Name
字段时,而不是{{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