I am have made a Custom Adapter list for the spinner. When i select an item from the spinner, i can get the selected item and can Toast it in the "setOnItemSelectedListener()";
spinner = findViewById(R.id.spinnerId);
CustomAdapter arrayAdapter = new CustomAdapter(this, countryLists, population, flags);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(isSelected == true){
isSelected = false;
}
else {
Toast.makeText(getApplicationContext(),countryLists[position]+" is selected", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
the above code is working fine.
Now i want to pass the selected item value from a button "OnClickListener" like the below code.
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String value = spinner.getSelectedItem().toString();
textView.setText(value );
}
});
but Error is showing "java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference"
What could be the problem??