-1

I am trying to implement spinners, and at the Line

spinner1.setAdapter(adapter1);

I get the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference

This is my Code:

public class Temp extends Fragment implements AdapterView.OnItemSelectedListener{
    private static final String[] paths1 = {"sam1", "sam2", "sam3", "sam4"},
            paths2 = {"1", "2", "3", "4"};
    private String name,seq;
    private Spinner spinner1,spinner2;
    private TextView tv1;
    private ArrayAdapter<String> adapter1,adapter2;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View attendFragView = inflater.inflate(R.layout.fragment_attendance, container, false);

        //Spinner 1 code
        spinner1=(Spinner)attendFragView.findViewById(R.id.regPageFacSpinner1);
        adapter1=new ArrayAdapter<String>(attendFragView.getContext(),R.layout.spinner_lay_notched,
                paths2);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter1);
        spinner1.setOnItemSelectedListener(this);

        //Spinner 2 Code

        spinner2=(Spinner)attendFragView.findViewById(R.id.regPageFacSpinner2);
        adapter2=new ArrayAdapter<String>(attendFragView.getContext(),R.layout.spinner_lay_notched,
                paths1);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter2);
        spinner2.setOnItemSelectedListener(this);


        final EditText et1 = (EditText) attendFragView.findViewById(R.id.secOtpFac);
        tv1=(TextView)attendFragView.findViewById(R.id.attendPageFac5);

        Button genOtp = (Button) attendFragView.findViewById(R.id.otpGenButFac);
        genOtp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String dept= et1.getText().toString();
                String name=spinner1.getSelectedItem().toString();
                String seq=spinner2.getSelectedItem().toString();
                JSONObject attendJSONSend = new JSONObject();
                try {
                    attendJSONSend.put("name", name);
                    attendJSONSend.put("seq", seq);
                    attendJSONSend.put("dept", dept);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        return attendFragView;
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
}
Mauker
  • 11,237
  • 7
  • 58
  • 76
sachco
  • 274
  • 2
  • 9

1 Answers1

2

As the Exception says , spinner1 is null because the compiler try to use setAdapter on a null Object.

Recommendation in order to fix this problem. (as I faced it a lot).

  • Check your layout and make sure that your view ids are correct in your code and inside the XML layout file.
  • Android Studio Will save your created view ids although you might removed them before. Try to Clean/Rebuild.
  • And more specifically the problem is on this id: R.id.regPageFacSpinner1.

Good luck.

Mauker
  • 11,237
  • 7
  • 58
  • 76
A Farmanbar
  • 4,381
  • 5
  • 24
  • 42