-4

The following code generates NullPointerException in Android.

searchResults.setText(searchResultString);

Where:

  • searchResults is a TextView
  • searchResultString is a String

Can somebody list out the possible reasons why this may be happening?

NuSkooler
  • 5,391
  • 1
  • 34
  • 58
  • It seems you are trying to set a String type to a TextView type. Maybe this person's post will help http://stackoverflow.com/a/12201919/3173748. Also, this is a very poorly worded question. Please give the context of your problem when asking the question to get better help (i.e. "I am trying to setText to a TextView but I get this error" ) – superjisan Jun 14 '16 at 22:34

2 Answers2

0

So your textView not binding or attaching correctly. Other wise setText expecting string and your codes should work. Check where you bind your searchResult object.

Eren Utku
  • 1,731
  • 1
  • 18
  • 27
0

Make sure that the searchResults TextView is initialized in Android. This is a very common programming mistake in Android.

You can initialize it like this.

TextView searchResults;
searchResults = (TextView) findViewById(R.id.searchResultsTextView);

Let me know if this solution works out.

Just make sure that the binding between the java file and the xml file is done properly for the given text view.

Aditya
  • 1,172
  • 11
  • 32