• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

showing "org.apache.jasper.JasperException: java.lang.NullPointerException"

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello frnds, i'm creating a online shopping site using jsp and servlets in Netbeans 7.4

I have page named as "homepage.jsp". In this page i got some problems. While running the project homepage.jsp page is working properly.
But running this page alone, i got the following error...

HTTP Status 500 - Internal Server Error

type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.NullPointerException
root cause

java.lang.NullPointerException
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.0 logs.

GlassFish Server Open Source Edition 4.0

Could anyone please help me on this one...???

homepage.jsp

<%@page import="java.util.ArrayList"%>
<%@ page import="classes.Item" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Shop Around</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/style.css" type="text/css" media="all" />
<!--[if lte IE 6]><link rel="stylesheet" href="css/ie6.css" type="text/css" media="all" /><![endif]-->
<script src="js/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="js/jquery.jcarousel.pack.js" type="text/javascript"></script>
<script src="js/jquery-func.js" type="text/javascript"></script>
</head>
<body>
<form action="requesthandle" method="post">
<!-- Shell -->
<div class="shell">
<!-- Header -->
<div id="header">
<h1 id="logo"><a href="#">Shop Around</a></h1>
<!-- Cart -->
<div id="cart"> <a href="mycart.jsp" class="cart-link" style="text-decoration: none">Your Shopping Cart</a><br/>
<% if (session.getAttribute("itemlist") != null) {
ArrayList mycart = (ArrayList) session.getAttribute("itemlist");
for (int i = 0; i < mycart.size(); i++) {
Item it = (Item) mycart.get(i);
}
}
%>
<font color="white" size="2">Carts (<% ArrayList il = (ArrayList) session.getAttribute("itemlist");
out.print(il.size());%>)</font>   
<font color="white">Total Rs. <% out.print(session.getAttribute("total"));%></font><br />

</div>
<!-- End Cart -->
<!-- Navigation -->
<div id="navigation">
<ul>
<li><a href="#" class="active">Home</a></li>
<li><a href="#">Support</a></li>
<li class='dropdown'><a href="login.jsp">My Account</a></li>
<li><a href="#">The Store</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
<!-- End Navigation -->
</div>
<!-- End Header -->
<!-- Main -->
<div id="main">
<div class="cl"> </div>
<!-- Content -->
<div id="content">
<!-- Content Slider -->
<div id="slider" class="box">
<div id="slider-holder">
<ul>
<li><a href="#"><img src="css/images/pic1.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/pic2.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/espl.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/mall.jpg" alt="" /></a></li>
</ul>
</div>
<div id="slider-nav"> <a href="#" class="active">1</a> <a href="#">2</a> <a href="#">3</a> <a href="#">4</a> </div>
</div>
<!-- End Content Slider -->
<!-- Products -->
<div class="products">
<div class="cl"> </div>
<ul style="width: 715px; height: 387px">
<li> <a href="#"><img src="css/images/1.jpg" alt="" /></a>
<div class="product-info">
<h3>Electronic</h3>
<div class="product-desc">
<h4></h4>
<p><br />
Logitech G27 Racing Wheel OS</p>
<strong class="price">Rs.21,289</strong> </div>
<center><input name="addtocart7" type="submit" value="Add To Cart"/></center>
</div>
</li>
<li> <a href="#"><img src="css/images/2.jpg" alt="" /></a>
<div class="product-info">
<h3>Electronic</h3>
<div class="product-desc">
<h4></h4>
<p><br />Panasonic Lumax DC DMC-FX78
</p>
<strong class="price">Rs.10,099</strong> </div>
<center><input name="addtocart8" type="submit" value="Add To Cart"/></center>
</div>
</li>
<li class="last"> <a href="#"><img src="css/images/3.jpg" alt="" /></a>
<div class="product-info">
<h3>Electronic</h3>
<div class="product-desc">
<h4></h4>
<p><br />
Milagrow 7" Wi-Fi Tablet (7.4, 4GB) - Black
</p>
<strong class="price">Rs.7,499</strong> </div>
<center><input name="addtocart9" type="submit" value="Add To Cart"/></center>
</div>
</li>
</ul>
<div class="cl"> </div>
</div>
<!-- End Products -->
</div>
<!-- End Content -->
<!-- Sidebar -->
<div id="sidebar">
<!-- Search -->
<div class="box search">
<h2>Search by <span></span></h2>
<div class="box-content">
<form action="#" method="post">
<label>Keyword</label>
<input type="text" class="field" />
<label>Category</label>
<select class="field">
<option value="">-- Select Category --</option>
<ul> </ul>
</select>

