There is a class Quote . It has a method:
public String getQuote(int key) { String quote = ""; switch(key) { case 0: quote = "Q1"; break; case 1: quote = "Q2"; break; case 2: quote = "Q3"; break; default: qoute = "Error"; break; } return qoute; } Then I try to assign this text to TextView:
Qoute qoute = new Quote(); textViewQuote.setText(qoute.getQoute(0)); When I run the application, a java.lang.NullPointerException error appears, and points to the line:
textViewQuote.setText(quote.getQuote(0)); How can I fix this error?
textViewQuoteisnullfor some reason. Where do you initialize this object? - iksuytextViewQuote = (TextView)findViewById(R.id.quote);. I initialize immediately before use - andrewnullat the time this line is executed, then understand why it isnull. The documentation states thatfindViewByIdcan returnnull: Apparently he returnsnullto you for some reason. - iksuy