Link to home
Start Free TrialLog in
Avatar of FrostyGirl
FrostyGirlFlag for United States of America

asked on

JSP Error: java.lang.NullPointerException

Hi, I'm going to post the code below for this error.  Basically I'm new to JSP and I'm been given the task to fix this broken application.  Whenever I go to post a <form>, I receive this error:

org.apache.jasper.JasperException
&#9;org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
&#9;org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
&#9;org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
&#9;javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause

java.lang.NullPointerException
&#9;com.monumental.trampoline.utilities.forms.multipart.MultipartRequest.getFile(MultipartRequest.java:72)
&#9;org.apache.jsp.admin.productfamilies.process_005fnavigation_jsp._jspService(process_005fnavigation_jsp.java:205)
&#9;org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
&#9;javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
&#9;org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
&#9;org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
&#9;org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
&#9;javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
Here is the form page:

<%@ page language="java" %>
<%@ page import="com.monumental.trampoline.component.*" %>
<%@ page import="com.monumental.trampoline.navigation.*" %>
<%@ page import="com.monumental.trampoline.security.*" %>
<%@ page import="com.monumental.trampoline.content.*" %>
<%@ page import="com.vdlm.productfamilies.*" %>
<jsp:useBean id="dbResources" class="com.monumental.datasources.rdbms.DbResources" />
<jsp:useBean id="userbean" class="com.monumental.trampoline.security.UserBean" />
<jsp:useBean id="navbean" class="com.vdlm.productfamilies.ProductFamilyNavigationCacheBean" />
<% userbean.init(pageContext, dbResources); %>
<% userbean.onlyUsers( "../reload.jsp"); %>
<% CompProperties props = new CompProperties(); %>
<%
boolean add = false;
boolean edit = false;
boolean delete = false;
Permission permission = new Permission();
User user = userbean.getUser();
if( user.getGroup().hasData() ) {
	permission = user.getGroup().getPermission( new ProductFamilyNavigation() );
	}
if(permission.getAdd()) {
	add = true;
	}
if(permission.getEdit()) {
	edit = true;
	}
if(permission.getDelete()) {
	delete = true;
	}
%>
<%
int currentNavId = 0;
if(request.getParameter(ProductFamilyNavigation.PARAM)!=null) {
	currentNavId = Integer.parseInt(request.getParameter(ProductFamilyNavigation.PARAM));
} else if(session.getAttribute("currentNavId")!=null) {
	currentNavId = ((Integer)session.getAttribute("currentNavId")).intValue();
	}
session.setAttribute("currentNavId",new Integer(currentNavId));
%>
<%
navbean.init(pageContext, dbResources);



ProductFamilyNavigationCache navcache = navbean.getCache();
CompEntities navigations = navcache.getAllSubNavigations(currentNavId);
CompEntities navigations2 = navcache.getTree();
ProductFamilyNavigation navigation;
ProductFamilyNavigation tempNav;

TemplateCache tempcache = new TemplateCache(dbResources,props.getAdminConfiguration());
TemplateController tempcontrol = new TemplateController(dbResources,props.getAdminConfiguration());
CompEntities templates2 = tempcontrol.getTemplates( true );
Template template;
CompEntities templates = new CompEntities();
while(templates2.hasNext()) {
	template = (Template)templates2.next();
	template = (Template)tempcache.getTemplate(template.getId());
	templates.add(template);
}

ProductFamilyNavigation thisnav = navcache.getNavigation(currentNavId);
CompEntities hier = null;
if(thisnav!=null) {
	hier = navcache.getNavigationHierarchy(thisnav);
	}	
dbResources.close();
%>
<% int indent = 1; %>
<%
String restrict = "";
boolean restricted = false;
Content content = null;
if(request.getParameter("restrict") != null) { 
		restrict = request.getParameter("restrict");
		restricted = true;
		Class myClass = Class.forName(restrict);
		content = (Content)myClass.newInstance();
	}
%>

