0

When using JSF with Tomcat on eclipse, a null pointer is always returned. This project is on IP tracking, and it is required to inject another bean into the application bean.

Bean to be injected: track bean using the injection: increaseCount

The Track class

import java.util.HashMap;
import java.util.Map;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

@Named(value = "track")
@ApplicationScoped
public class Track {
    private Map<String, Integer> map = new HashMap<>();


    public void add(String ipAddress) {
        map.put(ipAddress, map.containsKey(ipAddress) ? map.get(ipAddress) + 1 : 1);
    }

    public int getCount(String ipAddress) {
        return map.containsKey(ipAddress) ? map.get(ipAddress) : 0;
    }

    public String getAllCount() {
        return "Count summary is " + map;
    }

}

The IncreaseCount Class

I tried following the solution indicated here but encountered the same problem.

httpss://stackoverflow.com/questions/16399974/nullpointerexception-while-trying-to-access-inject-bean-in-constructor
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

@Named(value = "increaseCount")
@SessionScoped
public class IncreaseCount implements java.io.Serializable {
    @Inject
    private Track track;
    private String ipAddress;

    @PostConstruct
    public void init() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
                .getRequest();
        this.ipAddress = request.getRemoteAddr();
    }

    /*
     * public IncreaseCount() { HttpServletRequest request = (HttpServletRequest)
     * FacesContext.getCurrentInstance().getExternalContext() .getRequest();
     * this.ipAddress = request.getRemoteAddr(); }
     */

    public void click() {
        track.add(ipAddress);
    }

    public String getIpAddress() {
        return ipAddress;
    }

    public int getCount() {
        return track.getCount(ipAddress);
    }
}

The XHTML page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
    <title>IncreaseCount</title>
</h:head>
<h:body>
    <h:form>
        <h:commandButton action="#{increaseCount.click()}" value="Click Me" />
        <br>The current count is #{increaseCount.getCount()} and your IP address is #{increaseCount.getIpAddress()}.</br>
    </h:form>
</h:body>
</html> 

The Exception

As you can see, invoking the getCount() method in the Track class produces an Exception.

Root Cause
java.lang.NullPointerException
    chapter39.IncreaseCount.getCount(IncreaseCount.java:39)
    java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.base/java.lang.reflect.Method.invoke(Method.java:567)
    javax.el.BeanELResolver.invoke(BeanELResolver.java:158)
    javax.el.CompositeELResolver.invoke(CompositeELResolver.java:79)
    org.apache.el.parser.AstValue.getValue(AstValue.java:159)
    org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:190)
    com.sun.faces.facelets.el.ELText$ELTextVariable.writeText(Unknown Source)
    com.sun.faces.facelets.el.ELText$ELTextComposite.writeText(Unknown Source)
    com.sun.faces.facelets.compiler.TextInstruction.write(Unknown Source)
    com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(Unknown Source)
    com.sun.faces.facelets.compiler.UILeaf.encodeAll(Unknown Source)
    javax.faces.render.Renderer.encodeChildren(Unknown Source)
    javax.faces.component.UIComponentBase.encodeChildren(Unknown Source)
    javax.faces.component.UIComponent.encodeAll(Unknown Source)
    javax.faces.component.UIComponent.encodeAll(Unknown Source)
    javax.faces.component.UIComponent.encodeAll(Unknown Source)
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(Unknown Source)
    com.sun.faces.application.view.MultiViewHandler.renderView(Unknown Source)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(Unknown Source)
    com.sun.faces.lifecycle.Phase.doPhase(Unknown Source)
    com.sun.faces.lifecycle.LifecycleImpl.render(Unknown Source)
    javax.faces.webapp.FacesServlet.service(Unknown Source)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
Kennedy
  • 27
  • 8

1 Answers1

-1
For JSF-based bean definitions:-
    javax.faces.bean.SessionScoped //for bean scoping
    javax.faces.bean.ManagedBean //for bean declaration
    javax.faces.bean.ManagedProperty //for bean injection
@ManagedProperty(value="#{yourBeanName}")
private Track track;

For Better Understanding Please Go through this link

k381
  • 49
  • 3