0

I am trying to init object with default if not initialized by the user. I want the user be able not to add params in the request but I will have it with the default value.

I'm not sure what I am missing but I get Null Pointer exception.

Having this object

@Builder
@Data
@Valid
@RequiredArgsConstructor
@AllArgsConstructor
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
public class Request {
    private final List<Author> authors;
    private final Config config;

    private Params params;
}


@Builder
@AllArgsConstructor
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
@Data
@Setter(AccessLevel.NONE)
public class Params {

  @Builder.Default private boolean adultOnly = false;
}


fun main(args: Array<String>) {
    val request = Request(
            authors,
            config(
                key,
                level
            )
        )

also using builder throw null exception

val request = QueriesRequest.builder()
            .userQueries(userQueries)
            .configurationKey(
                QueryConfigurationKey(configurationKey,
                    granularityLevel))
            .site(site).build()

request.params.adultOnly // throw NULL , I expected to have the default false.
}

What I am missing?

daniu
  • 14,137
  • 4
  • 32
  • 53
userit1985
  • 961
  • 1
  • 13
  • 28
  • `params` is never set to a value, it is still null – knittl Dec 01 '22 at 20:50
  • Does this even compile? – Christoph Dahlen Dec 01 '22 at 20:50
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – knittl Dec 01 '22 at 20:50
  • @ChristophDahlen, sure. I wondering if there is a syntax of the Builder I am missing for initialize Params with default value if not given – userit1985 Dec 01 '22 at 20:57
  • @knittl I thought if there is a default Builder to initialize this Params if not given by the user. – userit1985 Dec 01 '22 at 21:01
  • @userit1985 your question is tagged [tag:java], but the code looks like [tag:kotlin]. Which one is it? – knittl Dec 01 '22 at 21:03
  • @userit1985 you are not using the builder, you are calling the all-args constructor. – knittl Dec 01 '22 at 21:03
  • @knittl model objects in java while test in kotlin – userit1985 Dec 01 '22 at 21:31
  • @userit1985 `params` is null. The default value of `adultOnly` does not matter, because `params` does not have a Lombok default value; it is never initialized. `params` (of `Request`) is null, not `adultOnly` (`adultOnly` is a boolean, so it cannot ever be null in the first place). – knittl Dec 01 '22 at 21:37
  • @knittl I tried to have Builder.Default on params in the request but I am getting compilation error. How can I have a default for params? – userit1985 Dec 03 '22 at 19:41
  • @userit1985 if you get an error, you should tell us what the error. You should [edit] your question to include additional details, e.g. the new approach that you have tried and the error that you encounter. We are not wizards and cannot guess what your error might have been. – knittl Dec 04 '22 at 08:51

1 Answers1

0

The params field in the Request class is never initialized and remains null. You could set a default value for it:

@Builder
@Data
@Valid
@RequiredArgsConstructor
@AllArgsConstructor
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
public class Request {
    private final List<Author> authors;
    private final Config config;

    @Builder.Default
    private Params params = Params.builder().build();
}

I'm using the Params' builder itself to initialize the default value, so that you will get the default values as defined by that builder as well.

knittl
  • 246,190
  • 53
  • 318
  • 364