Exception while excel processing after upload using commons file upload
Hi all,
I am experiencing problem while creating a workbook after getting the input stream from the uploaded file. its not going catch block instead it is going to finally and giving null pointer exeption in finally as one variable in finally is not defined. the variable is defined in try as well as catch but during run time the variable is not getting assigned any value also. I'll put the code over here. please help me with a solution
import org.w3c.dom.* ;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import jxl.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.servlet.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
public class CescoreUploadServlet extends baseHttpServlet
{
private DataSource cesDS = null;
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doPost(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String targetPage = null;
File f = null;
System.out.println("Upload Controller");
HttpSession session = req.getSession(true);
try
{
if(cesDS == null){
cesDS = new JNDIDataSource(getServletContext().getInitParameter(Constants.DATA_SOURCE_NAME));
}
CesRepository cRep = new CesRepository(cesDS);
if (session.getAttribute("DataContainerInfo") == null) {
System.out.println("Initializing DataContainerInfo");
DataContainer DataContainer = new DataContainer();
cRep.setInitialParameters(DataContainer);
session.setAttribute("DataContainerInfo",DataContainer);
}
else System.out.println("DataContainerInfo is available");
UserInfo userInfo = null;
String login_id = req.getRemoteUser();
if(session.getAttribute("UserID") != null) login_id = (String)session.getAttribute("UserID");
if(session.getAttribute("userProfile") == null ) session.setAttribute("userProfile", cRep.getUserInfo(login_id));
userInfo = (UserInfo)session.getAttribute("userProfile");
System.out.println("<<<<<< userInfo contains : "+userInfo.getHrID()+" >>>>>>");
String projIdValue = null;
String msg = null;
boolean isMultipart = FileUpload.isMultipartContent(req);
if(isMultipart){
System.out.println("is MultiPart");
DiskFileUpload upload = new DiskFileUpload();
List fileList = upload.parseRequest(req);
InputStream uploadedFileStream = null;
String uploadedFileName = null;
ArrayList impArray = new ArrayList();
Iterator iter = fileList.iterator();
while (iter.hasNext()) {
System.out.println("inside while");
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
System.out.println("item is not form field");
if (item.getSize() < 1)
{
throw new Exception("No file was uploaded");
}
else
{
uploadedFileName = item.getName();
System.out.println("uploaded file name "+uploadedFileName);
System.out.println("uploaded file size is "+item.getSize());
uploadedFileStream = item.getInputStream();
System.out.println("uploaded input stream available size is "+uploadedFileStream.available());
}
}
else
{
System.out.println("item is form field");
String key = item.getFieldName();
String value = item.getString();
System.out.println("key is"+key);
System.out.println("value is"+value);
if(key.equals("projectId2")){
projIdValue = value;
}
}
}
System.out.println("outside while");
POIFSFileSystem fs = new POIFSFileSystem(uploadedFileStream);
System.out.println("got POIFSFileSystem");//this is been printed in logs
HSSFWorkbook wb = new HSSFWorkbook(fs);//it is breaking over here
System.out.println("got HSSFWorkbook");//this is not been printed in logs
HSSFSheet sheet = wb.getSheetAt(0);
System.out.println("got HSSFSheet");
Iterator rows = sheet.rowIterator();
if(rows.hasNext()){
while( rows.hasNext() ) {
System.out.println("rows iteration");
HSSFRow row = (HSSFRow) rows.next();
Iterator cells = row.cellIterator();
while( cells.hasNext() ) {
System.out.println("cell iteration");
HSSFCell cell = (HSSFCell) cells.next();
HashMap hm = new HashMap();//if everything is fine i'll use this hashmap to store values
}
}
System.out.println("CES UPLOAD.SERVLET. After adding");
msg = "Attendees have been added successfully";
req.setAttribute("msgText", msg);
targetPage = "/ces_disp.jsp";
}
else
{
throw new Exception("The Excel Sheet Uploaded has no entries. Please check and try again");
}
}
else{
throw new Exception("The Form is not Multipart");
}
}
catch (Exception e)
{
System.out.println("CES UPLOAD.SERVLET.EXCEPTION ::: Exception");
targetPage = "/ces_disp.jsp";
if(e != null) req.setAttribute("msgText", e.getMessage());
else req.setAttribute(Constants.EXCEPTION_ATTR_NAME, new Exception("Unknown Exception"));
e.printStackTrace();
}
finally{
System.out.println("CES UPLOAD.SERVLET. ::: Finally");
ServletContext stx = getServletConfig().getServletContext();
RequestDispatcher dispatcher = sCx.getRequestDispatcher(targetPage);
dispatcher.forward(req, res);
}
}
}
Message was edited by: Noufal
Noufal_k
Message was edited by:
Noufal_k
[6067 byte] By [
Noufal_ka] at [2007-10-2 21:16:56]

