请求处理失败;嵌套异常是 java.lang.NumberFormatException: null

我收到错误为 Request processing failed; nested exception is java.lang.NumberFormatException: null

这是我的代码:

 @Controller
 @RequestMapping("/customerAdmin")
 public class CustomerAdmincontroller {
        @InitBinder
        public void initBinder(WebDataBinder binder) {
            binder.registerCustomEditor(Customer.class, "customer", new PropertyEditorSupport() {@
                Override
                public void setAsText(String customer) {
      setValue(new Customer(Integer.parseInt(customer)));
                }
            });
        }

        @RequestMapping(value = "/addProfile.htm", method = RequestMethod.GET)
        public ModelAndView addProfileForm(ModelMap model, HttpServletRequest request) {
            int id = Integer.parseInt(request.getParameter("id"));
            System.out.println(id);
            Profile profile = new Profile();
            Customer customer = new Customer(id);
            profile.setCustomer(customer);
            model.addAttribute("profile", profile);
            ModelAndView mav = new ModelAndView("addProfile");
            return mav;
        }
}

我收到错误

2014 年 10 月 16 日下午 4:31:47 调用

严重:servlet [dispatcherServlet] 在路径 [/CustomerPortal] 的上下文中的 Servlet.service() 引发异常 [请求处理失败;嵌套异常是 java.lang.NumberFormatException: null] 的根本原因

当我运行项目时,这个错误已经显示,我在这个 Here 中看到了这个错误,但对我来说仍然是错误。任何人都可以帮助我吗?我的控制器:

@Autowired
    addozv stdDAO;

    private ServletRequest request;
    @SuppressWarnings({ "null"})  
          @RequestMapping(value="/addadmins" , method = RequestMethod.POST)
           public void addadmins(ModelMap model, HttpServletRequest requ) {
               String username = requ.getParameter("username");
               String password = requ.getParameter("password");
               String nozv = requ.getParameter("nozv");
               String namefamily = requ.getParameter("namefamily");
               String codemely = requ.getParameter("codemely");
               String father = requ.getParameter("father");
               String adress = requ.getParameter("adress");
               String telephone = requ.getParameter("telephone");
               admins adm=new admins();
               adm.setUsername(username);
               adm.setPassword(password);
               adm.setNozv(Integer.parseInt(nozv));
               adm.setNamefamily(namefamily);
               adm.setCodemely(Integer.parseInt(codemely));
               adm.setFather(father);
               adm.setAdress(adress);
               adm.setTelephone(Integer.parseInt(telephone));  
               stdDAO.add(adm);

数据库:

@Override
    public void add(admins adm) {
        String sql = "INSERT INTO admins " +
                "username, password, nozv, namefamily, codemely, father, adress, telephone) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        Connection conn = null;
        try{
            conn = dataSource.getConnection();
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1, adm.getUsername());
            ps.setString(2, adm.getPassword());
            ps.setInt(3, adm.getNozv());
            ps.setString(4, adm.getNamefamily());
            ps.setInt(5, adm.getCodemely());
            ps.setString(6, adm.getFather());
            ps.setString(7, adm.getAdress());
            ps.setInt(8, adm.getTelephone());
            ps.executeUpdate();
            ps.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);

            } finally {
                if (conn != null) {
                    try {
                conn.close();
            } catch (SQLException e) {}
        }       
}

在 jsp 页面中,这是真的,我不知道为什么在这个页面中已经是错误。在此代码之前,我正在编写此代码:

@SuppressWarnings("null")
   @RequestMapping(value="/addozvv" , method = RequestMethod.GET)
   public String addozv(ModelMap model) {
      model.addAttribute("title", "ozv");

      return "addozvv";
   }

   @SuppressWarnings("null")
   @RequestMapping(value="/addozvv" , method = RequestMethod.POST)
   public void showPageSaveozv(ModelMap model, HttpServletRequest req) {
       String Id = req.getParameter("nozviat");
       String namefamily = req.getParameter("namefamily");
       String codemely = req.getParameter("codemely");
       String father = req.getParameter("father");
       String adress = req.getParameter("adress");
       String otelephone = req.getParameter("otelephone");
       String znamefamily = req.getParameter("znamefamily");
       String zcodemely = req.getParameter("zcodemely");
       String office = req.getParameter("office");
       String ztelephone = req.getParameter("ztelephone");

      /////////////
      ozviat ozv = new ozviat();

      ozv.setId(Integer.parseInt(Id));   
      ozv.setNamefamily(namefamily); 

    ozv.setCodemely(Integer.parseInt(codemely));  
      ozv.setFather(father);   
      ozv.setAdress(adress);  
      ozv.setOtelephone(Integer.parseInt(otelephone));   
      ozv.setZnamefamily(znamefamily);   
      ozv.setZcodemely(Integer.parseInt(zcodemely));   
      ozv.setOffice(office); 
      ozv.setZtelephone(Integer.parseInt(ztelephone)); 
      stdDAO.insert(ozv);

   }

但是当我运行项目时它是真的,但另一个是错误的

stack overflow Request processing failed; nested exception is java.lang.NumberFormatException: null
原文答案

答案:

作者头像

如我所见,您可能在没有 id 参数的情况下调用 /addProfile.htm 并且 parseInt 在这一行中抛出 NPE:

int id = Integer.parseInt(request.getParameter("id"));

相关问题