1

I'm trying to update a textview with the value of a seekbar, and I'm having trouble setting an OnSeekBarChangeListener. I don't have any compile errors, but every time I try to run the app, I get a null pointer exception.

The error says:

Attempt to invoke virtual method 'void android.widget.SeekBar.setOnSeekBarChangeListener(android.widget.SeekBar$OnSeekBarChangeListener)' on a null object reference

I can't for the life of me figure out why, despite trawling through StackOverflow. Every similar problem, or tutorial on how to set an OnSeekBarChangeListener seems to suggest I do exactly what I've done. Help!

    public class MainActivity extends AppCompatActivity {
public TextView time;
public SeekBar bar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    time = (TextView) findViewById(R.id.time);
    bar = (SeekBar) findViewById(R.id.bar);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bar.setOnSeekBarChangeListener(listener);

}

private SeekBar.OnSeekBarChangeListener listener = new OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        time.setText((String.valueOf(progress/100) + ":" + String.valueOf(progress % 100)));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {


    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
};}
Fahad Al Saud
  • 43
  • 1
  • 4
  • `bar` is null. Are you sure your resources, particularly `R.id.bar`, are set up properly? – David Conrad Dec 15 '15 at 22:58
  • 1
    Take a look at @prograde's answer. If you haven't set the content view yet, the calls to findViewById will fail. I think you will find that `time` is null, too. – David Conrad Dec 15 '15 at 23:06

3 Answers3

10

I would try to set these lines

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

first in the onCreate-method, and after that, try to find the seekBar view with

bar = (SeekBar) findViewById(R.id.bar);

Please, let me know if it solves your problem!

The findViewById() method will try to find the view with the specified ID. But to do that, it has to know which layout file is used for the activity, so that it knows where to search. That's what you point out with the setContentView, and that's why it has to come first.

prograde
  • 2,620
  • 2
  • 23
  • 32
2

Replace your onCreate with:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    time = (TextView) findViewById(R.id.time);
    bar = (SeekBar) findViewById(R.id.bar);
    bar.setOnSeekBarChangeListener(listener);
}

You should call setContentView() before using findViewById!

thiagolr
  • 6,909
  • 6
  • 44
  • 64
0

This error occur too when you call an wrong activity or when you call the correct activity and call another wrong activity that do not have the component you want, example

setContentView(R.layout.correct_activity_with_your_component);
some code....    setContentView(R.layout.wrong_activity_without_your_component_bar_activity_main);
bar = (SeekBar) findViewById(R.id.bar);

So one solution is write the line similar to setContentView(R.layout.correct_activity_with_your_component); before your componenet and verify if there is not another command similar to

setContentView(R.layout.correct_activity_with_your_component);

before you call your component with:

bar = (SeekBar) findViewById(R.id.bar);
Gustavomcls
  • 1,655
  • 2
  • 12
  • 20