2017-08-24 73 views
1

我在JSF 2.2項目有sessionScoped ManagedBean,我想調用另一個ManagedBean@ManagedProperty這是我第sessionScoped豆:JSF ManagedProperty NullPointerException異常

public class MainWorkerMB implements Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 


    @ManagedProperty("#{fragmentHandlerMB}") 
    private FragmentHandlerMB fragmentHandlerMB; 

    public FragmentHandlerMB getFragmentHandlerMB() { 
     return fragmentHandlerMB; 
    } 

    public void setFragmentHandlerMB(FragmentHandlerMB fragmentHandlerMB) { 
     this.fragmentHandlerMB = fragmentHandlerMB; 
    } 

    public void search() { 

     fragmentHandlerMB.changeFrag("search_result.xhtml" , "Result Page" , -1); 

    } 

這是我的另一個bean這也是sessionScoped

@ManagedBean(eager=true) 
@SessionScoped 
public class FragmentHandlerMB implements Serializable { 

@PostConstruct 
    public void init() { 
     log.info("constructed.."); 
     // other code parts // 

    } 

    public FragmentHandlerMB() { 
     super(); 
    } 

public void changeFrag(String fragToOpen, String fragToOpenName, int pageIndex) { 

//... 

} 
} 

後,當我嘗試調用我謝勝利豆方法我得到了NullPointerException

Caused By: java.lang.NullPointerException 
    at com.kebodev.managed.MainWorkerMB.search(MainWorkerMB.java:29) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    Truncated. see log file for complete stacktrace 

所以它看起來像我的bean不存在。

你能幫我嗎? 謝謝!

回答

1

我解決了這個問題,通過在faces-config.xml刪除整個<managed-bean>配置和添加@ManagedBean @SessionScoped anotation我都ManagedBean.