Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: [solved] Newbie question: Generating an Excel View

  1. #1

    Default [solved] Newbie question: Generating an Excel View

    Hi.. I'm a newbie in java and spring framework.. so I'm sorry if my errors are a bit.. dumb or something..

    I've tried the Countries sample provided with the spring framework distribution.. and it worked perfectly..

    but when I try to use the same code to generate an excel view in my application.. I always encountered this exception:

    Code:
    java.lang.NullPointerException
    	com...OvertimeExcelView.buildExcelDocument(OvertimeExcelView.java:44)
    	org.springframework.web.servlet.view.document.AbstractExcelView.renderMergedOutputModel(AbstractExcelView.java:140)
    	org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:219)
    	org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:630)
    	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:523)
    	org.springframework.web.servlet.FrameworkServlet.serviceWrapper(FrameworkServlet.java:342)
    	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:328)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
    in my servlet xml:

    Code:
    	<bean id="overTimeFormController" 
    			class="com...OverTimeFormController">
    		<property name="commandName"><value>user</value></property>
    		<property name="commandClass">
    			<value>com...User</value>
    		</property>
    		<property name="formView">
    			<value>overtimeView</value>
    		</property>
    		<property name="successView">
    			<value>overTime.spring</value>
    		</property>	
    		<property name="validator"><ref bean="beanValidator" /></property>
    	</bean>
    I used a class named User to contain all the data to be shown in the view..
    and my controller class code is like this:

    Code:
    public class OverTimeFormController extends SimpleFormController &#123;
        
        /* &#40;non-Javadoc&#41;
         * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit&#40;javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException&#41;
         */
        protected ModelAndView onSubmit&#40;HttpServletRequest request,
                HttpServletResponse response, Object command, BindException binder&#41;
                throws Exception &#123;
            
            if&#40;request.getParameter&#40;"excel"&#41;!=null&#41;&#123;
    
                RefreshablePagedListHolder listHolder = 
        		    &#40;RefreshablePagedListHolder&#41; 
        		    	request.getSession&#40;true&#41;.getAttribute&#40;"user"&#41;;
        		return new ModelAndView&#40;"overtime_excelView", "user", listHolder&#41;;
                
            &#125;else&#123;
    	        User user = &#40;User&#41; command;
    	        BindException errors = new BindException&#40;user, "user"&#41;;
    	        return new ModelAndView&#40;"overtimeView", errors.getModel&#40;&#41;&#41;;            
            &#125;
        &#125;
        
    &#125;
    everything is working perfectly, the validation using commons validator, viewresolving using resource bundles.. but when I try to export to excel I always get a null pointer exception

    like you can see in the controller, the flow is like this..
    1. the user submitted the form with some parameters (like employee number and date), and the view is returned back to the user listing his overtime data.. (the application is perfectly working until this step)
    2. if the user pressed the export to excel button.. the controller will then generate the same form in excel format.. (this is where i get the exception)

    I also tried supplying the User object in formBackingObject.. but that didnt seem to help..

    what am I doing wrong? And am I supplying enough code? please tell me if i dont supply enough code...
    any help would be appreciated.. thank you

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    From the stackTrace, the exception is thrown in
    Code:
      com...OvertimeExcelView.buildExcelDocument
    could you provide the source, or an excerpt for this class?
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3

    Default

    Oh.. of course..

    this is the source of OverTimeExcelView.java:

    Code:
    /*
     * Created on Aug 20, 2004
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Generation - Code and Comments
     */
    package com...view;
    
    import java.util.List;
    import java.util.Locale;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.springframework.beans.support.RefreshablePagedListHolder;
    import org.springframework.web.servlet.view.document.AbstractExcelView;
    
    /**
     * @author Administrator
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Generation - Code and Comments
     */
    public class OvertimeExcelView extends AbstractExcelView &#123;
    
        /* &#40;non-Javadoc&#41;
         * @see org.springframework.web.servlet.view.document.AbstractExcelView#buildExcelDocument&#40;java.util.Map, org.apache.poi.hssf.usermodel.HSSFWorkbook, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse&#41;
         */
        protected void buildExcelDocument&#40;Map model, HSSFWorkbook wb,
                HttpServletRequest request, HttpServletResponse response&#41;
        throws Exception &#123;
            
            HSSFSheet sheet;
            HSSFRow sheetRow;
            HSSFCell cell;
    
    		// We search the data to insert
    		RefreshablePagedListHolder pgHolder = &#40;RefreshablePagedListHolder&#41; model.get&#40;"user"&#41;;
    		Locale loc = pgHolder.getLocale&#40;&#41;;
    
    		sheet = wb.createSheet&#40;"Spring"&#41;;
    
    		if &#40;null == pgHolder&#41; &#123;
    			getCell&#40;sheet, 0, 0&#41;.setCellValue&#40;getMessageSourceAccessor&#40;&#41;.getMessage&#40;"nolist", loc&#41;&#41;;
    			return;
    		&#125;        
            
            // Go to the first sheet
            // getSheetAt&#58; only if wb is created from an existing document
            //sheet = wb.getSheetAt&#40; 0 &#41;;
            sheet.setDefaultColumnWidth&#40;&#40;short&#41;12&#41;;
    
            // write a text at A1
            cell = getCell&#40; sheet, 0, 0 &#41;;
            setText&#40;cell,"Spring-Excel test"&#41;;
    
            List words = &#40;List &#41; model.get&#40;"list"&#41;;
            for &#40;int i=0; i < words.size&#40;&#41;; i++&#41; &#123;
                cell = getCell&#40; sheet, 2+i, 0 &#41;;
                setText&#40;cell, &#40;String&#41; words.get&#40;i&#41;&#41;;
            &#125;
    
        &#125;
    
    &#125;
    but i guess the exception is thrown before this class is called/instantiated..
    i just copied the example in the spring api for this class..

  4. #4
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    java.lang.NullPointerException is thrown in line 44 in OvertimeExcelView.java
    Code:
      Locale loc = pgHolder.getLocale&#40;&#41;;
    this means that model.get("user") returns null.

    Attribute "user" is extracted from HttpSession in OverTimeFormController
    Code:
      RefreshablePagedListHolder listHolder = 
                  &#40;RefreshablePagedListHolder&#41;
                     request.getSession&#40;true&#41;.getAttribute&#40;"user"&#41;;
    can you check if the returned value is not null?
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  5. #5

    Default

    yes.. i guess that's the problem.. the object "user" is not in the session..
    i already checked that

    Code:
    RefreshablePagedListHolder listHolder = 
                  &#40;RefreshablePagedListHolder&#41; 
                     request.getSession&#40;true&#41;.getAttribute&#40;"user"&#41;;
    returned null.. but like i said before.. I already tried to supply that object in the method formBackingObject in the controller.. but that didn't help...

    i really dont know where the problem is.. or why that returned null... because when I tried to show the view in plain html.. using jstl.. there's no problem at all..

  6. #6
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    Are the user use supply to jstl and the one you want to use in Excel the same?
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  7. #7

    Default

    yes of course.. all the data I want to process is in the user object..

  8. #8
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    so why not just do
    Code:
            User user = &#40;User&#41; command; 
            if&#40;request.getParameter&#40;"excel"&#41;!=null&#41;&#123; 
              return new ModelAndView&#40;"overtime_excelView", "user", user&#41;; 
                
            &#125;else&#123; 
               BindException errors = new BindException&#40;user, "user"&#41;; 
               return new ModelAndView&#40;"overtimeView", errors.getModel&#40;&#41;&#41;;            
            &#125;
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  9. #9

    Default

    oh... yes2 that worked.. thanks a lot..

    silly me.. I thought you must always use a refreshablepagedlistholder to contain the object to be shown in the excel view..

    i guess i still have a lot to learn about java...

    so.. here's what i did.. just in case some newbie like me want to know how to do it :oops: :

    in ModelAndView:

    Code:
            User user = &#40;User&#41; command; 
            if&#40;request.getParameter&#40;"excel"&#41;!=null&#41;&#123; 
              return new ModelAndView&#40;"overtime_excelView", "user", user&#41;; 
                
            &#125;else&#123; 
               BindException errors = new BindException&#40;user, "user"&#41;; 
               return new ModelAndView&#40;"overtimeView", errors.getModel&#40;&#41;&#41;;            
            &#125;
    in OvertimeExcelView.java:

    Code:
    public class OvertimeExcelView extends AbstractExcelView &#123;
    
        /* &#40;non-Javadoc&#41;
         * @see org.springframework.web.servlet.view.document.AbstractExcelView#buildExcelDocument&#40;java.util.Map, org.apache.poi.hssf.usermodel.HSSFWorkbook, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse&#41;
         */
        protected void buildExcelDocument&#40;Map model, HSSFWorkbook wb,
                HttpServletRequest request, HttpServletResponse response&#41;
        throws Exception &#123;
            
            HSSFSheet sheet;
            HSSFRow sheetRow;
            HSSFCell cell;
    
           sheet = wb.createSheet&#40;"Spring"&#41;;
            sheet.setDefaultColumnWidth&#40;&#40;short&#41;12&#41;;
    
            // write a text at A1
            cell = getCell&#40; sheet, 0, 0 &#41;;
            setText&#40;cell,"Spring-Excel test"&#41;;
    
            User user = &#40;User&#41; model.get&#40;"user"&#41;;
            List words = user.getList&#40;&#41;;
    
            ...
    
        &#125;
    
    &#125;

  10. #10
    Join Date
    Feb 2005
    Posts
    8

    Default

    how did you define overtime_excelView in configuration file? do you define it as a page or something else?

    thanks

Similar Threads

  1. Replies: 9
    Last Post: Nov 1st, 2005, 10:36 PM
  2. Beandoc crashing (on its samples!)
    By aaime in forum Container
    Replies: 17
    Last Post: Oct 7th, 2005, 07:21 AM
  3. Replies: 3
    Last Post: Apr 29th, 2005, 06:09 AM
  4. Content Provider vs View Model
    By Martin Kersten in forum Swing
    Replies: 21
    Last Post: Mar 10th, 2005, 02:25 PM
  5. Newbie question on Pages and Views
    By ragnarwestad in forum Swing
    Replies: 8
    Last Post: Dec 13th, 2004, 10:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •