How can I solve NullPointerException using ModelAndView object?

457 Views Asked by At

I found what made problem for 'nullPointerException'(Request processing failed; nested exception is 'java.lang.NullPointerException') as I used object 'ModelAndView' in the controller below. Three kinds of variables can't get null below. I know variable 'viewName' in the controller can't get value and object Map also can bring clear value as well. Furthermore, the variable 'idto' of object 'ItemDTO' has null instead of any value.

How can I fix existing codes suitable for the variables to get right value?

Below is the codes relevant to what I explain.

The code is controller below.

 @Controller
    @RequestMapping("booksale")
    public class BookSaleController {
        @Inject
        private BookSaleService bService;
    
    @RequestMapping(value="/itemDetail/{ino}")
        public ModelAndView itemDetail(@PathVariable("ino")int ino, HttpServletRequest request, 
                                       HttpServletResponse response) {      
            
            String viewName = (String) request.getAttribute("viewName");    
            HttpSession session = request.getSession();     
            Map iMap = bService.itemDetail(ino);        
            ModelAndView mav = new ModelAndView(viewName);  
            mav.setViewName("/itemDetail/{ino}");       
            mav.addObject("iMap", iMap);        
            ItemDTO idto = (ItemDTO) iMap.get("idto");      
            appendItemQmenu(ino, idto, session);
             
            return mav;
            
            
        }
        
    
            
            
        private void appendItemQmenu(int ino, ItemDTO idto, HttpSession session) {
            // TODO Auto-generated method stub
            boolean existing_presence = false;
            List<ItemDTO> QuickItemList = 
            (List<ItemDTO>) session.getAttribute("QuickItemList");
            
            if (QuickItemList != null) {
                if (QuickItemList.size() < 3) {
                    for (int i = 0; i < QuickItemList.size(); i++) {
                        ItemDTO itemBean = QuickItemList.get(i);
                        String sIno = Integer.toString(ino);
                        
                        if (sIno.equals(itemBean.getIno())) {
                            existing_presence = true;
                            return;
                        }
                        
                    }
                    if (existing_presence== false) {
                        QuickItemList.add(idto);
                    }
                    
                }
            }else {
                QuickItemList = new ArrayList<ItemDTO>();
                QuickItemList.add(idto);
            }
            session.setAttribute("QuickItemList", QuickItemList);
            session.setAttribute("QuickItemListNum", QuickItemList.size());
        }

The second code is about servlet-context.xml which is linked to MyBatis below.

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    
    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->

<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
    <beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/> 
</beans:bean>
    <context:component-scan base-package="com.bookshop01" />
    
    <mvc:interceptors>
  <mvc:interceptor>
    <mvc:mapping path="/*/*.do"/> 
    <mvc:mapping path="/*/*/*.do"/> 
      <beans:bean class="com.bookshop01.common.interceptor.ViewNameInterceptor" />
  </mvc:interceptor>
 </mvc:interceptors>
 
    <!-- MultiPartResolver  -->
    <beans:bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
       <beans:property name="maxUploadSize" value="52428800" />
       <beans:property name="maxInMemorySize" value="52428800" />
       <beans:property name="defaultEncoding" value="utf-8" />
    </beans:bean>
    
 
</beans:beans>

Final code is about itemDetail.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>
<c:set var="items" value="${itemMap.iDTO}"/>
<c:set var="imageList" value="${itemMap.imageList}"/>
<%
    pageContext.setAttribute("crcn", "\n");
    pageContext.setAttribute("br", "<br/>");
%>        
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
  
<title>Insert title here</title>
<style>
#layer {
    z-index: 2;
    position: absolute;
    top: 0px;
    left: 0px;
    width: 100%;
}

#popup {
    z-index: 3;
    position: fixed;
    text-align: center;
    left: 50%;
    top: 45%;
    width: 300px;
    height: 200px;
    background-color: #ccffff;
    border: 3px solid #87cb42;
}