<input type="submit" class="search-submit" value="Search" />
<p> <a href="#" class="bul">Advanced search</a><br />
<a href="#" class="bul">Contact Customer Support</a><br/>
<a href="logout1.jsp" class="bul">Logout</a></p>
</form>
</div>
</div>
<!-- End Search -->
<!-- Categories -->
<div class="box categories">
<h2>Categories <span></span></h2>
<div class="box-content">
<ul>
<li><a href="electronics.html">Electronics & Office</a></li>
<li><a href="#">Games & Toys</a></li>
<li><a href="#">Home, Furniture & Patio</a></li>
<li><a href="#">Sports, Fitness & Outdoors</a></li>
<li><a href="#">Pharmacy, Health & Beauty</a></li>
<li class="last"><a href="#">See All Departments</a></li>
</ul>
</div>
</div>
<!-- End Categories -->
</div>
<!-- End Sidebar -->
<div class="cl"> </div>
</div>
<!-- End Main -->
<!-- Side Full -->
<div class="side-full">
<!-- More Products -->
<div class="more-products">
<div class="more-products-holder">
<ul>
<li><a href="#"><img src="css/images/small1.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/small2.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/small3.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/small4.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/small5.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/small6.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/small7.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/small1.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/small2.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/small3.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/small4.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/small5.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/small6.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/small7.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/small1.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/small2.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/small3.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/small4.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/small5.jpg" alt="" /></a></li>
<li><a href="#"><img src="css/images/small6.jpg" alt="" /></a></li>
<li class="last"><a href="#"><img src="css/images/small7.jpg" alt="" /></a></li>
</ul>
</div>
<div class="more-nav"> <a href="#" class="prev">previous</a> <a href="#" class="next">next</a> </div>
</div>
<!-- End More Products -->
<!-- Text Cols -->
<div class="cols">
<div class="cl"> </div>
<div class="col">
<h3 class="ico ico1">Tours & Travels</h3>
<p></p>
<p class="more"><a href="#" class="bul">View More</a></p>
</div>
<div class="col">
<h3 class="ico ico2">Toll Free Number</h3>
<p></p>
<p class="more"><a href="#" class="bul">View More</a></p>
</div>
<div class="col">
<h3 class="ico ico3">Gifts & Events</h3>
<p></p>
<p class="more"><a href="#" class="bul">View More</a></p>
</div>
<div class="col col-last">
<h3 class="ico ico4">Shopping Cart</h3>
<p></p>
<p class="more"><a href="#" class="bul">View More</a></p>
</div>
<div class="cl"> </div>
</div>
<!-- End Text Cols -->
</div>
<!-- End Side Full -->
<!-- Footer -->
<div id="footer">
<p class="left"> <a href="#">Home</a> <span>|</span> <a href="#">Support</a> <span>|</span> <a href="#">My Account</a> <span>|</span> <a href="#">The Store</a> <span>|</span> <a href="#">Contact</a> </p>
<p class="right"> © 2010 Shop Around.</p>
</div>
<!-- End Footer -->
</div>
<!-- End Shell -->
<div align=center></div>
</form>
</body>
</html>


requesthandle.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import classes.Item;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
*
* @author Dilukshan Mahendra
*/
public class requesthandle extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();


// PrintWriter out = response.getWriter();

