I am trying to populate data from database into a TextView using this code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView view = (TextView) findViewById(R.id.textView);
db = (new DatabaseHelper(this)).getWritableDatabase();
cursor = getCursor();
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
String text = cursor.getString(2);
view.append(text);
cursor.moveToNext();
}
However I am getting a NullPointerException on this line:
view.append(text);
and I am not sure about the reason as the view exists.
If you are sure that
viewexists and is not null, thentexthas to be null. You could for instance rewrite it asview.append(text != null ? text : "(null)");