Announcement Announcement Module
Collapse
No announcement yet.
Avoiding NPE in the controller while setting up a reference. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Avoiding NPE in the controller while setting up a reference.

    Hi,
    I was under the impression that the bean configuration ,for my example below "worklistManager", will provide me with the valid instance of the object on the controller and hence i can call the getXXX methods on the object.

    Code:
    public class LandingPageController extends MultiActionController {
    protected final Log logger = LogFactory.getLog(getClass());
    private WorklistManager worklistManager;
    public ModelAndView handleRequest(HttpServletRequest request,
    			HttpServletResponse response) throws Exception {
    	String now = (new java.util.Date()).toString();
    	List<WorklistItem> witems = worklistManager
    			.getAllWorklistItemsByUser("");
    
    	Map<String, Object> myModel = new HashMap<String, Object>();
    	myModel.put("now", now);
    	myModel.put("witems", witems);
    	return new ModelAndView("landingpage", "model", myModel);	
    }
    
    public void setWorklistManager(WorklistManager worklistManager) {
    		this.worklistManager = worklistManager;
    }
    }
    I keep getting a null pointer on the following line

    List<WorklistItem> witems = worklistManager
    .getAllWorklistItemsByUser("");


    In my config file i have the following

    Code:
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
            <property name="url" value="JDBC_URL"/>
            <property name="username" value="USER_NAME"/>
            <property name="password" value="PASSWORD"/>
        </bean>
    	
    	<bean id="worklistManager" class="eirapp.service.WorklistManagerImpl">
        	<property name="dataSource" ref="dataSource" />
        </bean>
    	
    	<bean id="landingpageController" class="eirapp.web.LandingPageController">
    		<property name="worklistManager" ref="worklistManager" />
    	</bean>
    Is my understanding incorrect? Can any one suggest what i need to do in the controller code to avoid a NPE.

    Thanks,

  • #2
    never mind. I did not instantiate the controller correctly. The corrected format helped me fix the NPE
    Code:
    	<bean id="worklistManager" class="eirapp.service.WorklistManagerImpl">
        	<property name="dataSource" ref="dataSource" />
        </bean>
    	
    	<bean name="/landingpage.htm" class="eirapp.web.LandingPageController">
    		<property name="worklistManager" ref="worklistManager" />	</bean>

    Comment

    Working...
    X