0

I did some coding : converter, dropdown list, List, ... I get a NullPointerException though I am sure that ALL USED OBJECT have been initialized.

Below are the new edited beens ans jsf (the converter is in the Fil_M_B bean and M_B stands for Managed Bean)

Could you please help me figure out whats going wrong now ?

Fil_M_B bean

@Named(value = "fil_M_B")
@SessionScoped

public class Fil_M_B implements Serializable {

@EJB
private FiliereFacadeLocal filiereFacade;

public FiliereFacadeLocal getFiliereFacade() {
    return filiereFacade;
}

private List<Filiere> list_fil;

public List<Filiere> getList_fil() {
    return list_fil;
}

@PostConstruct
public void list(){
    list_fil = filiereFacade.findAll();
}

// NOW THE CONVERTER

@FacesConverter(forClass = Filiere.class)
public static class FiliereControllerConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }
        Fil_M_B controller = (Fil_M_B) facesContext.getApplication().getELResolver().
                getValue(facesContext.getELContext(), null, "Fil_M_B");
        return controller.filiereFacade.find(getKey(value));
    }

    java.lang.String getKey(String value) {
        java.lang.String key;
        key = value;
        return key;
    }

    String getStringKey(java.lang.String value) {
        StringBuilder sb = new StringBuilder();
        sb.append(value);
        return sb.toString();
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof Filiere) {
            Filiere o = (Filiere) object;
            return getStringKey(o.getNomFiliere());
        } else {
            throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Filiere.class.getName());
        }
    }

}
/**
 * Creates a new instance of Fil_M_B
 */
public Fil_M_B() {
}

Spec_M_B

@Named(value = "spec_M_B")
@SessionScoped
public class Spec_M_B implements Serializable{

@EJB
private SpecialiteFacadeLocal specialiteFacade;

Specialite spec = new Specialite();

public Specialite getSpec() {
    return spec;
}

public void setSpec(Specialite spec) {
    this.spec = spec;
}

public void ADD(){
    specialiteFacade.create(spec);
}
/**
 * Creates a new instance of Spec_M_B
 */
public Spec_M_B() {
}

}

Jsf page

<f:view>
        <h:form>
            <h:panelGrid columns="2">
                <h:outputLabel value="Specialite" for="nomSpec" />
                <h:inputText id="nomSpec" value="#{spec_M_B.spec.nomSpec}" title="Nom Specialite" required="true" requiredMessage="Required!"/>
                <h:outputLabel value="Filiere" for="nomFiliere" />
                <h:selectOneMenu id="nomFiliere" value="#{spec_M_B.spec.nomFiliere}" title="Nom Filiere" required="true" requiredMessage="Required !">
                    <f:selectItems value="#{fil_M_B.list_fil}"/>
                </h:selectOneMenu>
            </h:panelGrid>
            <h:commandButton value="ADD" action="#{spec_M_B.ADD()}"/>
        </h:form>    
    </f:view> 

Could you please help me figure out whats going wrong ?

Stack trace Below is the stack trace I I see in my browser afetr clicking the ADD button

            java.lang.NullPointerException
                at TEST_3.Fil_M_B.access$000(Fil_M_B.java:27)
                at TEST_3.Fil_M_B$FiliereControllerConverter.getAsObject(Fil_M_B.java:59)
                at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:171)
                at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:201)
                at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:318)
                at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1045)
                at javax.faces.component.UIInput.validate(UIInput.java:975)
                at javax.faces.component.UIInput.executeValidate(UIInput.java:1248)
                at javax.faces.component.UIInput.processValidators(UIInput.java:712)

                at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
                at javax.faces.component.UIForm.processValidators(UIForm.java:253)
                at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
                at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
                at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1195)
                at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
                at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
                at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
                at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
                at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
                at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
                at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
                at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
                at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
                at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
                at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
                at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
                at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
                at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
                at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
                at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
                at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
                at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
                at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
                at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
                at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
                at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
                at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
                at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
                at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
                at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
                at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
                at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
                at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
                at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
                at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
                at java.lang.Thread.run(Thread.java:745)
