0

The code that creates a pdf file works fine when the filestorePath is hardcode. As soon as I replace the hardcoded filestorePath with a value fetched from a database, I get a NullPointerException when the view is called. The query returns the correct value, because the logic is used in other places in the code and there it works fine.

Any ideas?

public class PDFDeliveryNoteBuilder extends AbstractPdfViewCustom{

Logger logger = LoggerFactory.getLogger(PDFDeliveryNoteBuilder.class);

@Autowired
private RepositoryJdbcTemplate dbRepository;

private BaseFont bfBold;
private BaseFont bf;
private int pageNumber = 0;

protected void buildPdfDocument(Map<String, Object> model, Document doc,
                    PdfWriter docWriter, HttpServletRequest request, HttpServletResponse response)
                    throws Exception {

    //String filestorePath = "/xxx/www/xxxxx/tmp/";
    //TODO gives null pointer exception
    SysParam sysParam = dbRepository.findSysParam();
    String sysTmpdir = sysParam.getSysTmpdir();
    String filestorePath = sysTmpdir;
    String downloadfileNm = "DeliveryNote.pdf";
    String filePath = filestorePath+downloadfileNm;
    logger.debug("--> filePath: "+filePath);
    initializeFonts();

    try {
        // get data model which is passed by the Spring container
        @SuppressWarnings("unchecked")
        List<AuditTrailHist> auditTrailHistPublishDeliveredList = (ArrayList<AuditTrailHist>) model.get("auditTrailHistDeliveredList");

        docWriter = PdfWriter.getInstance(doc , new FileOutputStream(filePath));
        doc.addAuthor("xxx");
        doc.addCreationDate();
        doc.addProducer();
        doc.addCreator("xxx");
        doc.addTitle("Delivery Note");
        doc.setPageSize(PageSize.LETTER);
        doc.open();
        PdfContentByte cb = docWriter.getDirectContent();
        boolean beginPage = true;
        int y = 0;
        //get the most recent audit trail history record for header
        Integer size = auditTrailHistPublishDeliveredList.size();
        AuditTrailHist auditTrailHistRecentRec = auditTrailHistPublishDeliveredList.get(size-1);
        //get all the audit trail history records for detail
        for (AuditTrailHist auditTrailHist : auditTrailHistPublishDeliveredList) {
            if(beginPage){
                beginPage = false;
                generateLayout(doc, cb);
                generateHeader(doc, cb,auditTrailHistRecentRec);
                y = 615; 
            }
            generateDetail(doc, cb, auditTrailHist, y);
            y = y - 15;
            if(y < 50){
                printPageNumber(cb);
                doc.newPage();
                beginPage = true;
            }
        }
        printPageNumber(cb);
    }
    catch (DocumentException dex) {
        dex.printStackTrace();
    }
    catch (Exception ex) {
        ex.printStackTrace();
    }
    finally {
        if (doc != null){
            doc.close();
        }
        if (docWriter != null) {
            docWriter.close();
        }
    }
} //end buildPdfDocument

Here is the error log:

2015-04-22 23:53:41.551 DEBUG   org.springframework.jdbc.datasource.DataSourceUtils [http-bio-8080-exec-3]  Returning JDBC Connection to DataSource
2015-04-22 23:53:41.552 DEBUG   org.springframework.beans.factory.support.DefaultListableBeanFactory    [http-bio-8080-exec-3]  Returning cached instance of singleton bean 'pdfDeliveryNoteView'
2015-04-22 23:53:41.552 DEBUG   org.springframework.web.servlet.DispatcherServlet   [http-bio-8080-exec-3]  Rendering view [com.xxxx.view.PDFDeliveryNoteBuilder: name 'pdfDeliveryNoteView'] in DispatcherServlet with name 'dispatcher'
2015-04-22 23:53:41.679 DEBUG   com.xxx.view.PDFDeliveryNoteBuilder [http-bio-8080-exec-3]  --> before dbRepository
2015-04-22 23:53:41.681 DEBUG   org.springframework.web.servlet.DispatcherServlet   [http-bio-8080-exec-3]  Error rendering view [com.xxxx.view.PDFDeliveryNoteBuilder: name 'pdfDeliveryNoteView'] in DispatcherServlet with name 'dispatcher'
java.lang.NullPointerException
    at com.xxxx.view.PDFDeliveryNoteBuilder.buildPdfDocument(PDFDeliveryNoteBuilder.java:48)
    at com.xxxx.view.AbstractPdfViewCustom.renderMergedOutputModel(AbstractPdfViewCustom.java:92)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)

