0

i am calling a function from controller class. function is defined in another class function. but before executing return code it gives error. (as debugging) it jumps from the line modelMap.put("list", arraylist); it gives exception, 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;
    }
Suraj Roy
  • 51
  • 2
  • 12

1 Answers1

0

modelMap is null, that's why you get NullPointerException. You need to initialize it

ModelMap modelMap = new ModelMap();
obj.getdata(modelMap);
Guy
  • 46,488
  • 10
  • 44
  • 88
  • thankss.. its returning .. :) but new problem ,, after returning to controller obj is null. although modelMap has data. – Suraj Roy Mar 05 '16 at 09:19
  • @SurajRoy There is nothing in the code you posted that will make `obj` `null`. Further more, if `obj` was `null` you couldn't `obj.getdata(modelMap);` (I assume you mean `obj` from `function obj = new function();`). – Guy Mar 05 '16 at 09:28