0

In a Java Web Project(JSF+Spring), i want to do this:

i have a search box in index.xhtml, i want that when i click search button, a method called from a class, and then result get's back:

this is index.xhtml:

<h:body>
    <h:form>
        <h:inputText value="#{searchBean.phrase}"></h:inputText>
        <h:commandButton value="Search" action="result"></h:commandButton>
    </h:form>
</h:body>

this is result.xhtml:

<h:body>
    #{searchBean.result}
</h:body>

this is searchBean.java:

@ManagedBean
@SessionScoped
public class SearchBean {

    private String phrase;
    private String result;

    private SearchClass searchObject;


    public String getPhrase(){
        return this.phrase;
    }

    public void setPhrase(String value){
        this.phrase = value;
    }


    public String getResult(){
        this.result = searchObject.search(getPhrase()); // here throws to an error
        return this.result;
    }

    public void setResult(String value){

    }
}

and this is searchClass:

public class SearchClass{

    public String search(String searchPhrase)
    {
        return "this is a sample result";
    }
}

but when i run this, this sections throws to an error:

public String getResult(){
        this.result = searchObject.search(getPhrase()); // here throws to an error
        return this.result;
    }

it seems calling a method from another class not allowed. this is error 500 that i get:

java.lang.NullPointerException
    com.media.search.web.controller.SearchBean.getResult(SearchBean.java:35)

i just want to get some information from a method, whats the wrong here? should i do something in xml config files related to JSF or Spring?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Fcoder
  • 9,066
  • 17
  • 63
  • 100
  • 1
    Where is searchObject instantiated? If you want the container to create it you must inject it – Jaqen H'ghar Oct 27 '15 at 18:26
  • it seems my problem is related to instantiation of searchObject. i use a interface. i do this and it works: public SearchService searchService = new SearchServiceImpl(); – Fcoder Oct 27 '15 at 18:36

0 Answers0