<!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>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" />
<title></title>
<style type="text/css">
@import url(../common/main.css);
</style>
</head>
<body>
<div id="bodycontent">
<% int tabindex = 1; %>
<div id="addnavigation">
	<form method="post" action="process_navigation.jsp" id="contentform" enctype="multipart/form-data">
		<fieldset>
			<legend>add a category</legend>
			<label for="<%= ProductFamilyNavigation.NAME_COLUMN %>">name:</label>
			<input type="text" size="50" id="<%= ProductFamilyNavigation.NAME_COLUMN %>" name="<%= ProductFamilyNavigation.NAME_COLUMN %>" tabindex="<%=tabindex++%>"/>
			<br />				
			<label for="<%= ProductFamilyNavigation.DESCRIPTION_COLUMN %>">description:</label>
			<input type="text" size="50" id="<%= ProductFamilyNavigation.DESCRIPTION_COLUMN %>" name="<%= ProductFamilyNavigation.DESCRIPTION_COLUMN %>" tabindex="<%=tabindex++%>"/>
			<br />
			<label for="<%= ProductFamilyNavigation.PICTURE_COLUMN %>">picture:</label>
			<input type="file" id="<%= ProductFamilyNavigation.PICTURE_COLUMN %>" name="<%= ProductFamilyNavigation.PICTURE_COLUMN %>" tabindex="<%=tabindex++%>" />
			<br />
			<input type="hidden" name="action" value="add" />
			<input type="hidden" name="<%= ProductFamilyNavigation.PARENT_ID_COLUMN %>" value="<%= currentNavId %>" />			
			<input class="submit" type="submit" name="submit" value="create" tabindex="<%=tabindex++%>" />
		</fieldset>
	</form>
	</div>
	
	
	<div id="currentnavigations">
		<h1 id="currentnavigationsheader">category hierarchy</h1>
		<span class="toplevelnavigation"><a href="navigation.jsp?<%= ProductFamilyNavigation.PARAM %>=0">Top Level</a>
		<% if(hier!=null) { %>
			<% while(hier.hasNext()) { %>			
				<% navigation = (ProductFamilyNavigation)hier.next(); %>
					: <a class="<% if(!navigation.isActive()) { %>inactive<% } %>nav" href="navigation.jsp?<%= ProductFamilyNavigation.PARAM %>=<%= navigation.getId() %>"><%= navigation.getName() %></a>
			<% } %>
		<% } %>	
		</span>
		
		<% Template navTemplate; %>
		<% boolean navON = true; %>
		<% if(navigations.howMany() > 0) { %>
		<div id="currentnavcontainer">       			
			<% for(int x=0; x<navigations.howMany();x++) { %> 
				<% navigation = (ProductFamilyNavigation)navigations.get(x); %>
				<% navTemplate = navigation.getTemplate(); %>
				<%
				if(restricted) {
				if (navTemplate==null || !navTemplate.canContainContent(content)) {
					navON = false;
				} else {
					navON = true;
				}
				}
				%>
				<div class="singlenav">
					<span class="navheader">
						<a class="<% if(!navigation.isActive()) { %>inactive<% } %>navigation" href="navigation.jsp?<%= ProductFamilyNavigation.PARAM %>=<%= navigation.getId() %>"><%= navigation.getName() %><% if(navigation.isLink()) { %>&nbsp;&nbsp;<--links to another navigation--><% } %></a>
					</span>
					<form method="get" action="process_navigation.jsp" class="navposition">
						<select name="<%= ProductFamilyNavigation.POSITION_COLUMN %>" tabindex="<%=tabindex++%>">		
						<% for(int y=0; y<navigations.howMany();y++) { %> 
							<% tempNav = (ProductFamilyNavigation)navigations.get(y); %>
							<option value="<%= y+1 %>" <% if(tempNav.getId()==navigation.getId()) { %>selected<% } %>><%= y+1 %></option>
						<% } %>
						</select>
						<input type="hidden" name="<%= ProductFamilyNavigation.PARAM %>" value="<%= navigation.getId() %>" />
						<input type="hidden" name="action" value="order" />
						<input class="submit" type="submit" value="set order" tabindex="<%=tabindex++%>"  <% if(!navON) { %>style="visibility: hidden;"<% } %>/>
					</form>	
					<form method="get" action="process_navigation.jsp" class="navstatus">
						<select name="<%= ProductFamilyNavigation.STATUS_COLUMN %>" tabindex="<%=tabindex++%>">			
							<option><% if(navigation.isActive()) { %>active<% } else { %>inactive<% } %></option> 
							<option value="<% if(navigation.isActive()) { %>active<% } else { %>inactive<% } %>">-----</option>
							<option>active</option>
							<option>inactive</option>
						</select>	
						<input type="hidden" name="<%= ProductFamilyNavigation.PARAM %>" value="<%= navigation.getId() %>" />
						<input type="hidden" name="action" value="status" />
						<input class="submit" type="submit" value="set status" tabindex="<%=tabindex++%>" <% if(!navON) { %>style="visibility: hidden;"<% } %>/>
					</form>				
					<form method="get" action="navigation_details.jsp" class="navparent">
						<input type="hidden" name="<%= ProductFamilyNavigation.PARAM %>" value="<%= navigation.getId() %>" />
						<input class="submit" type="submit" value="more options"  tabindex="<%=tabindex++%>" <% if(!navON) { %>style="visibility: hidden;"<% } %>/>
					</form>														
					<br />					
				</div>
			<% } %>	
		</div>
		<% } %>		
	</div>
