Skip to content
Advertisement

Java mutable object to causing nullpointer exception

I have the following DTO and I pass the objects to the ArrayLists to prevent objects to be changed and fix the SonarQube error as “Message: Store a copy of allergenInfoList”, etc.

JavaScript

However, this approach requires null check and it makes my code ugly as shown below:

JavaScript

So, is there any better approach to fix the problem without null check?

Advertisement

Answer

It may be better to implement a utility/helper method to handle null checks (either directly, using Objects::isNull or Optional) and return expected result:

JavaScript

Then update the DTO code as needed:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement