Skip to main content

CDI NULLPOINTER EXCEPTION

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
raysod
Offline
Joined: 2011-12-22

It seems that every injection that I do will compile fine but all injected variables throw a Null Pointer Exception when accessed at runtime. If I initialize the injected variables right before I use them, all is well, though. The app is as follows:

webPage accesses backing bean(ClaimQueryDisplay).
ClaimQueryDIsplay bean's submit method accesses service layer via ClaimQueryService's retrieveList method.
ClaimQueryService's retrieveList method access data access layer via ClaimQueryDAO's retrieveList method(ClaimQueryDAO injected via ClaimQueryDAOManager2 & ClaimQueryProducer2).
ClaimQueryDAO's retrieveList method accesses data via entityManager(injected via SureClaimDatabaseManager & DataBasePeoducer).

I have attached the code.

package com.maezetech.claim.claimquery;

import com.maezetech.beans.LoginBean;
import com.maezetech.date.DateFormatter;
import java.io.Serializable;
import java.util.GregorianCalendar;
import java.util.List;
import javax.annotation.*;
import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.inject.Named;

/**
*
* @author HOG
*/

@Named(value="ClaimQueryDisplay")
@ConversationScoped

public class ClaimQueryDisplay implements Serializable{

private String claimLoadFromDate;
private String claimLoadToDate;
private String carrierNumber;
private String user;
private String batchNumber;
private List claimList;
private ClaimQuery claimQueryEntity;

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

/*
* Creates the query string based on the current state of class(values held in local variables
* @return String retval - the query to execute
*/
private String createQueryString(){

String retval="";

return retval;
}

public String getClaimLoadFromDate(){
return this.claimLoadFromDate;
}

public void setClaimLoadFromDate(String newval){
this.claimLoadFromDate = newval;
}

public String getClaimLoadToDate(){
return this.claimLoadToDate;
}

public void setClaimLoadToDate(String newval){
this.claimLoadToDate = newval;
}

public String getCarrierNumber(){
return this.carrierNumber;
}

public void setCarrierNumber(String newval){
this.carrierNumber = newval;
}

public String getUser(){
return this.user;
}

public void setUser(String newval){
this.user = newval;
}

public String getbatchNumber(){
return this.batchNumber;
}

public void setBatchNumber(String newval){
this.batchNumber = newval;
}

public List getClaimList(){
return this.claimList;
}

public void setClaimList(List newList){
this.claimList = newList;
}

public void submitQuery(){
System.out.println(this.claimLoadFromDate+ " "+this.claimLoadToDate);
ClaimQueryService service = new ClaimQueryService();
List list = service.retrieveList("SELECT BILL_NUM,PAT_LNMA,UPLOAD_DT,CARR_NUM,USER, BATCH_NUM FROM BILL LIMIT 10");
setClaimList(list);

}

//This will setup original display with just recent claims(last 30 days) from current user and all others as
//requested by user input
@PostConstruct
public void setup(){
DateFormatter currDate = new DateFormatter(new GregorianCalendar(), DateFormatter.MM_DD_YYYY, "/");
claimLoadToDate = currDate.getFormattedDate();
claimLoadFromDate = currDate.addDays(-30).getFormattedDate();
FacesContext fCon = FacesContext.getCurrentInstance();
Application application = fCon.getApplication();
ELContext elCon = fCon.getELContext();
ValueExpression valExp = application.getExpressionFactory().createValueExpression(elCon, "#{LoginBean}",Object.class);
LoginBean lBean = (LoginBean) valExp.getValue(elCon);
user = lBean.getName();

System.out.println(this.toString());
}

public String toString(){

return this.getClass().getName()+":\n\t"+"claimLoadFromDate: "+claimLoadFromDate+"\n\t"+"claimLoadToDate: "+
claimLoadToDate+"\n\tuser: "+user+"\n\tbatchNumber: "+batchNumber+"\n\t";
}
}

package com.maezetech.claim.claimquery;

/**
*
* @author HOG
*/
import com.maezetech.claim.claimquery.cdi.ClaimQueryDAOManager2;
import com.maezetech.persistence.service.CollectionService;
import java.util.List;
import javax.inject.Inject;

public class ClaimQueryService implements CollectionService{

@Inject
@ClaimQueryDAOManager2
ClaimQueryDAO claimQueryDAO;

@Override
public void addList(List list) {
claimQueryDAO.addList(list);
}

@Override
public void removeList(List list) {
claimQueryDAO.removeList(list);
}

@Override
public void updateList(List list) {
claimQueryDAO.updateList(list);
}

@Override
public List retrieveList(String query) {
System.out.println("ClaimQueryDAO = "+claimQueryDAO);
// if(claimQueryDAO == null){claimQueryDAO = new ClaimQueryDAO();}
System.out.println("ClaimQueryDAO = "+claimQueryDAO);
return claimQueryDAO.retrieveList(query);
}
}

package com.maezetech.claim.claimquery;

/**
*
* @author HOG
*/
import com.maezetech.claim.Claim;
import com.maezetech.persist.CDI.database.SureClaimDatabaseManager;
import com.maezetech.persistence.dao.CollectionDAO;
import java.util.List;
import java.util.ListIterator;
import javax.enterprise.inject.Produces;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

/*
* Class to query Claims only - solely used to display limited info on a group of claims.
*/

public class ClaimQueryDAO implements CollectionDAO {

@Inject
@SureClaimDatabaseManager
private EntityManager entityManager;

/*
* Method will perform nothing
*/

@Override
public void addList(List list) {
}

/*
* Method will perform nothing
*/

@Override
public void removeList(List list) {
}

@Override
public List retrieveList(String querySyntax) {
Query query = entityManager.createQuery(querySyntax);
return query.getResultList();
}

/*
* method will perform nothing
*/
@Override
public void updateList(List list) {
}

public EntityManager getEntityManager(){
return this.entityManager;
}

public void setEntityManager(EntityManager em){
this.entityManager = em;
}
}

import com.maezetech.claim.claimquery.ClaimQueryDAO;
import javax.enterprise.inject.Produces;

/**
*
* @author HOG
*/
public class ClaimQueryDAOProducer2 {
@Produces
@ClaimQueryDAOManager2
private ClaimQueryDAO claimQueryDAO;

}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.maezetech.claim.claimquery.cdi;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;
import javax.inject.Qualifier;

/**
*
* @author HOG
*/
@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface ClaimQueryDAOManager2 {
}

package com.maezetech.persist.CDI.database;

/**
*
* @author HOG
*/
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class DataBaseProducer {

@Produces
@SureClaimDatabaseManager
@PersistenceContext(unitName="SureClaimDatabase", name="SureClaimDatabase")
private EntityManager entityManager;
}

package com.maezetech.persist.CDI.database;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.inject.Qualifier;

/**
*
* @author HOG
*/
@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface SureClaimDatabaseManager {
}