函数类函数没有返回控制器类调用函数

时间:2016-03-05 08:54:47

标签: java spring-mvc model modelmapper

我从控制器类调用一个函数。函数在另一个类函数中定义。但在执行返回代码之前,它会给出错误。 (作为调试)它从行 modelMap.put(" list",arraylist)跳转; 它给出异常,nullpointerexception。

controller.java

@RequestMapping(value = "/index.htm")
    public String getData() throws ClassNotFoundException {
        function obj = new function();
        ModelMap modelMap = null;
        obj.getdata(modelMap);
        return "index";
    }

function.java

public ModelMap getdata(ModelMap modelMap) throws ClassNotFoundException {
        SqlRowSet srs = null;
        ArrayList arraylist = new ArrayList();
        SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource = new connection.connection().getConnection(dataSource);
        JdbcTemplate get = new JdbcTemplate(dataSource);
        //Operation
        String sql = "SELECT * FROM user_info";
        srs = get.queryForRowSet(sql);
        while (srs.next()) {
            pojo obj = new pojo();
            obj.setEmail_id(srs.getString("email_id"));
            obj.setPassword(srs.getString("pwd"));
            arraylist.add(obj);
        }

        modelMap.put("list", arraylist);
        return modelMap;
    }

1 个答案:

答案 0 :(得分:0)

modelMapnull,这就是您获得NullPointerException的原因。你需要初始化它

ModelMap modelMap = new ModelMap();
obj.getdata(modelMap);