Results 1 to 5 of 5

Thread: Null Pointer Exception - newbie

  1. #1
    Join Date
    Jul 2007
    Posts
    11

    Default Null Pointer Exception - newbie

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

  2. #2

    Default

    Quote 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>

  3. #3
    Join Date
    Jul 2007
    Posts
    11

    Default

    Understood.

    Thanks for such a quick response.

  4. #4
    Join Date
    Jul 2007
    Posts
    11

    Default

    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.

  5. #5

    Default

    Quote 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.

Posting Permissions

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