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.

"Unbalanced read/write with collections" hint fails for fields initialized in constructor

See original GitHub issue

Apache NetBeans version

Apache NetBeans 14

What happened

I’ve a Maven project of type jar. Suppose that I also have a class with a single field of type List. Then, I initialize that field in the declaration itself. If I never add any element to that collection, NetBeans properly warns me that The collection is never added to.

However, if I initialize the field in the constructor, NetBeans seems to be unable to detect that I still have not added any element to the collection.

How to reproduce

In this piece of code, NetBeans correctly detects that no element has been added to the collection field:

public final class Test {

	private final List<String> collection = new ArrayList<> ();
                           // ^^^ WARNING! The collection is never added to.

	public Test () {
	}

	public boolean isEmpty () {
		return collection.isEmpty ();
	}

}

However, if I initialize the field in the constructor, the hint is never shown:

public final class Test {

	private final List<String> collection;

	public Test () {
		collection = new ArrayList<> ();
	}

	public boolean isEmpty () {
		return collection.isEmpty ();
	}

}

Did this work correctly in an earlier version?

No

Operating System

Debian GNU/Linux 11.4 (Bullseye)

JDK

OpenJDK Runtime Environment (build 17.0.3+7-Debian-1deb11u1)

Apache NetBeans packaging

Apache NetBeans binary zip

Anything else

No response

Are you willing to submit a pull request?

No

Code of Conduct

Yes

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
negoracommented, Jul 28, 2022

I’ve quickly tested this in RC2 and it works fine. Thank you!

1reaction
mbiencommented, Jul 27, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why after implementing get in field property I can't initialize ...
I am doing tutorial from MSDN and I was following it step by step, after implementing get method in Balance property I got...
Read more >
Groovy Language Documentation
Principles; Variables vs fields in type inference; Collection literal type ... Constructors are special methods used to initialize an object with a specific ......
Read more >
CHANGELOG.md - Google Git
Fix false positive reports regarding optional parameters on private constructors being unused. Introduce a diagnostic which is reported when a constructor ...
Read more >
CS 2112/ENGRD 2112 Fall 2021
Constructors. It's awkward and error-prone to have to initialize the instance variables of objects after creating them with new .
Read more >
SDK Language Manual - NetModule
2.4.5.1 Defining structure fields . ... 2.4.5.3 Constructor method . ... They provide out-of-band error signaling and handling.
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