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

    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 :

    Code:
    public class FirstController extends DefaultControllor implements Controller {
    
    public ModelAndView handleRequest ( 
       HttpServletRequest request, 
       HttpServletResponse response) {
    
       Manager mgr = getManager();
       mgr.myMethod();  
    
    }
    }
    Now when I access the myMethod from Manager class, it throws a null pointer. Am quite sure am doing some stupid, any suggestions ?
    Shouldn't the applicationContext intialize the manager object from the super class ?

    thanks

  • #2
    You have actually two beans. One of type DefaultController with the manager set and one of class FirstController where the manager is not set.

    I guess you wish to use one bean as template for the other, which might be done like this:
    Code:
    <bean id="manager" class="business.Manager">
       
        <bean id="defaultController" abstract="true">
            <property name="manager">
            	<ref bean="manager"/>
            </property>
        </bean>
    
        <bean id="firstController" parent"defaultController" class="controller.FirstController"/>
    Now you have only one bean which derives attributes from its abstract parent.

    Comment


    • #3
      Andreas, thats exactly was my question.

      Thank you

      Comment

      Working...
      X