Android 从getGroupView中的数组强制转换自定义对象时出现NullPointerException

Android 从getGroupView中的数组强制转换自定义对象时出现NullPointerException,android,nullpointerexception,expandablelistview,Android,Nullpointerexception,Expandablelistview,有人能帮我弄清楚为什么这里会出现NullPointerException吗? 我正在尝试为ExpandableListViewActivity设置适配器 在下面的代码中,我知道数组中正在填充对象,但是当我尝试在适配器的getGroupView()方法中从数组中检索对象时,我得到了一个nullpointerexception 有趣的是,我可以在错误发生之前在代码行中获取对象的name属性 public class Section extends ExpandableListActivity {

有人能帮我弄清楚为什么这里会出现NullPointerException吗? 我正在尝试为ExpandableListViewActivity设置适配器

在下面的代码中,我知道数组中正在填充对象,但是当我尝试在适配器的getGroupView()方法中从数组中检索对象时,我得到了一个nullpointerexception

有趣的是,我可以在错误发生之前在代码行中获取对象的name属性

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

}

}
提示错误(117)的行是:

感谢您的帮助。

重述您的日志:

07-13 06:27:57.719: VERBOSE/BPC(210): section is null
第115行编码:

if(((bpsection)getGroup(groupPosition)) == null)Log.v("BPC", "section is null");
然后第117行引发NullPointerException,因为

getGroup(groupPosition)

null
a无法访问属性
name

请在填充节数组时查看循环。看起来
i
没有递增。也许可以更精确地解释一下(…)中的语句

    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;

     ...

     }

提示:这与演员阵容无关。它只是简单地执行
someNullExpr.someField
,它通常会爆炸。找出哪个接收器是调试的空部分。+1用于命名您的
LayoutInflater
“blowMeUp”。好的。。。再次查看代码,没有注意到上面的几行。我不知道,上面有几行。可能是后台的某个线程,正在访问/修改阵列?我没有意识到我遗漏了这一点!我一到家就去看看!循环就是问题所在。你是对的。我没有增加我的计数器。不过,当我在for循环而不是while循环中使用Cursor.isAfterLast时,还有一些工作要做。所以,我得到了一个空对象附加到我的数组的末尾。最后一项导致了NullPointerException。非常感谢你的帮助。
getGroup(groupPosition)
    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;

     ...

     }