BenGoBlue05 / UdacityAlumni

Udacity Alumni Android App
57 stars 30 forks source link

App crashes on first load with IllegalArgumentException #40

Closed PPartisan closed 7 years ago

PPartisan commented 7 years ago

Steps to reproduce

Open the application. Shortly after loading the home page, the application will crash with the following LogCat message:

java.lang.IllegalArgumentException: n <= 0: 0
    at java.util.Random.nextInt(Random.java:182)
    at com.google.developer.udacityalumni.service.AlumIntentService.addArticles(AlumIntentService.java:55)
    at com.google.developer.udacityalumni.service.AlumIntentService.onHandleIntent(AlumIntentService.java:109)
    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:207)
    at android.os.HandlerThread.run(HandlerThread.java:61)

Other Information

The problem appears to be caused on this line in AlumIntentService:

JSONArray tags = article.getJSONArray(KEY_TAGS);
int ind = new Random().nextInt(tags.length());

If tags has a length of zero, then the application crashes as the nextInt() method in Random cannot accept zero as an argument.

Checking the JSON file at http://udacity-alumni-api.herokuapp.com/api/v1/articles shows what's likely causing the problem (note the empty tags array):

    {
      "id": 11,
      "title": "my title",
      "featured": false,
      "spotlighted": true,
      "content": "yo this is my story\n",
      "feature_image": "",
      "status": "draft",
      "slug": "my-title",
      "user": {
        "id": 15,
        "email": "noahedwardhall@gmail.com",
        "created_at": "2017-01-25T09:33:40.858Z",
        "updated_at": "2017-01-25T09:43:34.337Z",
        "name": "noah hall",
        "avatar": null,
        "role": "user",
        "bio": "",
        "public": true
      },
      "created_at": "2017-01-25T10:41:15.683Z",
      "updated_at": "2017-01-25T10:41:15.709Z",
      "tags": [

      ]
    }