</div>

Open in new window

Avatar of FrostyGirl
FrostyGirl
Flag of United States of America image

ASKER

AND here is the processor.jsp


<%@ page language="java" %>
<%@ page import="com.monumental.trampoline.component.*" %>
<%@ page import="com.monumental.trampoline.navigation.*" %>
<%@ page import="com.monumental.trampoline.security.*" %>
<%@ page import="com.vdlm.productfamilies.*" %>
<%@ page import="com.monumental.trampoline.utilities.forms.multipart.*" %>
<%@ page import="java.io.*" %>

<jsp:useBean id="dbResources" class="com.monumental.datasources.rdbms.DbResources" />
<jsp:useBean id="userbean" class="com.monumental.trampoline.security.UserBean" />
<% userbean.init(pageContext, dbResources); %>
<% userbean.onlyUsers( "/trampoline/admin/reload.jsp"); %>
<% User user = userbean.getUser(); %>
<% CompProperties props = new CompProperties(); %>
<%
	

	
ProductFamilyNavigationController navcontrol = new ProductFamilyNavigationController(dbResources,props.getAdminConfiguration());
ProductFamilyNavigation navigation = new ProductFamilyNavigation();
navigation.clearData();

String action = request.getParameter("action");
String redirect = request.getHeader("referer");
String onlypages = request.getParameter("only");


MultipartRequest multipart = null;
String basePath = pageContext.getServletContext().getRealPath("/");
int contid = 0;

if (request.getParameter("action")==null) {
	String uploadDir = basePath + navigation.getComponentUploadDirectory()  + "/" + user.getId();	
	File createDir = new File(uploadDir);
	if(!createDir.exists()) {
		createDir.mkdirs();
	}

	multipart = new MultipartRequest(request,uploadDir,105242880);
	action = multipart.getParameter("action");
	if(multipart.getParameter(ProductFamilyNavigation.PARAM)!=null) {
		contid = Integer.parseInt(multipart.getParameter(ProductFamilyNavigation.PARAM));
	}
} else {
	if(request.getParameter(ProductFamilyNavigation.PARAM)!=null) {
		contid = Integer.parseInt(request.getParameter(ProductFamilyNavigation.PARAM));
	}
}


