Results 1 to 5 of 5

Thread: newbie question MVC Step by step errors

  1. #1
    Join Date
    Feb 2005
    Location
    Minneapolis
    Posts
    3

    Default newbie question MVC Step by step errors

    I am working throught the MVC step by step exercise and I've encountered a null pointer exception when I try to reference
    getProdMan.getProducts() in the SpringTestController class (step 19). the List "products" never seems to be instantiated anywhere. My questions are:
    does spring "magically" create and populate this list from the data I put in the xml file? and if so where does it do that and where am I going wrong?

    Thanks!
    Tim

  2. #2
    Join Date
    Aug 2004
    Location
    Southampton, UK
    Posts
    826

    Default

    Can you post your configuration and an example of the problem in code please.

    Thanks

    Rob
    Rob Harrop
    Lead Engineer, dm Server
    SpringSource
    http://www.springsource.com

    Co-Author - Pro Spring

  3. #3
    Join Date
    Feb 2005
    Location
    Minneapolis
    Posts
    3

    Default

    robh,

    sorry to take so long to respond, I have been away.
    my configuration is as follows
    Tomcat 4.1 on Win2k

    my problem is that the List "products" never gets instantiated or populated and so I get a null pointer exception when trying to access it in the handleRequest method of the controller.

    When I put code in to instantiate and populate products, the problem goes away, but I get the impression this is supposed to be done automatically somehow and this is what I do not understand.

    The Controller:
    Code:
    public class SpringTestController implements Controller {
        protected final Log logger = LogFactory.getLog(getClass());
        private ProductManager prodMan= new ProductManager();
    
        public ProductManager getProdMan() {
            return prodMan;
        }
    
        public void setProdMan(ProductManager prodMan) {
    
            this.prodMan = prodMan;
        }
    
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            
            String now = (new java.util.Date()).toString();       
            Map myModel = new HashMap();
            myModel.put("now",now);
            // the nullPointer exception happens on this line:
            myModel.put("products",this.getProdMan().getProducts());
            return new ModelAndView("hello","model",myModel);
        }
    }
    The Product:

    Code:
    public class Product implements Serializable {
    
        private String description;
        private Double price;
    
        public Double getPrice() {
            
            return price;
        }
    
        public void setPrice(Double price) {
            this.price = price;
        }
    
        public void setDescription(String s){
            description = s;
        }
        public String getDescription(){
            return description;
        }
    }
    Here is productManager:
    Code:
    public class ProductManager implements Serializable{
    
        private ArrayList products;
    
        public ArrayList getProducts() {
    
            return products;
        }
    
        public void setProducts(ArrayList products) {
            this.products = products;
        }
    }

    here is the SpringTest-servlet.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <!--
    - Application context definition for "springapp" DispatcherServlet.
      -->
    
    <beans>
        <bean id="SpringTestController" class="SpringTestController"/>
    
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="/hello.htm">SpringTestController</prop>
                </props>
            </property>
        </bean>
    
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass">
                <value>org.springframework.web.servlet.view.JstlView</value>
    
            </property>
            <property name="prefix"><value>/WEB-INF/jsp/</value></property>
            <property name="suffix"><value>.jsp</value></property>
        </bean>
    
        <bean id="prodMan" class="ProductManager">
            <property name="products">
                <list>
                    <ref bean="product1"/>
                    <ref bean="product2"/>
                    <ref bean="product3"/>
                </list>
            </property>
        </bean>
    
        <bean id="product1" class="Product">
            <property name="description">
                <value>Lamp</value>
            </property>
        </bean>
    
        <bean id="product2" class="Product">
            <property name="description">
                <value>Battery</value>
            </property>
        </bean>
    
        <bean id="product3" class="Product">
            <property name="description">
                <value>Valve</value>
            </property>
        </bean>
    
        <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename">
                <value>messages</value>
            </property>
        </bean>
    
    </beans>
    and, just in case, web.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http&#58;//java.sun.com/dtd/web-app_2_3.dtd'>
    
    <web-app>
        <servlet>
            <servlet-name>SpringTest</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>SpringTest</servlet-name>
            <url-pattern>*.htm</url-pattern>
        </servlet-mapping>
    
        <welcome-file-list>
            <welcome-file>
          index.jsp
            </welcome-file>
        </welcome-file-list>
    </web-app>
    Tim

  4. #4
    Join Date
    Aug 2004
    Location
    Boston MA
    Posts
    27

    Default Inject ProdMan into your Controller

    Looks like you forgot to inject your Product Manager into your controller. This is why you are gettting an NPE.

    Replace this:
    Code:
    <bean id="SpringTestController" class="SpringTestController"/>
    With this:
    Code:
    <bean id="SpringTestController" class="SpringTestController">
      <property name="prodMan"><ref bean="prodMan"/></property>
    </bean>
    And you should be good to go.


    Keller

  5. #5
    Join Date
    Feb 2005
    Location
    Minneapolis
    Posts
    3

    Default

    Fabulous! It is working now. I must have checked that 6 times and I still messed it up when copying.
    Tim

Similar Threads

  1. java.util.Properties Silly Newbie Question
    By robbiest in forum Container
    Replies: 4
    Last Post: Oct 25th, 2011, 05:49 AM
  2. Replies: 11
    Last Post: Jul 23rd, 2007, 02:09 AM
  3. Replies: 3
    Last Post: Jul 8th, 2005, 09:00 AM
  4. Spring MVC step by step Question
    By syed_rizvi in forum Web
    Replies: 5
    Last Post: Jun 23rd, 2005, 08:36 PM
  5. Replies: 3
    Last Post: Apr 3rd, 2005, 04:34 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
  •