Application closing when consuming json

Asked

Viewed 57 times

1

What should I do to fix this crash?

My code:

public class Tela1 extends AppCompatActivity {

    TextView textView2;
    String[] objetos = new String[0];
    String url1 = "https://economia.awesomeapi.com.br/json/USD-BRL/1";
    JSONObject jsonObjectTexts;


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

        new AsyncTaskExample().execute(url1);

        ImageView myImageView = (ImageView) findViewById(R.id.imageView);
        myImageView.setImageResource(R.drawable.imagem);


    }

    public class AsyncTaskExample extends AsyncTask<String, String, String[]> {

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected String[] doInBackground(String... url) {

            try {
                jsonObjectTexts = JsonParser.readJsonFromUrl(url[0]);
                objetos[0] = jsonObjectTexts.getString("ask");
            } catch (IOException | JSONException e) {
                e.printStackTrace();
            }

            return objetos;
        }

        @Override
        protected void onPostExecute(String[] stringFromDoInBackground) {

            textView2 = (TextView) findViewById(R.id.textView2);


            textView2.setText(stringFromDoInBackground[0]);
            }

    }
}

Mistake I get:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: br.com.planetsweb.dolarhoje, PID: 2917
                  java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                      at br.com.planetsweb.dolarhoje.Tela1$AsyncTaskExample.onPostExecute(Tela1.java:60)
                      at br.com.planetsweb.dolarhoje.Tela1$AsyncTaskExample.onPostExecute(Tela1.java:35)
                      at android.os.AsyncTask.finish(AsyncTask.java:632)
                      at android.os.AsyncTask.access$600(AsyncTask.java:177)
                      at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:136)
                      at android.app.ActivityThread.main(ActivityThread.java:5045)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:515)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                      at dalvik.system.NativeStart.main(Native Method)
Application terminated.
  • 1

    Hello Dans. The error occurs because the array is initialize with size 0 and you try to put a value in the first position. It would be more interesting to get an idea of the size of the array before initializing it.

1 answer

4


In this line

String[] objetos = new String[0];

You initialize an array of 0 positions. In java arrays have no dynamic size, it would be interesting to exchange this array for an arraylist.

The error occurs because as the array has 0 positions you cannot access the first position. This is what is reported in the error message you received.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.