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

时间:2014-10-16 11:08:41

标签: java spring hibernate spring-mvc

我收到错误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调用

     

SEVERE:上下文中servlet [dispatcherServlet]的Servlet.service()    与路径[/ CustomerPortal]抛出异常[请求处理    失败;嵌套异常是java.lang.NumberFormatException:null]    有根本原因

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

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

Jdbc:

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


   }

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

1 个答案:

答案 0 :(得分:0)

正如我所看到的,你可能在没有id参数的情况下调用/addProfile.htm,而parseInt会在这一行中抛出NPE:

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