if (action!=null && action.equals("add")) {
	navigation.setParentId(Integer.parseInt(multipart.getParameter(ProductFamilyNavigation.PARENT_ID_COLUMN)));
	navigation.setName(multipart.getParameter(ProductFamilyNavigation.NAME_COLUMN));
	navigation.setDescription(multipart.getParameter(ProductFamilyNavigation.DESCRIPTION_COLUMN));
	
	
	int myId = navcontrol.addNavigation(navigation);
	
	navigation.setId(myId);	

	//Do some file uploading here for the picture
	File upload = multipart.getFile(ProductFamilyNavigation.PICTURE_COLUMN);
	if(upload != null)
	{
		String newName = upload.getName().replace(' ','_');
		navcontrol.deleteFileFromContentDirectory(basePath, navigation, navigation.getPicture());
		boolean success = navcontrol.saveFileToContentDirectory(upload, basePath, navigation, newName,true);
		upload.delete();
		navigation.setPicture(newName);
	}
	
	//Do some file uploading here for the picture
	upload = multipart.getFile(ProductFamilyNavigation.ROLLOVER_COLUMN);
	if(upload != null) {
		String newName = upload.getName().replace(' ','_');
		navcontrol.deleteFileFromContentDirectory(basePath, navigation, navigation.getRollOver());
		boolean success = navcontrol.saveFileToContentDirectory(upload, basePath, navigation, newName,true);
		upload.delete();
		navigation.setRollOver(newName);
	}
	
	navcontrol.updateNavigation(navigation);
    
}

if (action!=null && action.equals("status")) {
    navigation.setId(Integer.parseInt(request.getParameter(ProductFamilyNavigation.PARAM)));
    if( request.getParameter(ProductFamilyNavigation.STATUS_COLUMN).equals("active") ) {
    	navigation.setActive(true);
    	} else {
    	navigation.setActive(false);
    	}    
    navcontrol.updateNavigation(navigation);
}

if (action!=null && action.equals("location")) {
    navigation.setId(Integer.parseInt(request.getParameter(ProductFamilyNavigation.PARAM)));
    navigation.setParentId(Integer.parseInt(request.getParameter(ProductFamilyNavigation.PARENT_ID_COLUMN)));
    navigation.setPosition(1);    
    navcontrol.updateNavigation(navigation);
    navcontrol.setNavigationPosition(navigation);
	if(onlypages != null) { 
		redirect = "navigation.jsp?" + ProductFamilyNavigation.PARAM + "=" + navigation.getParentId() + "&only=pages";
	} else {
		redirect = "navigation.jsp?" + ProductFamilyNavigation.PARAM + "=" + navigation.getParentId();	
	}

}

if (action!=null && action.equals("order")) {
    navigation.setId(Integer.parseInt(request.getParameter(ProductFamilyNavigation.PARAM)));
    navigation.setPosition(Integer.parseInt(request.getParameter(ProductFamilyNavigation.POSITION_COLUMN)));    
    navcontrol.setNavigationPosition(navigation);
}

if (action!=null && action.equals("edit")) {
    navigation.setId(Integer.parseInt(multipart.getParameter(ProductFamilyNavigation.PARAM)));
    navigation.setName(multipart.getParameter(ProductFamilyNavigation.NAME_COLUMN)); 
    navigation.setDescription(multipart.getParameter(ProductFamilyNavigation.DESCRIPTION_COLUMN));
	
   	File upload = multipart.getFile(ProductFamilyNavigation.PICTURE_COLUMN);
	if(upload != null)
	{
		String newName = upload.getName().replace(' ','_');
		navcontrol.deleteFileFromContentDirectory(basePath, navigation, navigation.getPicture());
		boolean success = navcontrol.saveFileToContentDirectory(upload, basePath, navigation, newName,true);
		upload.delete();
		navigation.setPicture(newName);
	}
	
	//Do some file uploading here for the picture
	upload = multipart.getFile(ProductFamilyNavigation.ROLLOVER_COLUMN);
	if(upload != null) {
		String newName = upload.getName().replace(' ','_');
		navcontrol.deleteFileFromContentDirectory(basePath, navigation, navigation.getRollOver());
		boolean success = navcontrol.saveFileToContentDirectory(upload, basePath, navigation, newName,true);
		upload.delete();
		navigation.setRollOver(newName);
	}
	
	navcontrol.updateNavigation(navigation);
}