#close {
    z-index: 4;
    float: right;
}
.modal{
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0);
    transition: 200ms ease-in-out;
    border: 1px solid black;
    border-radius: 10px;
    z-index: 10;
    background-color: white;
    width: 500px;
    max-width: 80%; 
}

.modal .active{
    transform: translate(-50%, -50%) scale(1);
}
.modal-header{
    padding:  10px 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid black;
    
}
.modal-header .title{
    font-size: 1.25rem;
    font-weight: bold;
}
.modal-header .close-button{
    cursor: pointer;
    border: none;
    outline: none;
    background: none;
    font-size: 1.25rem;
    font-weight: bold;
}
.modal-body{
    padding: 10px 15px;
}
#overlay {
    position: fixed;
    opacity: 0;
    transition: 200ms ease-in-out;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0,0,0,.5);
    pointer-events: none;
}
#overlay .active{
    opacity: 1;
    pointer-events: all; 
}
</style>
<script type="text/javascript">
$(document).ready(function(){
    function add_cart(ino){
        $.ajax({
            type: "post",
            async: false,
            url: ${contextPath}/
            data: {
                ino:ino
                },
            success : function(data, textStatus){

                if(data.trim()=='add success'){
                        openModal(modal);
                }else if(data.trim()=='existing_presence'){
                        alert("Already choosen");
                    }
                
                },
                error : function(data, textStatus){
                    alert("Error occured"+data);
                } 
            });
        }

        var openModalButtons = document.querySelectorAll('[data-modal-target]');
        var closeModalButtons = document.querySelectorAll('[data-close-button]');
        var overlay = document.getElementById('overlay');

        openModalButtons.forEach(button =>){
            button.addEventListener('click', () =>){
                var modal = document.querySelector(button.dataset.modalTarget)
                openModal(modal);
            })
        })  
        
        overlay.addEventListener('click', () => {
            var modals = document.querySelectorAll('.modal.active')
            modals.forEach(modal => {
                closeModal(modal)
            })
        })

        closeModalButtons.forEach(button =>){
            button.addEventListener('click', () =>){
                var modal = button.closest('.modal')
                closeModal(modal);
            })
        })  
        
        
    function openModal(modal){
        if(modal == null) return
        modal.classList.add('active')
        overlay.classList.add('active') 
            
    }   

    function closeModal(modal){
        if(modal == null) return
        modal.classList.remove('active')
        overlay.classList.remove('active')  
            
    }
function fn_pickup_items(ino, ititle, price, filename){
    var LogOn = document.getElementById("LogOn");
    var isLogOn = LogOn.value;

    if(isLogOn == "false" || isLogOn == ''){
        alert("You can order if you sign in")
    }

    var total_price, final_total_price;
    var pcs = document.getElementById("pcs");

    var form = document.createElement("form");
    var i_ino = document.createElement("input");
    var i_ititle = document.createElement("input");
    var i_sales_price = document.createElement("input");
    var i_filename = document.createElement("input");
    var i_pcs = document.createElement("input");

    i_ino.name = "ino";
    i_ititle.name = "ititle";
    i_sales_price.name = "sales_price";
    i_filename.name = "filename";
    i_pcs.name = "pcs";

    i_ino.value = ino;
    i_ititle.value = ititle;
    i_sales_price.value = sales_price;
    i_filename.value = filename;
    i_pcs.value = pcs;

    form.appendChild(ino);
    form.appendChild(ititle);
    form.appendChild(sales_price);
    form.appendChild(filename);
    form.appendChild(pcs);

    document.body.appendChild(form);
    form.method="post";
    form.action="${contextPath}/pickup/pickupinsert.jsp";
    form.submit();   
}
        
}); 
</script>
</head>
<body>
    <%@ include file="../detailview/header.jsp" %>
    <%@ include file="../detailview/navbar.jsp" %>
    <div class="container">
    <div class="row">
        <div class="col-sm-4">
            <div class="bookimg">
                <img alt="" src="/resources/img/book.jpg">
            </div>
            <div class="stock">
                <label for="stock">Stock</label>                        
                <p>{dto.stock}</p>                          
            </div>  
        </div>
        <div class="col-sm-8">
            <div id="detail_table">
        <table>
            <tbody>
                <tr>
                    <td class="fixed">Net price</td>
                    <td class="active"><span>
                       <fmt:formatNumber  value="${dto.price}" type="number" var="price" />
                         ${price}
                    </span></td>
                </tr>
                <tr class="dot_line">
                    <td class="fixed">Selling price</td>
                    <td class="active"><span>
                       <fmt:formatNumber  value="${dto.price*0.9}" type="number" var="discounted_price"/>
                         ${discounted_price}(10% discount)</span></td>
                </tr>
                <tr>
                    <td class="fixed">Gathering Points</td>
                    <td class="active">${dto.point}P(10% mileage)</td>
                </tr>
                <tr class="dot_line">
                    <td class="fixed"> Additional mileage of points</td>
                    <td class="fixed">Getting 1,000 points if purcahsing more than 10,000 won, 
