This question already has an answer here:
-
What is a NullPointerException, and how do I fix it?
12 answers
Note: This is intended as a canonical question for a commonly seen problem here on Stack Overflow.
My Android app crashed. I checked the stacktrace from the logcat and found that the cause is a NullPointerException
at the line
myEditText.setText(myString);
What is the problem and how do I fix it?
Please provide more detail about your code. I think you didn't initialize myEditText correctly
something like this:
EditText myEditText = findViewById(R.id.xxx);
you didn't initialize myEditText
reference to any EditText
Object in the activity which holds it..
kindly be sure you initialize it and you use that statement in the right place.
Remember one thing whenever you get null pointer exception in any findViewById it can only happens if id declared inside XML and java files are not matching or say from java side you are trying to access Id which don't even declared in XML side.
Another point i want to make is you are using setText() method and you are passing String object now keep in mind that it can also arise null pointer if it's a null object and don't have any value.
The error message indicates that myEditText
has not been initialized correctly. First, you need to be sure that you call findViewById()
. For example
myEditText = findViewById(R.id.text);
If you already did this, double-check that the resource id matches the one declared in the XML layout file for your activity or fragment.
Also, you may have forgot to call setContentView()
in your activity's onCreate()
method.
use "" and then append your string. its probably your myString returning null.
myEditText.setText("" + myString);