Announcement Announcement Module
Collapse
No announcement yet.
Null Pointer Exception - newbie Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Null Pointer Exception - newbie

    sorry, hit the wrong button
    Last edited by openski; Jul 12th, 2007, 07:24 PM.

  • #2
    Originally posted by openski View Post
    Hello everybody,

    I am currently learning spring ( version 2.0.3 ) and getting a null pointer exception.
    I have the following bean definitions in an application context:

    Code:
        
        <bean id="manager" class="business.Manager">
       
        <bean id="defaultController" class="controller.DefaultController">
            <property name="manager">
            	<ref bean="manager"/>
            </property>
        </bean>
    
        <bean id="firstController" class="controller.FirstController"/>
    The DefaultController class has the getter and setters for the manager.
    The FirstController class looks like :

    public class FirstController extends DefaultControllor implements Controller {

    public ModelAndView handleRequest (
    HttpServletRequest request, HttpServletResponse response) {


    }
    NullPointer exception occurs because in the FirstController you dont have the manager property set.

    DefaultControllor and FirstController beans are different object instances according to the configuration.

    If FirstController is the child of DefaultController than you only need to configure the FirstController in the xml file as shown below


    <bean id="manager" class="business.Manager">

    <bean id="firstController" class="controller.FirstController">
    <property name="manager">
    <ref bean="manager"/>
    </property>
    </bean>

    Comment


    • #3
      Understood.

      Thanks for such a quick response.

      Comment


      • #4
        You wrote : the two beans are different object instances,
        So how do I configure them as an heirarchy such that I don't have to repeatedly reference the manager in each of the child controllers ?

        thanks again.

        Comment


        • #5
          Originally posted by openski View Post
          You wrote : the two beans are different object instances,
          So how do I configure them as an heirarchy such that I don't have to repeatedly reference the manager in each of the child controllers ?

          thanks again.
          In the configuration if you have 1 or more controllers that need reference to the manager, then in you will need to specify that in the config files for each controller explicitly.

          Maybe telling spring to inject all the depencies using auto-wire mode perhaps. But i normally turn the auto-wire off and specify properties in the config files as we dont want everything to be setup magically.

          I also use the afterPropertiesSet method to check if all the required objects are setup.

          Comment

          Working...
          X