In activity_main.xml display the view :

 <EditText android:id="@+id/textEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="text" /> 

In the onCreate method inside the Activity :

 setContentView(R.layout.activity_main); EditText textEditText = (EditText)findViewById(R.id.textEditText); textEditText.method(); 

But when calling any method on textEditText Android Studio warns that

Method invocation ... may produce 'java.lang.NullPointerException'

Is such an outcome possible? Or is it a mistake?

  • Why do you think that any method cannot return null in certain circumstances, I also think that it is necessary to indicate which method causes this warning. - pavlofff
  • @pavlofff in the onCreate method with any method. For example, under what circumstance would textEditText be null? - iamtihonov
  • It's just that in the latest versions of AS they added inspections related to a null-safe code, this has now become very fashionable (null-safe code), so it gives out for any reason, just ignore it. - pavlofff

3 answers 3

This warning indicates that at the time of calling textEditText.method(); The textEditText variable can be null . For example, due to the fact that findViewById before this could not find in your View for the specified id . In this case, textEditText written to textEditText

  • The studio is supposed to see the line setContentView (R.layout.activity_main) and that inside xml there is an EditText with such id and null cannot be returned - iamtihonov
  • But if textEditText.method () is called in another method, there is no warning - iamtihonov
  • @iamtihonov studio is not so smart, and just recommends that in order to avoid NPE, check to see if this View is right - Vladyslav Matviienko
  • @ Yes, probably it is, I just thought there might really be situations when he would return null, even if he wrote everything correctly - iamtihonov

To really get null instead of EditText from your markup, you need to make the onCreate method onCreate without displaying the activation on the screen. For example:

  1. We create a default constructor in the class.
  2. In it we cause onCreate
  3. In onCreate create an instance of the onCreate class.
  4. Voila - when creating an instance, onCreate will be onCreate and the application will onCreate .

It can, of course, setContentView already on setContentView . I did not check, of course, my idea.
It is also, it seems, so it may even go into recursion, if, suddenly, it does not fall)

I think the studio just takes into account the possibility of calling a method from the constructor and simply does not know that the constructor should not be redefined and activated and your code should not fall under normal conditions.

  • Tried to do so, falls on super.onCreate (savedInstanceState), gives a NullPointerException: Attempt to read from the field 'java.lang.String android.content.pm.ActivityInfo.parentActivityName' on a null object reference - iamtihonov
  • @iamtihonov, oh, but I don’t have to try something - it’s really a very wrong way of using activity that I tried to think of which should definitely fall somewhere))) And this is all to the fact that yes, IDE is not always right and necessary just keep in mind that all the nuances to explain it to her developers will never be able to. Those. in your particular case, its warning is better to ignore. - Yuriy SPb
  • Those. for actviti and fragments, the creation of a constructor is almost certainly a receiving error for the system, because the system itself must operate on the designers and interfere with this process. - Yuriy SPb

From the comments it becomes clear that in this example NPE cannot occur and we can safely ignore this warning.