if (action!=null && action.equals("link")) {
    navigation.setId(Integer.parseInt(request.getParameter(ProductFamilyNavigation.PARAM)));
    navigation.setReferenceId(Integer.parseInt(request.getParameter(ProductFamilyNavigation.REFERENCE_ID_COLUMN)));   
    navcontrol.updateNavigation(navigation);
}
/*
if (action!=null && action.equals("template")) {
    navigation.setId(Integer.parseInt(request.getParameter(ProductFamilyNavigation.PARAM)));
    navigation.setTemplateId(Integer.parseInt(request.getParameter(ProductFamilyNavigation.TEMPLATE_ID_COLUMN)));
    //navigation.setContentTemplateId(Integer.parseInt(request.getParameter(ProductFamilyNavigation.CONTENT_TEMPLATE_ID_COLUMN)));    
    navcontrol.updateNavigation(navigation);
}
*/
if (action!=null && action.equals("delete")) {
    navigation.setId(Integer.parseInt(request.getParameter(ProductFamilyNavigation.PARAM)));
    ProductFamilyNavigationContent navContent;
    
    //delete page navigation
    navContent = new com.vdlm.productfamilies.ProductFamilyNavigationContent();
    navContent.setNavigationId(navigation.getId());
    navcontrol.deleteNavigationContentByNavigation(navContent);
           
    navcontrol.deleteNavigation(navigation.getId());
    redirect = "navigation.jsp";
}

ProductFamilyNavigationCacheBean cache = new ProductFamilyNavigationCacheBean();
cache.init( pageContext, dbResources );
cache.resetCache();

dbResources.close();
response.sendRedirect(redirect);
%>

Open in new window

Avatar of Sathish David  Kumar N
check this below line whether it having value or not

currentNavId = Integer.parseInt(request.getParameter(ProductFamilyNavigation.PARAM));

currentNavId = ((Integer)session.getAttribute("currentNavId")).intValue();
it's value is zero.  It shouldn't be though.  I don't know where the fail point is.
oh, also, how can I display the current navID on the page so I can see if it's working?  For testing that is.
I don't know if I will be any help to you. But I will try.
><%@ page import="com.monumental.trampoline.utilities.forms.multipart.*" %>
Where does this code package come from ? Is it something you created ? Is it online somewhere ?
The error message tells us that the problem is at  
>process_005fnavigation_jsp.java:205    
This file is the Servlet  source code that was created by translating the process_005navigation.jsp file.  
Can you look in Tomcat work folder for this file ?  
Please post lines of code near and around line 205.  
By the way, the action in your multipart form is process_navigation.jsp.
Did you change the file name ?
Anyway my guess is the problem line is line 73 of your processor
>upload = multipart.getFile(ProductFamilyNavigation.ROLLOVER_COLUMN);  
I don't see any mention of ProductFamilyNavigation.ROLLOVER_COLUMN  in your form page.  
In your form page, the file element in the multipart form has the name
>ProductFamilyNavigation.PICTURE_COLUMN  
So maybe you could try changing  the problem line to
upload = multipart.getFile(ProductFamilyNavigation.PICTURE_COLUMN );  
But I notice in your previous question at
https://www.experts-exchange.com/questions/26977611/JSP-NullPointer-Error.html 
That is what was at that time. Why did you change it to ROLLOVER_COLUMN  ?

The multipart form does direct to process_navigation.jsp.  Process_navigation has the 'stuff' that processes the form action.  

I didn't change anything to ROLLOVER_COLUMN.  That may have come from a different page.... I will doublecheck.

I'll post the scriplet soon...
Avatar of vsasi
vsasi

1)User try catch block to find
2)Use null check for any bean value retrieval or parameter
ASKER CERTIFIED SOLUTION
Avatar of rikaz
rikaz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial