这个空指针真的很烦我——请帮忙!

发布于 2024-10-15 12:20:16 字数 2344 浏览 1 评论 0原文

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

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

非常感谢

Can anyone help me with the following null pointer exception issue please:

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

This is the ReesultsTable class(a dataview)

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

Thank you so much

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟沫凡尘 2024-10-22 12:20:16

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

resultsList = res;

In your ResultsTable constructor, you are always saving the member variable resultsList (initialized to null) to the res ArrayList parameter. Swap the sides of the first line, and it might be OK. In other words, you want the first line of the constructor to be

resultsList = res;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文