I'm Getting this Comment from PMD.But i have to empty the data.Is there any way to remove the content of that Object.
Asked
Active
Viewed 1.1k times
2
-
6It would probably help if you posted the part of the code that is reported by PMD. – Jan Thomä Dec 15 '10 at 12:12
-
Can you not `dispose` of the object? – Lazarus Dec 15 '10 at 12:13
-
1? http://stackoverflow.com/questions/1615419/could-someone-explaining-the-reasoning-behind-some-of-these-pmd-rules – barti_ddu Dec 15 '10 at 12:21
-
3It is highly likely that you don't "need to empty the data". – Peter Lawrey Dec 15 '10 at 12:21
-
@Peter Lawrey : Thanks for that :) At least I learnt something here ;) – Lazarus Dec 15 '10 at 12:23
-
BTW what do you mean by "I have to empty the data" ? – pgras Dec 15 '10 at 13:37
4 Answers
2
Setting a variable to null just removes a reference to the object. it does not change the actual object.
If you need to clean up, you most likely need to do actions to the object referenced by your variable instead.
Thorbjørn Ravn Andersen
- 73,784
- 33
- 194
- 347
-
then how to change the object value..At last i placed myStringobject = ''; instead of mystringObject = null;Then the errors were went. – jayavardhan Dec 16 '10 at 07:11
-
@jayavardhan, for the record: When you or PMD have problems with code, then _SHOW_ the code when asking here to get better answers. – Thorbjørn Ravn Andersen Dec 16 '10 at 07:35
1
Assigning an object reference the value null does not 'empty the data'; it merely sets that variable reference to null.
Andrew Barber
- 39,603
- 20
- 94
- 123
0
It probably wants you to use the Null Object pattern.
Sylvain Defresne
- 42,429
- 12
- 75
- 85
-
FWIW, the object might simply have been instantiated prematurely. Just fixed one such case. – Tomislav Nakic-Alfirevic Sep 17 '13 at 08:34
0
Just set your variable to final.
For instance, if there is the following code, SONAR will report as a code smell:
private Object obj = null;
However, if you just add final, it can be resolved.
private final Object obj = null;
Alexsander Caproni
- 117
- 7