2,000 points if purchasing 50,000 won or additional mileage 300 points in using delivery service of convenient store</td>
                </tr>
                <tr>
                    <td class="fixed">published date</td>
                    <td class="fixed">
                       <c:set var="pub_date" value="${dto.publishDay}" />
                       <c:set var="arr" value="${fn:split(pub_date,' ')}" />
                       <c:out value="${arr[0]}" />
                    </td>
                </tr>
                <tr>
                    <td class="fixed">Total page</td>
                    <td class="fixed">${dto.totalPage}page</td>
                </tr>
                <tr>
                    <td class="fixed">Delivery fare</td>
                    <td class="fixed"><strong>Free of charge</strong></td>
                </tr>
                <tr>
                    <td class="fixed">Guide to deliver</td>
                    <td class="fixed"><strong>[One-day delivery]</strong> Start one-day delivery!<br> <strong>[Delivery for weekends]</strong>
                        Able to deliver for weekends</TD>
                </tr>
                <tr>
                    <td class="fixed">date to arrive</td>
                    <td class="fixed">Able to get next day if you order in the morning</td>
                </tr>
                <tr>
                    <td class="fixed">
                        <button id="pcs" type="button">Quantity</button>
                    </td>                            
                    </tr>
                </tbody>
            </table>
            <ul>
                <li><a class="buy" href="javascript:fn_pickup_items('${dto.ino}','${dto.ititle}','${dto.sales_price}','${dto.filename}');">Purchase </a></li>
                <li><a class="cart" href="javascript:add_cart('${dto.ino}')">Cart</a></li>          
            </ul>           
            </div>
        </div>
    </div>
