Kim VamPa

스프링 에러 : IndexOutOfBoundsException 본문

개발노트/에러

스프링 에러 : IndexOutOfBoundsException

Kim VamPa 2020. 2. 11. 09:32
728x90
반응형

에러문구

예외org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:877) javax.servlet.http.HttpServlet.service(HttpServlet.java:660) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

근본 원인 (root cause)

java.lang.NullPointerException com.sjb.controller.PurchaseController.preBuy(PurchaseController.java:51) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) java.lang.reflect.Method.invoke(Unknown Source) org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:877) javax.servlet.http.HttpServlet.service(HttpServlet.java:660) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequ

 

 

 

문제가되는 문장

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    @RequestMapping(value="/preBuy", method=RequestMethod.POST)
 
    public void preBuy(String[] cartId, Model model, HttpSession session) throws Exception{
 
        System.out.println("preBuy 실행");
        
        MemberVO member = (MemberVO) session.getAttribute("member");
        String memberId = member.getMemberId();
        member = memberservice.importInfo(memberId);
        model.addAttribute("memberinfo",member);
        
        
        List<CartListVO> list = new ArrayList<CartListVO>(); 
        
        
        // 장바구니 목록중 선택한것 가져오기
        for(int i = 0; i<cartId.length ; i++) {
            int id = 0;
            CartListVO cartlist = new CartListVO();
            id = Integer.parseInt(cartId[i]);
            cartlist = cartservice.buyList(id);
            
            list.add(cartlist);
            
        }
        
        if!list.isEmpty() ) {
        model.addAttribute("buylist",list);
        System.out.println("list성공?>?" + list);
        System.out.println(list.get(2));
        }
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

문제가 된 문장은 30번 줄이다.

 

 

이유 : ArrayList 에서 get(index)를 통해 값을 가져오는 문장이다. 하지만 만약 해당 인덱스가 없는경우 해당 에러가 난다

 

해결방법 : 나의 경우는 데어터 잘들어오는지 테스트를하기위한 System.out 문장이라 해당문장만 주면 되었다. 하지만 get을 써야할경우 If문을 통해 Index가 존재할때만 해당 get문장이 실행되도록 하면 된다.

728x90
반응형
Comments