S
S
SuperNoob2016-06-03 08:56:30
Java
SuperNoob, 2016-06-03 08:56:30

When accessing the variable method invocation?

public class MainActivity extends AppCompatActivity {

    TextView myText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myText = (TextView) findViewById(R.id.myText);

        myText.setText("test"); //Method invocation 'myText.setText("test")' may produce 'java.lang.NullPointerException'
    }

What is the problem?
In other modules, the same code works fine, I don't understand.
Tell me please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2016-06-03
@SuperNoob

The IDE is telling you that there might not be myText in activity_main.xml. Then the variable will be null.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question