Skip to content

Conversation

@Febell
Copy link
Contributor

@Febell Febell commented Jul 23, 2025

If we return an optional and the first element of the list exists but the value is null the optional throws an exception like:

java.lang.NullPointerException: null
	at java.base/java.util.Objects.requireNonNull(Objects.java:233)
	at java.base/java.util.Optional.of(Optional.java:113)

So we should return an empty optional if the first element of the list exists but the value is null.

Return an empty optional if the first element of the list exists but the value is null.
@zach-m
Copy link
Owner

zach-m commented Nov 2, 2025

A list with null value should not happen. We need to fix the source of the problem.
Can you provide an example when this happens?
It seems that a better approach would be to prevent adding nulls to the list when it is initialized.

@Febell
Copy link
Contributor Author

Febell commented Nov 10, 2025

Sure I can do that. I attached a file with a real life ONIX file stripped down to the trouble causing book:

GTUPD00139360.XML

You can cause the error by calling:

try (InputStream inputStream = Files.newInputStream(Path.of("GTUPD00139360.XML"))) {
   List<JonixRecord> records = Jonix.source(inputStream).stream().toList();
   for (JonixRecord record : records) {
     Jonix.toBaseProduct(record);
   }
}

The exception I get is located at the code-line for:

dest.editionType = product.editionTypeCodes().firstValue().orElse(null);

Hope that helps. Sorry that I can not pinpoint it more, but I not familiar enough with ONIX to find it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants