-1

For some reason my code gives one random number but crashes after the second due to a null pointer exception. Does anybody know how I can fix this?

public void shownumbers() {
    Timer MyTimer = new Timer();
        MyTimer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                Random r = new Random();
                TextView viewbox = (TextView) findViewById(R.id.textView);
                viewbox.setText(Integer.toString(r.nextInt(10)));
            }
        }, 100, 500);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

The function findViewById(int) returns null if R.id.textView is not found. You can check the stack trace of your NullPointerException and find the exact line where it fails.

Larry B.
  • 753
  • 6
  • 21