Below the spring XML configuration. web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">

<display-name>xxx Web Application</display-name>
<description>xxx Web Application</description> 

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring-mvc-dispatcher.xml
        /WEB-INF/spring-jdbc.xml
        /WEB-INF/spring-security.xml
    </param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-mvc-dispatcher.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping> 

<error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/jsp/notfound.jsp</location>
</error-page>
<error-page>
    <error-code>403</error-code>
    <location>/WEB-INF/jsp/notauthorized.jsp</location>
</error-page>
<error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/WEB-INF/jsp/error.jsp</location>
</error-page>

<welcome-file-list>
    <welcome-file>/resources/index.html</welcome-file>
</welcome-file-list>

spring-mvc-dispatcher.xml:

<beans xmlns="http://www.springframework.org/schema/beans"   
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation=" 

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 

http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:component-scan base-package="com.xxx" />

<mvc:resources location="/resources/" mapping="/resources/**"/>

<mvc:annotation-driven />

<!-- <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /> -->

<bean id="xmlViewResolver"
    class="org.springframework.web.servlet.view.XmlViewResolver">
   <property name="location"><value>/WEB-INF/spring-mvc-views.xml</value>
   </property><property name="order" value="0" />
</bean>

<bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix"> <value>/WEB-INF/jsp/</value></property>
    <property name="suffix"> <value>.jsp</value></property>
    <property name="order" value="1" />
</bean>

<bean id="messageSource" 
    class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages" />
</bean>

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- set to 5 MB
    <property name="maxUploadSize" value="5242880"/>
    --> 
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="useDefaultSuffixPattern" value="false" />
</bean>

<!-- Validator beans  -->
<bean id="deliveryDataStoreCreateValidator" class="com.xxx.validator.DeliveryDataStoreCreateValidator" />
<bean id="deliveryDataStoreUpdateValidator" class="com.xxx.validator.DeliveryDataStoreUpdateValidator" />
<bean id="deliveryDataStoreAccessUpdateValidator" class="com.xxx.validator.DeliveryDataStoreAccessUpdateValidator" />
<bean id="reportTemplateCreateValidator" class="com.xxx.validator.ReportTemplateCreateValidator" />
<bean id="reportTemplateUpdateValidator" class="com.xxx.validator.ReportTemplateUpdateValidator" />
<bean id="reportTemplateAccessUpdateValidator" class="com.xxx.validator.ReportTemplateAccessUpdateValidator" />
<bean id="dataProfileIdFileValidator" class="com.xxx.validator.DataProfileIdFileValidator" />
<bean id="dataProfileIdValidator" class="com.xxx.validator.DataProfileIdValidator" />
<bean id="dataProfileCreateValidator" class="com.xxx.validator.DataProfileCreateValidator" />
<bean id="dataProfileUpdateValidator" class="com.xxx.validator.DataProfileUpdateValidator" />
<bean id="dataProfileAccessUpdateValidator" class="com.xx.validator.DataProfileAccessUpdateValidator" />
<bean id="userCreateValidator" class="com.xxx.validator.UserCreateValidator" />
<bean id="userUpdateValidator" class="com.xxx.validator.UserUpdateValidator" />

spring-mvc-views.xml:

<bean id="pdfDeliveryNoteView" class="com.xxx.view.PDFDeliveryNoteBuilder">

kenju
  • 5,866
  • 1
  • 41
  • 41
user3743058
  • 1
  • 1
  • 1
  • Post the null pointer exception trace. – Laerte Apr 22 '15 at 21:44
  • 1
    The NullPointer means: `RepositoryJdbcTemplate` is not correctly "autowired"! (Are both classes in component-scan ?) – xerx593 Apr 22 '15 at 23:16
  • The RepositoryJdbcTemplate is autowired in other places in the code the same way, and works fine. The difference is that I autowire in a view. Need I do additional XML configuration setup to use autowire in a view? – user3743058 Apr 23 '15 at 17:19
  • Post your controller code. You are also aware that you are duplicating all the beans in your `spring-mvc-dispatcher.xml` because tis file is being loaded twice? Your application basically takes more memory then needed because of unnecessary bean instances. – M. Deinum Sep 03 '15 at 08:31

0 Answers0