这个空指针真的很讨厌我 - 请帮忙!

时间:2011-02-04 01:44:24

标签: java wicket

任何人都可以帮我解决以下空指针异常问题:

AjaxButton viewResult = new AjaxButton("test"){
    protected void onSubmit(AjaxRequestTarget target,Form form){
        try {
            retrieveTestID();
            System.out.println(testID);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        results=new ArrayList();
        DB db=new DB();
        try{
            db.connect();
            String query = 
              "Select Stud_Username,result from students2tests where Test_ID=" 
              + testID;

            System.out.println(query);
            ResultSet rs=db.execSQL(query);
            while(rs.next()){
                Result result = new Result( rs.getString("Stud_Username"), 
                                            rs.getInt("result"));
                results.add(result);
            }
            if(results.isEmpty()){
                System.out.println("I'm empty, there is error");
            }
            else{
                //this is being printed so results isnt empty
                System.out.println("NOT EMPTY");
            }
            db.close();
            /***seems to be throwing an error because of this line***/
            ResultsTable table=new ResultsTable(results);
            setResponsePage(table);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

这是ReesultsTable类(数据视图)

public class ResultsTable extends BasePage {
    private ArrayList resultsList;

    public ResultsTable(ArrayList res){
        res=resultsList;
        if(resultsList.isEmpty()){
            System.out.println("I am empty,beware");
        }else{
            System.out.println("I am not emty-no worries!!!");  
        }

        final DataView dataView = new DataView("studresult", 
            new ListDataProvider(resultsList)) {
                public void populateItem(final Item item) {
                    final Result studres = (Result) item.getModelObject();
                    item.add(new Label("username", studres.getUsername()));
                    item.add(new Label("result", "" + studres.getResult()));
                }
            };

        dataView.setItemsPerPage(10);
        add(dataView);
        add(new PagingNavigator("navigator", dataView));
    }
}

非常感谢

1 个答案:

答案 0 :(得分:4)

在ResultsTable构造函数中,始终将成员变量resultsList(初始化为null)保存到res ArrayList参数。交换第一行的两侧,可能没问题。换句话说,您希望构造函数的第一行是

resultsList = res;