HttpSession mysession = request.getSession();
ArrayList mycart = (ArrayList) mysession.getAttribute("itemlist");

int value = (Integer) mysession.getAttribute("total");

HttpSession session = request.getSession(false);
if (!session.isNew()) {
// Session is valid
}
else {
response.sendRedirect("timeout.jsp");
}

String i1 = request.getParameter("addtocart1");
String i2 = request.getParameter("addtocart2");
String i3 = request.getParameter("addtocart3");

String i4 = request.getParameter("addtocart7");
String i5 = request.getParameter("addtocart8");
String i6 = request.getParameter("addtocart9");

String chk = request.getParameter("chkout");
String logout = request.getParameter("logout");
String pressdel = request.getParameter("del");

if (i1 != null) {
Item myitem = new Item("1", "Camera", 4690);
value = value + 4690;
mycart.add(myitem);
mysession.setAttribute("itemlist", mycart);
mysession.setAttribute("total", value);
response.sendRedirect("shop.jsp");
} else if (i2 != null) {
Item myitem = new Item("2", "Home Theater", 6999);
value = value + 6999;
mycart.add(myitem);
mysession.setAttribute("itemlist", mycart);
mysession.setAttribute("total", value);
response.sendRedirect("shop.jsp");
} else if (i3 != null) {
Item myitem = new Item("3", "Gaming", 5199);
value = value + 5199;
mycart.add(myitem);
mysession.setAttribute("itemlist", mycart);
mysession.setAttribute("total", value);
response.sendRedirect("shop.jsp");
} else if (i4 != null) {
Item myitem = new Item("4", "Logitech G27 Racing Wheel OS", 21289);
value = value + 21289;
mycart.add(myitem);
mysession.setAttribute("itemlist", mycart);
mysession.setAttribute("total", value);
response.sendRedirect("homepage.jsp");
} else if (i5 != null) {
Item myitem = new Item("5", "Panasonic Lumax DC DMC-FX78", 10099);
value = value + 10099;
mycart.add(myitem);
mysession.setAttribute("itemlist", mycart);
mysession.setAttribute("total", value);
response.sendRedirect("homepage.jsp");
} else if (i6 != null) {
Item myitem = new Item("6", "Milagrow 7\" Wi-Fi Tablet (7.4, 4GB) - Black", 7499);
value = value + 7499;
mycart.add(myitem);
mysession.setAttribute("itemlist", mycart);
mysession.setAttribute("total", value);
response.sendRedirect("homepage.jsp");

} else if (chk != null) {
mysession.setAttribute("chk", chk);
response.sendRedirect("checkout.jsp");

} else if (logout != null) {
mysession.invalidate();
response.sendRedirect("homepage.jsp");

} else if (pressdel != null) {
Item item_to_Delete = (Item) mycart.get(Integer.parseInt(pressdel));
value = value - item_to_Delete.price;

mysession.setAttribute("total", value);
mycart.remove(Integer.parseInt(pressdel));
mysession.setAttribute("tod", pressdel);
response.sendRedirect("mycart.jsp");
}

}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Posting all that code does not do the slightest bit of good - the stack trace in the log should show where in the code the exception was thrown.

Locate the statement in the compiled servlet that Jasper creates from your JSP that threw the exception.

Bill

 
Greenhorn
Posts: 6
Eclipse IDE Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe you will have to replace below condition with something like the one i mentioned:

replace below:
<font color="white" size="2">Carts (<% ArrayList il = (ArrayList) session.getAttribute("itemlist");
out.print(il.size());%>)</font>

with:
<font color="white" size="2">Carts (<% ArrayList il = (ArrayList) session.getAttribute("itemlist");
if(il==null){il = new ArrayList();}
out.print(il.size());%>)</font>

you are trying to use itemlist without checking if the arraylist is null or not, its a good practice to check for all session values if they are null or not before using them.
 
Paddy spent all of his days in the O'Furniture back yard with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic