NullPointerException when casting custom object from array in getGroupView
Can anyone help me figure out why I'm getting a NullPointerException here? I'm trying to set up an adapter for an ExpandableListViewActivity.
In the code below, I know my arrays are getting populated with objects, but when I attempt to retrieve the objects from the array in the adapter's getGroupView() method, I get a nullpointerexception.
The funny thing is that I can get the name property of the object in the code line just before the error occurs.
public class Section extends ExpandableListActivity {
bpsection[] sectionsArray;
bpcategory[][] categoriesArray;
ExpandableListAdapter sectionsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_section);
LoadCategoriesTask loadingtask = new LoadCategoriesTask();
loadingtask.execute();
}
private class bpsection {
public int id;
public String name;
public int isadult;
public bpsection(int _id, String _name, int _isadult) {
this.id = _id;
this.name = _name;
this.isadult = _isadult;
}
}
private class bpcategory extends bpsection {
@SuppressWarnings("unused")
public int section;
public bpcategory(int _section, int _id, String _name, int _isadult) {
super(_id, _name, _isadult);
this.section = _section;
}
}
/*
* CUSTOM ADAPTER
*/
public class BPSectionsAdapter extends BaseExpandableListAdapter {
private LayoutInflater blowMeUp;
public BPSectionsAdapter(Context context)
{
blowMeUp = LayoutInflater.from(context);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return categoriesArray[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return categoriesArray[groupPosition][childPosition].id;
}
@Override
public int getChildrenCount(int groupPosition) {
return categoriesArray[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return sectionsArray[groupPosition];
}
@Override
public int getGroupCount() {
return sectionsArray.length;
}
@Override
public long getGroupId(int groupPosition) {
return sectionsArray[groupPosition].id;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View parentView = blowMeUp.inflate(R.layout.itm_section, null);
TextView tv = (TextView) parentView.findViewById(R.id.sectionView);
Log.v("BPC", "Getting Group View for Section " + ((bpsection)getGroup(groupPosition)).name);
if(tv == null)Log.v("BPC", "Text view isnull");
if(((bpsection)getGroup(groupPosition)) == null)Log.v("BPC", "section is null");
tv.setText(((bpsection)getGroup(groupPosition)).name);
return parentView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View childView = convertView;
if (childView == null) {
childView = blowMeUp.inflate(R.layout.itm_category, null);
}
TextView tv = (TextView) childView.findViewById(R.id.categoryView);
tv.setText(((bpcategory) getChild(groupPosition, childPosition)).name);
return childView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
/*
* ASYNC TASK
*/
private class LoadCategoriesTask extends AsyncTask<Void, Void, Void> {
private BPCDB db;
@Override
protected void onPostExecute(Void result) {
sectionsAdapter = new BPSectionsAdapter(Section.this);
setListAdapter(sectionsAdapter);
}
@Override
protected Void doInBackground(Void... params) {
ArrayList<bpcategory> categories;
this.db = new BPCDB(getApplicationContext());
db.open();
Cursor c_section = db.getSections();
if(c_section.getCount() > 1)
try {
db.dbhelper.copyDBFromAssets();
c_section = db.getSections();
} catch (IOException e) {
e.printStackTrace();
}
sectionsArray = new bpsection[c_section.getCount()]; // We can init
// now that
开发者_如何学编程 // we have a
// count
categoriesArray = new bpcategory[c_section.getCount()][];
int i = 0;
for (c_section.moveToFirst(); c_section.moveToNext(); c_section
.isAfterLast()) {
bpsection s = new bpsection(c_section.getInt(0), // id
c_section.getString(1), // name
c_section.getInt(2) // isadult
);
sectionsArray[i] = s;
categories = new ArrayList<bpcategory>(); // Reinitialize
// category list for
// each section
// iteration
Cursor c_category = db.getCategories(Integer.toString(s.id));
// The first category of each section will be the "all whatever"
// of the section
categories.add(new bpcategory(s.id, 0, "all " + s.name,
s.isadult));
for (c_category.moveToFirst(); c_category.moveToNext(); c_category
.isAfterLast()) {
bpcategory c = new bpcategory(c_category.getInt(1),
c_category.getInt(0), c_category.getString(2),
c_category.getInt(3));
categories.add(c);
} // end of categories loop
int size = categories.size();
Log.v("BPC", "Section: " + s.name + "Categories: "+ size);
categoriesArray[i] = categories.toArray(new bpcategory[size]);
} // end of section loop
Log.v("BPC", "Sections: " + sectionsArray.length);
Log.v("BPC", "Categories: " + categoriesArray.length);
return null;
}
}
}
Here is the LogCat
07-13 06:27:57.719: VERBOSE/BPC(210): Getting Group View for Section services
07-13 06:27:57.719: VERBOSE/BPC(210): section is null
07-13 06:27:57.729: DEBUG/AndroidRuntime(210): Shutting down VM
07-13 06:27:57.729: WARN/dalvikvm(210): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
07-13 06:27:57.729: ERROR/AndroidRuntime(210): Uncaught handler: thread main exiting due to uncaught exception
07-13 06:27:57.749: ERROR/AndroidRuntime(210): java.lang.NullPointerException
07-13 06:27:57.749: ERROR/AndroidRuntime(210): at com.mogulsoftware.android.bpcruiser.Section$BPSectionsAdapter.getGroupView(Section.java:117)
07-13 06:27:57.749: ERROR/AndroidRuntime(210): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)
07-13 06:27:57.749: ERROR/AndroidRuntime(210): at android.widget.AbsListView.obtainView(AbsListView.java:1273)
07-13 06:27:57.749: ERROR/AndroidRuntime(210): at android.widget.ListView.makeAndAddView(ListView.java:1658)
07-13 06:27:57.749: ERROR/AndroidRuntime(210): at android.widget.ListView.fillDown(ListView.java:637)
The line that prompts the error (117) is:
tv.setText(((bpsection)getGroup(groupPosition)).name);
Any help is appreciated.
Please have a look at your loops when your are filling the section arrays. Looks like i
does not get incremented. Maybe explain the statements in for(...)
a litte more precisely
int i = 0;
for (c_section.moveToFirst(); c_section.moveToNext(); c_section
.isAfterLast()) {
bpsection s = new bpsection(c_section.getInt(0), // id
c_section.getString(1), // name
c_section.getInt(2) // isadult
);
sectionsArray[i] = s;
...
}
Recap your log:
07-13 06:27:57.719: VERBOSE/BPC(210): section is null
Coded in line 115:
if(((bpsection)getGroup(groupPosition)) == null)Log.v("BPC", "section is null");
Then line 117 raises the NullPointerException because
getGroup(groupPosition)
is null
an the property name
cannot be accessed
精彩评论