</div>
        <div class="container">
        <ul class="nav nav-tabs">
            <li class="active"><a href="#t1">Classification on book</a></li>
            <li><a href="#t2">information on book</a></li>
            <li><a href="#t3">Table of contents & Contents</a></li>
            <li><a href="#t4">Information on writer</a></li>
            <li><a href="#t5">Introduction by writer</a></li>       
        </ul>
            <div class="tab_container">
                <div class="tab_content" id="t1">
                    <h4>Classification on book</h4>
                    <p>${fn:replace(dto.classify,crcn,br)}</p>
                    <c:forEach var="img" items="${imageList}">
                        <img src="${contextPath}/.?">
                    </c:forEach>
                </div>
            </div>
            <div class="tab_content" id="t2">
                <h4>Information on book</h4>
                <p>${fn:replace(dto.prolog,crcn,br)}</p>                
            </div>
            <div class="tab_content" id="t3">
                <h4>Table of contents & Contents</h4>
                <div class="Table_of_Content">Table of contents</div>   
                <p>${fn:replace(dto.TC,crcn,br)}</p>
                <div class="contents">Contents</div>
                <p>${fn:replace(dto.contents,crcn,br)}</p>              
            </div>
            <div class="tab_content" id="t4">
                <h4>Information on writer</h4>                  
                <p>${fn:replace(dto.iwriter,crcn,br)}</p>
                <p>${fn:replace(dto.iwri_pro,crcn,br)}</p>
            </div>
            <div class="tab_content" id="t5">
                <form name="form" method="post" action="#">
                <input type="hidden" name="membno" id="membno">
                <input type="hidden" name="bookcd" id="bookcd" value="100993608">
                <input type="hidden" name="device" value="p">
                <input type="hidden" name="actionType" id="rev_ins_type">
                <input type="hidden" name="review_no" id="rev_ins_no">
                <input type="hidden" name="valuation" id="rev_ins_rate">
                <div class="writeArea">
                    <h3 id="noView">You can be capable of leaving your feeling as reader if you sign in.</h3>       
                    <img src="#" alt="You can be capable of leaving your feeling as reader if you sign in.">
                        <dl>
                        <dt>Grades on book</dt>
                        <dd>
                        <input type="radio" value="1" name="radio_revRate" class="writeRadio">
                        <img src="/resources/img/star.png">
                        <input type="radio" value="2" name="radio_revRate" class="writeRadio">
                        <img src="/resources/img/star.png">
                        <img src="/resources/img/star.png">
                        <input type="radio" value="3" name="radio_revRate" class="writeRadio">
                        <img src="/resources/img/star.png">
                        <img src="/resources/img/star.png">
                        <img src="/resources/img/star.png">
                        <input type="radio" value="4" name="radio_revRate" class="writeRadio">
                        <img src="/resources/img/star.png">
                        <img src="/resources/img/star.png">
                        <img src="/resources/img/star.png">
                        <img src="/resources/img/star.png">
                        <input type="radio" value="4" name="radio_revRate" class="writeRadio">
                        <img src="/resources/img/star.png">
                        <img src="/resources/img/star.png">
                        <img src="/resources/img/star.png">
                        <img src="/resources/img/star.png">
                        <img src="/resources/img/star.png">
                        </dd>
                        <dt>Contents</dt>
                        <dd>
                        <textarea name="review_content" id="review_content" class="writeText"
                        style="height:47px;" onclick="review_popup('login');" 
                        onkeyup="fc_chk_byte(this, 4000);checkByteLength($(this).val(), 'review_content', 'tcount');"
                        hname="contents" required></textarea>
                        <a href="javascript:review popup('login');" 
                        class="mL10 reg-btn">Registration</a>
                        </dd>
                        <dd id="tcount">0/2000(Max 2000 letters)</dd>
                        </dl>                   
                    </div>
                </form>
            </div>                  
        </div>
        <div class="clear"></div>
        <button data-modal-target="#modal"></button>
        <div class="modal" id="modal">
            <div class="modal-header">
                <div class="title"></div>
                <button data-close-button class="close-button">&times;</button>
            </div>
            <div class="modal-body">
            
            </div>
        </div>
        <div class="active" id="overlay"></div>
    </body>
</html> 
1

There are 1 best solutions below

0
On

From your question I understand that the value returned from the map in the line is null.

ItemDTO idto = (ItemDTO) iMap.get("idto");

You are seeing this error because the Map does not contain an entry corresponding to the key 'idto'. Please check if the values is supposed to be present in the map, if the entry is to be present then why the entry is missing.

Other thing that you can do in the same line

ItemDTO idto = (ItemDTO) iMap.getOrDefault("idto", "DefaultValue");

This will try to find the entry corresponding to the key 'idto', if the entry is absent then it returns a default value 'DefaultValue'.

You can check this for more details https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#getOrDefault-java.lang.Object-V-