A user reports that our static analyser reports a false-positive in this #Java code. Indeed, it looks like a false-positive. By specification, TreeMap::higherKey can return null if there's no higherKey. Is it the bug in the analyzer?

Not so fast. Due to implicit type conversion inside conditional operator, the result of `map.higherKey(key)` first converted to `int`, and then back to `Integer`. If we make boxing and unboxing explicit, it will become the following:

Integer result = Integer.valueOf(
map.containsKey(key) ?
key :
map.higherKey(key).intValue());

The `result` variable indeed cannot be null. If `higherKey` returns null, you will have a NullPointerException instead. This case is covered in mistake 6 "Implicit type conversion in conditional expressions" of my book "100 Java Mistakes" https://mng.bz/671p
100 Java Mistakes and How to Avoid Them

Dodge the common mistakes that even senior developers make, take full advantage of static analysis tools, and deliver robust and error-free Java code.</b> Whenever you make a mistake writing Java, it’s almost guaranteed that someone else has made it before! In 100 Java Mistakes and How To Avoid Them</i> you’ll learn about the common and the not-so-common antipatterns, errors, and tricky bits that trip up almost every Java developer. Inside 100 Java Mistakes and How To Avoid Them</i> you will learn how to: Write better Java programs</li> Recognize common mistakes during programming</li> Create fewer bugs and save time for debugging and testing</li> Get help from static analyzers during programming</li> Configure static analysis tools to reduce the number of false reports</li> Extend static analysis tools with custom plugins</li> </ul> Each Java mistake in this handy guide comes with an illustrative code sample, an explanation of why the mistake occurs, and an actionable “ways to avoid this” section to help you dodge the error. Plus, you’ll benefit from useful static analysis sidebars that let you know when mistakes will—and won’t—be spotted by static analysis tools.

Manning Publications
If you think that static analyzer is wrong, check it twice. Ask colleagues to review your code. Prepare a sample that disproves the static analyzer warning and actually run it.
@tagir_valeev hey Tagir, I have been following you for a while now, and in my current job I will start writing #cshap after a long time, do you know any c# developer at your caliber? I wonder if there's a book like yours in c#
@Ozzy Hello! Sorry, but I know very little about the C# language and .Net ecosystem in general, so cannot recommend anything specific.
@Ozzy @tagir_valeev I recommend @jonskeet and his book C# in Depth. Also @ericlippert. I follow a lot of C# devs. Welcome!

@KirillOsenkov @tagir_valeev @jonskeet @ericlippert thank you Kirill,
today I will start reading c# in depth 🤓

any chance do you know a site like javabubble.org for c# , just to find who to follow easily

@Ozzy @tagir_valeev @jonskeet @ericlippert check out https://dotnet.social, follow the #dotnet hashtag and start with the list of people I follow :) I follow a lot of .NET devs.
dotnet.social

<code>Console.WriteLine("Hello W🌍rld!");</code> .NET Community on Mastodon; not affiliated with Microsoft. We welcome all people interested in .NET.

Mastodon hosted on dotnet.social
dotnet.social

<code>Console.WriteLine("Hello W🌍rld!");</code> .NET Community on Mastodon; not affiliated with Microsoft. We welcome all people interested in .NET.

Mastodon hosted on dotnet.social
FediDevs | FAQ

Frequently Asked Questions

💡𝚂𝗆𝖺𝗋𝗍𝗆𝖺𝗇 𝙰𝗉𝗉𝗌📱 (@[email protected])

1/5 #followfriday Guide to Mastodon for #dotnetmaui and #dotnet peeps. Firstly, if you're not on https://dotnet.social why not? 😂 Run by the awesome @styx31 if you want a cool handle (and cool place to hang out). 🙂 Next, if you haven't already, you want to go to https://fedifinder.glitch.me - it will export a CSV of the people you follow, which you then import into Mastodon and it finds whoever it can from that list and adds you as a follow to them. Also follow #dotnet and #dotnetmaui

dotnet.social