Thursday, April 28, 2022

[FIXED] How to hide NullPointer Warnings (due to NoArgsConstructor with force=true) in IntelliJ?

Issue

I have created a Java application using Dropwizard and Weld. I have also included Lombok and I have currently annotated my service classes like:

@Slf4j
@ApplicationScoped
@NoArgsConstructor(force = true)
@RequiredArgsConstructor(onConstructor = @__({@Inject}))
public class MovieRequestService {

    private final MovieService movieService;
    private final ActorService actorService;

It works fine and I am happy with it. The RequiredArgsConstructor is useful for my Unit tests. The NoArgsConstructor is necessary for Weld. force = true has to be set because of the final keywords... But this also leads to the fact that IntelliJ warns me on every single place where I use the movieService or actorService....

What should I do? Can I get rid of the warnings?


Solution

"Force=true" parameter of @NoArgsConstructor annotation means that fields are initialized to @Nullable value, so the warnings of "Constant conditions & exceptions" inspection (Java | Probable bugs | Constant conditions & exceptions) about possible NullPointerException are correct. You may disable the inspection's option "Report problems that happen only on some code paths" to get rid of the warning.



Answered By - Olga Klisho
Answer Checked By - David Goodson (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.