question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Joiner.join(Object[]) doesn't accept null but is annotated with @Nullable

See original GitHub issue

a4baad35 introduces @Nullable at Joiner.join(@Nullable Object[] parts) but actual code would throw a NullPointerException.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
jbduncancommented, Oct 2, 2021

@xihongshi6 In this situation, the right thing to do is to annotate the array like so:

public void someMethod(Object @Nullable [] array) {
}

Here, it tells us that the array itself (the [] part) can be null, whereas the elements inside cannot.

Likewise, to say that both the array itself and its elements can be null, one would do this:

public void someMethod(@Nullable Object @Nullable [] array) {
}

I agree that it is ambiguous! But I think it’s a limitation of how arrays and annotations work with each other in Java.

If you have any more Guava questions, may I suggest posting them over at StackOverflow? I look at the <kbd>guava</kbd> tag there often, so if you post with that tag, then I’ll be likely to see it. Alternatively, if you have any questions that are just Java-related, post them under the <kbd>java</kbd> tag (I don’t monitor questions with that tag), or send me an email. You can find my email address on my GitHub profile. 😃

2reactions
jbduncancommented, Oct 2, 2021

@xihongshi6 I don’t know about others, but it’s a rule that I understand anyway.

All it is saying is the array cannot be null, but any of the objects in the array can be null.

The reason why is because the @Nullable annotation is annotating the Object part of the array, rather than the [] part.

So this is allowed:

Joiner.on(" ").join(new Object[]{ 1, null, "cat" });

But this is not allowed:

Joiner.on(" ").join(null);

I hope this helps. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does string.Join(" ", new object[] { null, "a ... - Stack Overflow
1 Answer 1 ... If the first element of values is null, the Join(String, Object[]) method does not concatenate the elements in values...
Read more >
Source code - Guava
Entry; 029import javax.annotation.CheckForNull; 030import org.checkerframework.checker.nullness.qual.Nullable; 031 032/** 033 * An object which joins pieces ...
Read more >
guava/Joiner.java at master - GitHub
import javax.annotation.CheckForNull;. import org.checkerframework.checker.nullness.qual.Nullable;. /**. * An object which joins pieces of text (specified ...
Read more >
@Nullable and @NotNull | IntelliJ IDEA Documentation
@Nullable and @NotNull annotations let you check nullability of a variable, parameter, or return value. They help you control contracts ...
Read more >
Avoid Check for Null Statement in Java - Baeldung
Learn about the Null Object Pattern and how to implement it in Java ... the null contract through the @Nullable and @NonNull annotations....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found