0

Why and where is my array being thrown out of bounds?

public  class DownloadRawData extends AsyncTask<String, Void, String>

{

    @Override
    public void onPostExecute(String webData) {

        mData =webData;
        Log.v(LOG_TAG, "Data returned was: "+mData);
        if(mData ==null) {
            if (mRawURL ==null) {
                mDownloadStatus = DownloadStatus.NOT_INITIALIZED;
            } else {
                mDownloadStatus =DownloadStatus.FAILED_OR_EMPTY;
            }

        } else {
            mDownloadStatus =DownloadStatus.OK;
        }


    }
    protected  String doInBackground(String...params){
        HttpURLConnection urlConnection =null;
        BufferedReader  reader = null;
        if(params ==null)
            return  null;
        try{
            URL url = new URL(params[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            InputStream inputStream = urlConnection.getInputStream();
            if (inputStream ==null){
                return null;
            }
            StringBuilder buffer= new StringBuilder();

            reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while((line =reader.readLine()) !=null) {
                buffer.append(line + "\n");
            }
 return buffer.toString();


        } catch (IOException e) {
            Log.e(LOG_TAG,"Error",e);
            return null;
        } finally {
            if(urlConnection !=null) {
                urlConnection.disconnect();
            }
            if(reader !=null) {
                try {
                    reader.close();

                }
                catch (final IOException e) {
                    Log.e(LOG_TAG,"ERROR closing stream",e);
                }
            }
        }

heres my errors

Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
            at com.google.android.gms.samples.vision.face.multitracker.GetRawData$DownloadRawData.doInBackground(GetRawData.java:84)
            at com.google.android.gms.samples.vision.face.multitracker.GetRawData$DownloadRawData.doInBackground(GetRawData.java:55)

Java line 55 is

    public  class DownloadRawData extends AsyncTask<String, Void, String>

And Java line 84 is

    URL url = new URL(params[0]);
iagreen
  • 31,470
  • 8
  • 76
  • 90
Darian B
  • 317
  • 2
  • 3
  • 15

1 Answers1

4

This code:

if(params ==null)
        return  null;

Doesn't prevent a caller from invoking your method without any arguments, in which case your params will be an empty array, thus having the size of zero, that's why you are receiving the exception.

You should add something like params.length > 0 to that check, or change your method's logic in some other way to handle this situation.

user3707125
  • 3,394
  • 14
  • 23