import org.w3c.dom.* ;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import jxl.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.servlet.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
public class CescoreUploadServlet extends baseHttpServlet
{
private DataSource cesDS = null;
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doPost(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//including only relevant code
String targetPage = null;
System.out.println("Upload Controller");
HttpSession session = req.getSession(true);
try
{
String projIdValue = null;
String msg = null;
boolean isMultipart = FileUpload.isMultipartContent(req);
if(isMultipart){
System.out.println("is MultiPart");
DiskFileUpload upload = new DiskFileUpload();
List fileList = upload.parseRequest(req);
InputStream uploadedFileStream = null;
String uploadedFileName = null;
Iterator iter = fileList.iterator();
while (iter.hasNext()) {
System.out.println("inside while");
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
System.out.println("item is not form field");
if (item.getSize() < 1)
{
throw new Exception("No file was uploaded");
}
else
{
uploadedFileName = item.getName();
System.out.println("uploaded file name "+uploadedFileName);//printing c:/excelsheets/fileToUpload.xls
System.out.println("uploaded file size is "+item.getSize());//printing size is 15872
uploadedFileStream = item.getInputStream();
System.out.println("uploaded input stream available size is "+uploadedFileStream.available());//printing available input stream size is 15872
}
}
else
{
System.out.println("item is form field");
String key = item.getFieldName();
String value = item.getString();
System.out.println("key is"+key);
System.out.println("value is"+value);
if(key.equals("projectId2")){
projIdValue = value;
}
}
}
System.out.println("outside while");
POIFSFileSystem fs = new POIFSFileSystem(uploadedFileStream);
System.out.println("got POIFSFileSystem");//this is been printed in logs
HSSFWorkbook wb = new HSSFWorkbook(fs);//it is breaking over here
System.out.println("got HSSFWorkbook");//this is not been printed in logs
HSSFSheet sheet = wb.getSheetAt(0);
System.out.println("got HSSFSheet");
Iterator rows = sheet.rowIterator();
if(rows.hasNext()){
while( rows.hasNext() ) {
System.out.println("rows iteration");
HSSFRow row = (HSSFRow) rows.next();
Iterator cells = row.cellIterator();
while( cells.hasNext() ) {
System.out.println("cell iteration");
HSSFCell cell = (HSSFCell) cells.next();
HashMap hm = new HashMap();//if everything is fine i'll use this hashmap to store values
}
}
System.out.println("CES UPLOAD.SERVLET. After adding");
msg = "Attendees have been added successfully";
req.setAttribute("msgText", msg);
targetPage = "/ces_disp.jsp";
}
else
{
throw new Exception("The Excel Sheet Uploaded has no entries. Please check and try again");
}
}
else{
throw new Exception("The Form is not Multipart");
}
}
catch (Exception e)
{
System.out.println("CES UPLOAD.SERVLET.EXCEPTION ::: Exception");
targetPage = "/ces_disp.jsp";
if(e != null) req.setAttribute("msgText", e.getMessage());
else req.setAttribute(Constants.EXCEPTION_ATTR_NAME, new Exception("Unknown Exception"));
e.printStackTrace();
}
finally{
System.out.println("CES UPLOAD.SERVLET. ::: Finally");
ServletContext stx = getServletConfig().getServletContext();
RequestDispatcher dispatcher = stx.getRequestDispatcher(targetPage);//throwing null pointer exception for this line
dispatcher.forward(req, res);
}
}
}
this is what im getting in unix console
is MultiPart
inside while
item is form field
key isfrmAction
value isuploadAttendee
inside while
item is form field
key isprojectId
value is6968
inside while
item is form field
key ismodule
value isFinanceAdmin
inside while
item is not form field
uploaded file name C:\TempRenamed\Noufal\test\nks1.xls
uploaded file size is 13824
uploaded input stream available size is 13824
outside while
got POIFSFileSystem
Forwarding.......
:::::::::::::::::::::::::::: Processing time ends : 1148986638347:::::::::
:::::::::::::::::::::::::::: Processing time []: 8:::::::::
The target page is ++++++ null
<May 30, 2006 6:57:18 AM EDT> <Error> <HTTP> <BEA-101020> <[ServletContext(id=76815586,name=cescore,context-path=/CES/noufalCescore)] Servlet failed with Exception
java.lang.NullPointerException
at com.lehman.cad.gc.approvals.cescore.CescoreServlet.doGet(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V(CescoreServlet.java:5066)
at com.lehman.cad.gc.approvals.cescore.CescoreServlet.doPost(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V(CescoreServlet.java:62)
at javax.servlet.http.HttpServlet.service(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(Ljavax/servlet/ServletRequest;Ljavax/servlet/ServletResponse;)V(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run()Ljava/lang/Object;(ServletStubImpl.java:996)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(Ljavax/servlet/ServletRequest;Ljavax/servlet/ServletResponse;Lweblogic/servlet/internal/FilterChainImpl;)V(ServletStubImpl.java:419)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(Ljavax/servlet/ServletRequest;Ljavax/servlet/ServletResponse;)V(ServletStubImpl.java:315)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run()Ljava/lang/Object;(WebAppServletContext.java:6452)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(Lweblogic/security/subject/AbstractSubject;Ljava/security/PrivilegedAction;)Ljava/lang/Object;(Optimized Method)
at weblogic.security.service.SecurityManager.runAs(Lweblogic/security/acl/internal/AuthenticatedSubject;Lweblogic/security/acl/internal/AuthenticatedSubject;Ljava/security/PrivilegedAction;)Ljava/lang/Object;(SecurityManager.java:118)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(Lweblogic/servlet/internal/ServletRequestImpl;Lweblogic/servlet/internal/ServletResponseImpl;)V(WebAppServletContext.java:3661)
at weblogic.servlet.internal.ServletRequestImpl.execute(Lweblogic/kernel/ExecuteThread;)V(ServletRequestImpl.java:2630)
at weblogic.kernel.ExecuteThread.execute(Lweblogic/kernel/ExecuteRequest;)V(ExecuteThread.java:219)
at weblogic.kernel.ExecuteThread.run()V(ExecuteThread.java:178)
at java.lang.Thread.startThreadFromVM(Ljava/lang/Thread;)V(Unknown Source)