4

I am using tomee server to run my javaEE app. I have written a filter which injects an object. However the object does not seem to get instantiated :

Following is my code :

Filter.java

@Priority(value = 2)
@Provider
@Singleton
public class Filter implements ContainerRequestFilter {


     @Inject
     private Faculty faculty;


     public void filter(ContainerRequestContext requestContext) {
         System.out.println("faculty name is :"+faculty.getFacultyName());
     }
}

Faculty.java

import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;

public class Faculty {

    private List<String> facultyMembers;
    private String facultyName;

    @PostConstruct
    public void initialize() {
        this.facultyMembers = new ArrayList<String>();
        facultyMembers.add("Ian Schultz");
        facultyMembers.add("Diane Reyes");
        facultyName = "Computer Science";
    }

    public List<String> getFacultyMembers() {
        return facultyMembers;
    }

    public String getFacultyName() {
        return facultyName;
    }
}

I am getting an NPE. Following is the stack trace :

java.lang.NullPointerException
at com.xyz.commerceapp.filters.Filter.filter(TransactionStartFilter.java:45)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.runContainerRequestFilters(JAXRSUtils.java:1645)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:201)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:77)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)

Here are the versions of the artifacts being used:

<openejb.version>5.0.0-SNAPSHOT</openejb.version>
<tomee.version>2.0.0-SNAPSHOT</tomee.version>

I have tried including the beans.xml in both META-INF and WEB-INF but I am still seeing the NPE.Can someone let me know what am I doing wrong ?

Suraj Menon
  • 1,486
  • 3
  • 28
  • 50

3 Answers3

1

The injection worked once I changed @Singleton to @ApplicationScoped.

Suraj Menon
  • 1,486
  • 3
  • 28
  • 50
0

Please see your imports , it should not be importing Priority from any other library. The correct one is this : import javax.annotation.Priority;

arjun99
  • 358
  • 1
  • 8
-2

Make sure your archive (jar, war, …) has a beans.xml file defined

The beans.xml file is known as the bean archive descriptor. Below you can find the most simple example with just a root beans xml element:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

This file should be placed into META-INF/beans.xml or WEB-INF/beans.xml. If you don’t include this file in your module archive, CDI injection will not be active for this archive as the container will not scan the archive.

There are a few reasons why the spec was done this way:

  • Performance: The container only has to scan the modules where you’ve added the beans.xml

  • beans.xml also lets you define interceptors and decorators that have to be executed when the injection takes place.

Courtesy: Solving @Inject NullPointerException

a3.14_Infinity
  • 5,653
  • 7
  • 42
  • 66