# NullPointerException bei CDI



## oppi (9. Mrz 2012)

Hallo zusammen. Ich arbeite mich gerade durch ein CDI Tutorial mit JSF, bekomme aber leider eine Nullpointerexception. Mein Service objekt ist NULL. 
Ich weiss echt nicht wieso, halte mich an alle Vorgaben. Arbeite mit Netbeans 7.0 und die beans.xml ist vorhanden. Wäre super wenn jemand eine idee  hätte

Gruß


```
<?xml version='1.0' encoding='UTF-8' ?>
<!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://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
  <h:head>
    <title>Facelet Title</title>
  </h:head>
  <h:body>
    <h:form>
      <p:commandButton value="PrimeFaces Button" />
    </h:form>
    <p:pieChart value="#{dataManager.pieModel}" legendPosition="w" /> 
  </h:body>
</html>
```


```
package de.oppermann.backend.data;

import de.oppermann.external.api.DataService;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.model.chart.PieChartModel;

@Named
@SessionScoped
public class DataManager implements Serializable {
  @Inject
  private DataService service;
  private PieChartModel pieModel;
  
  public DataManager() {
  }

  public PieChartModel getPieModel() {
    pieModel.setData(service.getData());
    return pieModel;
  }

  public void setPieModel(PieChartModel pieModel) {
    this.pieModel = pieModel;
  } 
  
}
```


```
package de.oppermann.external.api;

import java.util.Map;

public interface DataService {
  public Map<String, Number> getData();
}
```


```
package de.oppermann.external.implementations;

import de.oppermann.external.api.DataService;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class DataServiceImpl implements DataService, Serializable {
  private Map<String, Number> internalMap = new HashMap<String, Number>();
  
  @Override
  public Map<String, Number> getData() {
    internalMap.put("Java", 1234);
    internalMap.put("C++", 2234);
    internalMap.put("C", 1888);
    internalMap.put("PHP", 1555);
    internalMap.put("C#", 1000);
    return internalMap;
  }
  
}
```


----------



## krazun (9. Mrz 2012)

Als erstes Fällt auf das DataService garnicht mit @Named annotiert ist. Dadurch steht es nicht unter CDI Verwaltung und kann auch nicht gefunden werden.

grüße,
krazun


----------



## oppi (9. Mrz 2012)

hm.. davon war in dem tutorial nirgends die rede. Hab jetzt mal mit der Annotation am Interface probiert, doch leider noch derselbe Fehler. Hier mal der StackTrace:


```
java.lang.NullPointerException
	at de.oppermann.backend.data.DataManager.getPieModel(DataManager.java:21)
	at de.oppermann.backend.data.org$jboss$weld$bean-web-ManagedBean-class_de$oppermann$backend$data$DataManager_$$_WeldClientProxy.getPieModel(org$jboss$weld$bean-web-ManagedBean-class_de$oppermann$backend$data$DataManager_$$_WeldClientProxy.java)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at javax.el.BeanELResolver.getValue(BeanELResolver.java:302)
	at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
	at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
	at com.sun.el.parser.AstValue.getValue(AstValue.java:116)
	at com.sun.el.parser.AstValue.getValue(AstValue.java:163)
	at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
	at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:55)
	at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
	at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
	at org.primefaces.component.chart.UIChart.getValue(UIChart.java:73)
	at org.primefaces.component.chart.pie.PieChartRenderer.encodeData(PieChartRenderer.java:66)
	at org.primefaces.component.chart.pie.PieChartRenderer.encodeScript(PieChartRenderer.java:51)
	at org.primefaces.component.chart.pie.PieChartRenderer.encodeEnd(PieChartRenderer.java:36)
	at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
	at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1763)
	at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759)
	at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759)
	at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:401)
	at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
	at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
	at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
	at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:410)
	at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1534)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
	at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
	at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
	at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
	at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:326)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227)
	at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:170)
	at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
	at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
	at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
	at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
	at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
	at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
	at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
	at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
	at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
	at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
	at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
	at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
	at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
	at java.lang.Thread.run(Thread.java:662)
```


----------



## krazun (9. Mrz 2012)

du rufst pieModel.setData() auf obwohl das PieChartModel noch null ist. ein pieModel = new PieChartModel() fehlt da noch.

grüße,
krazun


----------



## oppi (9. Mrz 2012)

oh dankeschön, fast schon peinlich 

Also an das Interface muss definitiv auch ein @Named ?

Vielen Dank


----------

