java – Error while populating spinner from MySQL database


I’m trying to populate a spinner from a MySQL database, the problem is, since I create the spinner before it starts to be filled, I get a null pointer exception. I know what it means but I’ve used this code before and it worked, even on this project, so I don’t know what the problem is, can anyone look at it?

Relevant code

public class PaginaEmpresa extends AppCompatActivity {

    private RequestQueue queue2;
    private String requestURL2 = "http://url/get_funcionario.php";

    private ArrayList<String> spinnerContent = new ArrayList<>();
    private String() spinnerConteudo = {};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pagina_empresa);

        JSONObject object2 = new JSONObject();

        queue2 = Volley.newRequestQueue(this);

        //agora vamos construir o pedido HTTP
        final Spinner spinner = findViewById(R.id.spinnerFuncionarios);
        JsonObjectRequest jsonObjectRequest2 = new JsonObjectRequest(Request.Method.POST, requestURL2, object2, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d("USER_DEBUG", "String Response : " + response.toString());
                try {
                    JSONArray results = response.getJSONArray("results");
                    String nome = "";
                    for (int i = 0; i < results.length(); i++) {
                        for (int j = 0; j < results.getJSONObject(i).getInt("nFuncionarios"); j++) {
                            nome = results.getJSONObject(i).getJSONArray("Funcionario").getJSONObject(j).getString("nome");
                            Log.d("USER_DEBUG", "" + nome);

                            spinnerContent.add(nome); //palavra no arrayList
                            spinnerConteudo = new String(spinnerContent.size()); //array do tamanho do arrayList
                            spinnerContent.toArray(spinnerConteudo);  //do arrayList para o array
                        }
                    }

                    spinnerConteudo = new String(spinnerContent.size());
                    spinnerContent.toArray(spinnerConteudo);

                    mostrarConteudoSpinner(spinner, spinnerConteudo);

                } catch (JSONException e) {
                    Log.d("USER_DEBUG", "Erro na leitura da resposta JSON");
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("USER_DEBUG", error.toString());
            }
        });

        queue2.add(jsonObjectRequest2);  //só neste momento é que o pedido é feito

    }

    public void mostrarConteudoSpinner(Spinner spinner, String() spinnerConteudo) {
        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(PaginaEmpresa.this, android.R.layout.simple_spinner_item, spinnerConteudo);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerAdapter.notifyDataSetChanged();

THIS IS WHERE THE APP CRASHES ---> spinner.setAdapter(spinnerAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                return;
            }

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

            }
        });
    }
}

Layout xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    tools:context=".PaginaEmpresa">

    <Spinner
        android:id="@+id/funcionarios"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/btnAssociarFunc"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/descEmpresa" />

</androidx.constraintlayout.widget.ConstraintLayout>```