0

I've created a method that shall return a two-dimensional Array, everything works perfectly as the array is being correctly filled in the method's try. But once I display the array on onCreate(), it's returning null.

public class ListTickets extends AppCompatActivity {


    public String[][] ticketTab ;

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

        ticketTab = new String[Integer.valueOf(nbTicket)][nbTicketTab];


        DisplayArray(getTicketsHTTP());


    }

        private String[][] getTicketsHTTP() {


        final JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, URL, null,
                new Response.Listener<JSONObject>()
                {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray Jdata = response.getJSONArray("data");
                            for (int i=0; i < Jdata.length(); i++) {
                                try {
                                    JSONObject oneTicket = Jdata.getJSONObject(i);

                                    titreTicket = oneTicket.getString("1");
                                    slaTicket = oneTicket.getString("30");
                                    dateDebutTicket = oneTicket.getString("15");
                                    urgenceTicket = oneTicket.getString("10");
                                    statutTicket = oneTicket.getString("12");
                                    idTicket = oneTicket.getString("2");


                                } catch (JSONException e) {
                                    Log.e("Err", e.getMessage());
                                }


                                ticketTab[i][0] = titreTicket;
                                ticketTab[i][1] = slaTicket;
                                ticketTab[i][2] = dateDebutTicket;
                                ticketTab[i][3] = urgenceText(urgenceTicket);
                                ticketTab[i][4] = calculTempsRestant(dateDebutTicket, slaTicket, dateEchanceTicket);
                                ticketTab[i][5] = String.valueOf(ticketEnretard);
                                ticketTab[i][6] = statutTicket;
                                ticketTab[i][7] = idTicket;


                            }


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        Log.e("Error.Response", error.toString());
                    }

                }
        ){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> params = new HashMap<String, String>();
                params.put("App-Token",FirstEverActivity.App_Token);
                params.put("Session-Token",session_token);
                return params;
            }

        };

        // add it to the RequestQueue
        queue.add(getRequest);

        return ticketTab;
    }


}

I declared ticketTab outside the onCreate because when I declare it inside the method, I cannot change it inside the try. How can I return the array correctly?

1 Answer 1

1

In your onCreate you are using this line:

ticketTab = new String[Integer.valueOf(nbTicket)][nbTicketTab];

and those values, nbTicket and nbTicketTab are not declared anywhere in your code, maybe that's why they are returning null, you have to initialize them and asign values.

Sign up to request clarification or add additional context in comments.

1 Comment

I removed them from the code to make it clearer. I've initialized them. The problem was coming from the JSON Request, it works in a asynchronous way, means that you never know when the onResponse() method is gonna be invoked. So it was returning a null array before even filling it because the onResponse() was not invoked yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.