Linked Questions

Popular Questions

Consider the following code:

public class SampleTest {
    private CustomObject customObject;

    public void setUp() {
        customObject = new CustomObject();

    public void viewReady() {

Where customObject is created in the setUp method.

This code has a warning saying Not-null fields must be initialized when the @NonNull annotation is used - but this is the earliest point in the code that I can create it, so I'm not sure what I can do.

Ideally, I would like to avoid:

  1. suppressing the warning (as I find these warnings generally useful in other circumstances and don't want to miss anything)
  2. creating the object inline, i.e. private CustomObject customObject = new CustomObject (as there are more complex examples which require multiple objects as constructor params I have omitted)
  3. Using Nullable here since then it would require me to null check before using it (else suppress these warnings - also bad for the same reason in 1).

Is there a way to allow this annotation to be kept?

Related Questions