question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inject ApplicationScoped inside ApplicationScoped throws NullPointerException

See original GitHub issue

Describe the bug

Basically: I want the Controller class to have a generic IUseCase (constructor injected) and the UseCase to have a generic IService (constructor injection).

Controller -> UseCase -> Server Controller -> UseCase -> MockServer Controller -> UseCaseMock

public class Controller {
    private final IUseCase useCase;

    @Inject
    public Controller(IUseCase getUseCase) {
        this.useCase = getUseCase;
    }
@ApplicationScoped
public class UseCase implements IUseCase {

    private IService service;

    @Inject
    public UseCase(IService webService) {
        this.service = webService;
        service.init();
    }
}
public class UseCaseMock implements IUseCase {
...
}
@ApplicationScoped
public class Service implements IService {
...
}
public class ServiceMock implements IService {
...
}

Expected behavior

When running the application, because of the @ApplicationScoped annotation, I expect that: Controller has the UseCase constructor injected and that the UseCase has Service constructor injected.

Actual behavior

Method threw ‘java.lang.NullPointerException’ exception. Cannot evaluate …usecases.impl.UseCase_ClientProxy.toString()

it didnt inject it, even tho it was annotated with ApplicationScoped.

How to Reproduce?

In a nutshell: have a class which constructor injects an ApplicationScoped class which constructor injects an ApplicationScoped class.

public class Controller {
    private final IUseCase useCase;

    @Inject
    public Controller(IUseCase getUseCase) {
        this.useCase = getUseCase;
    }
@ApplicationScoped
public class UseCase implements IUseCase {

    private IService service;

    @Inject
    public UseCase(IService webService) {
        this.service = webService;
        service.init();
    }
}
public class UseCaseMock implements IUseCase {
...
}
@ApplicationScoped
public class Service implements IService {
...
}
public class ServiceMock implements IService {
...
}

Output of uname -a or ver

No response

Output of java -version

openjdk version “1.8.0_312”

GraalVM version (if different from Java)

No response

Quarkus version or git rev

2.4.2.Final

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.8.5

Additional information

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
geoandcommented, Oct 26, 2022

I see that Controller is not annotated with a CDI annotation. How are you creating / using it?

0reactions
mkoubacommented, Nov 8, 2022

I’m going to close this issue. @vinland-saga Feel free to reopen if you find a way to reproduce the NPE.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - @Inject, @ApplicationScoped - null pointer exception
I'm tring to inject a bean into my servlet but always get null pointer exception when run this app in tomcat8 package di;...
Read more >
NullPointer when working with Events in CDI - CodeRanch
It is ApplicationScoped becuase the data it holds is fairly small and is only very rarely updated. So whenever a "Status" is changed,...
Read more >
Contexts and Dependency Injection - Quarkus
A normal scoped bean ( @ApplicationScoped , @RequestScoped , etc.) is needed when a method is invoked upon an injected instance (contextual reference...
Read more >
NullPointerException for REST client in native build #10249
However a NullPointerException is thrown when we call a method defined in the REST client when the application is built as a native...
Read more >
PI63955: NULLPOINTEREXCEPTION THROWN BY WELD ...
The following exception may occur for CDI managed beans that contain either @EJB or @Resource injection: Caused by: java.lang.NullPointerException at ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found