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?

  • 2
    Have you tried using a debugger? I suppose textViewQuote is null for some reason. Where do you initialize this object? - iksuy
  • textViewQuote - how is this object declared and initiated? Well, that is if null is not qoute, then textViewQuote. - Mark
  • @iksuy @Mark Here is the initialization: textViewQuote = (TextView)findViewById(R.id.quote); . I initialize immediately before use - andrew
  • So what about debugging? You should use the debugger, see which object is null at the time this line is executed, then understand why it is null . The documentation states that findViewById can return null : Apparently he returns null to you for some reason. - iksuy

0