0

I have following line of code in eclipse kepler.

if (this != null) {

}

This != null show an error "Redundant null check: this expression cannot be null" even if i have changed Redundant null check into warning by going through Java > Compiler > Errors/Warnings > Null analysis.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • 1
    Why would you check if an instance currently being executed is null? It is most obviously not. I know your question regards the warning vs error, but I am just curious. – bosnjak Mar 14 '14 at 08:44
  • Uh, `this` can never be `null`... If you have `this` your instance exists – fge Mar 14 '14 at 08:52
  • possible duplicate of [Eclipse Kepler shows error marks on warnings](http://stackoverflow.com/questions/18488813/eclipse-kepler-shows-error-marks-on-warnings) – helb Mar 14 '14 at 09:58

1 Answers1

3

Considering the following:

  • The keyword this can only be used inside an instance method, i.e. in a method which is executed on an existing object.
  • this refers to the object on which the method is called
  • this is read-only (cannot explicitly assign null to it for example).
  • Calling an instance method on a null object would result in an exception.

Therefore, it is guaranteed that this is never null.

The compiler warning helps you to identify code which makes no sense or code which is never be executed.

EDIT: About error / warning behavior of eclipse kepler, see eclipse-kepler-shows-error-marks-on-warnings

Community
  • 1
  • 1
helb
  • 7,609
  • 8
  • 36
  • 58