I am trying to update the value displayed in a TextView when the user changes the value inside the number picker widget.
I have the number picker set up and working correctly however when I attempt to call the TextView's .setText() method I get the following error:
java.lang.NullPointerException at com.example.waitron5.MenuItemArrayAdapter.onValueChange(MenuItemArrayAdapter.java:68)
Below is the code corresponding to where the error seems to be occuring in the MenuItemArrayAdapter class:
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
Toast.makeText(this.getContext(), "Change: " + picker.getValue(), Toast.LENGTH_SHORT).show();
//if oldVal > newVal increment price
price.setText("xyz");
//if oldVal < newVal decrement price
}
The Toast message was put in to test the listener was working correctly and it was. I then added the setText() method but that caused the error.
Any help on the issue would be much appreciated! I declare the TextView at the top of the MenuItemArrayAdapter class:
private TextView price;
NOTE: The number picker is contained within each view of a list. Which is why I have the MenuItemAdapter class. Could the problem be linked to trying to update the textView from the wrong location?
Below is the getView() method of the MenuItemAdapter class.
public View getView(final int position, View convertView, ViewGroup parent){
View v = convertView;
if(v == null){
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.menuitem_row, null);
}
//assign values to view
MenuItem item = this.menuItems.get(position);
TextView nameView = (TextView) v.findViewById(R.id.item_name);
TextView priceView = (TextView) v.findViewById(R.id.item_price);
nameView.setText(item.getName() + " ");
priceView.setText("€"+ String.valueOf(item.getPrice()));
price = (TextView)v.findViewById(R.id.price);
//number picker
np = (NumberPicker)v.findViewById(R.id.numpick);
np.setMaxValue(99);
np.setMinValue(0);
np.setValue(0);
np.setOnValueChangedListener(this);
return v;
}