Kurt Miller
  • 567
  • 1
  • 8
  • 23
  • 2
    Could you please state the exact line where the NullPointer occurs (mark it in the code as comment). – ctst Mar 14 '16 at 15:48
  • Have the `getAsString()` method return an empty string whenever a model value being passed as the last parameter to that method remains `null`. This is required by the JSF specification and follow the basic Java naming conventions. Working with Java EE and not following them is not a good sign or implication for you. – Tiny Mar 14 '16 at 16:18
  • @Tiny I don't understand. Both getAstring() and getAsObject() return null whenever their last parameter are null – Kurt Miller Mar 14 '16 at 16:25
  • @ctst I have added the stack trace showing the error code on the post. Do you see anything ? – Kurt Miller Mar 14 '16 at 16:26
  • @MichaelJohnson Not exactly what I meant, but now at least it is possible to conclude where the error might happen (the press the button thing). I meant to mark it in the code as comment, since we don't have line enumeration here, but it is ok now. The problem is, you didn't have initialised `SpecialiteFacadeLocal specialiteFacade;` in `Spec_M_B` but use it in your ADD-method (you try to call a method of it). Can you please confirm this. – ctst Mar 14 '16 at 16:32
  • 1
    Repeating the same thing : Returning an empty string in `getAsString()` is required by the JSF specification, when the supplied model value is `null` - apart from the exception you got on line number 59 in `getAsObject()`. – Tiny Mar 14 '16 at 16:36
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Kukeltje Mar 14 '16 at 16:37
  • @Kukeltje I would prefer flagging it as a typo/error which will never help any future readers but this applies to most NullPointerSearchImages. – ctst Mar 14 '16 at 16:40
  • @ctst: take a pick ;-) – Kukeltje Mar 14 '16 at 16:41
  • @Kukeltje: Ah, sorry, I thought you could read my mind (or my vote). I picked the should-be-closed/off-topic/error was solved in manner which doesn't help future readers :-) – ctst Mar 14 '16 at 17:16
  • @ctst Yes I confirm it. It does not nee to be "initialised" . SpecialiteFacadeLocal is an EJB (session bean created from entity classes) and therefore some how using the variable specialiteFacade allows me to access methods in the SpecialiteFacadeLocal. Create is one of these methods and it allows me to add data in a database. Beside how do you think I should initialise it (I used getter and setters but I have nothing to put in it) ? – Kurt Miller Mar 14 '16 at 18:25
  • @Tiny the line 59 is return controller.filiereFacade.find(getKey(value)) which from what I intended to do means if String value (which is the id) is not null, return an object of type Filiere using the EJB filiereFacade and its method Find(object id). ...from the Fil_M_B object (or class), we access the injected EJB FiliereFacadeLocal (thus the variable filiereFacade) and get to the Find method....How a null is popping out of there I dont get it. – Kurt Miller Mar 14 '16 at 18:37
  • Immediately above the return statement, `controller` of type `Fil_M_B` is `null`. In JSF, a managed bean can be obtained by `Bean bean = facesContext.getApplication().evaluateExpressionGet(facesContext, "#{beanName}", Bean.class);` but in your case, `Fil_M_B` is a CDI managed bean. Thus, it will not be available via `FacesContext` as the bean is not governed by JSF. Put OmniFaces on the project's class-path and inject the bean into the converter using `@Inject` (without OmniFaces, you will need a manual JNDI lookup). You could better inject the EJB `FiliereFacadeLocal` itself into converter. – Tiny Mar 14 '16 at 19:13
  • `@FacesConverter` and `@FacesValidator` are not qualified for injections. Thus, you will need a JNDI lookup (better use OmniFaces instead) or make the converter a managed bean using `@Named`, `@ApplicationScoped` (or `@RequestScoped`) but then you will have to specify the converter in XHTML everywhere (as `forClass = Filiere.class` will not be there). – Tiny Mar 14 '16 at 19:27
  • @Tiny This is getting too serious for me (I'm a beginner). If you could just post a sort of "pattern" or "template" for me I'll try and if that.... Also I don't quite know how to use Omnifaces in Netbean. I downloaded Omnifaces 2.2 JAR and added it to the library is that what I was suppose to do ? – Kurt Miller Mar 14 '16 at 22:05
  • @Tiny And I tried replacement in my bean with facesContext.getApplication().evaluateExpressionGet(facesContext, "#{beanName}", Bean.class); but it did not seem to do the trick . How would you do that (in which case no Omnifaces right ?) ? – Kurt Miller Mar 14 '16 at 22:14
  • You could directly inject the EJB `FiliereFacadeLocal` or `Fil_M_B` into the converter using `@Inject` given that you have OmniFaces on your project's class-path (no configuration is needed). – Tiny Mar 16 '16 at 08:27

0 Answers0