widavies / TBA-API-V2

A Java API for pulling robotics data from www.thebluealliance.com.
5 stars 0 forks source link

getEvents() not working in Android Studio #8

Closed JDaniel41 closed 7 years ago

JDaniel41 commented 7 years ago

Whenever I try to use the getEvents() method in my team's Android Studio app, it always throws this exception.

                  Process: com.fernbanklinks.frcscore, PID: 4412
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fernbanklinks.frcscore/com.fernbanklinks.frcscore.TBAEvents}: java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.simple.JSONArray.size()' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.simple.JSONArray.size()' on a null object reference
                      at com.cpjd.requests.EventRequest.getEvents(EventRequest.java:26)
                      at com.cpjd.main.TBA.getEvents(TBA.java:158)
                      at com.fernbanklinks.frcscore.TBAEvents.onCreate(TBAEvents.java:84)
                      at android.app.Activity.performCreate(Activity.java:6237)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

However, if I try running this in Dr. Java or any other Java compiler, there will be a delay, but it will return an array of every event. I think that the Android phone is just freaking out over the fact that there is no initial return, but how can I make it wait?

SAMPLE CODE that is being ran in onCreate():

        TBA.setID("Fernbank LINKS Robotics", "FRC Score Android App", "2017");
        TBA tba = new TBA();

        if(tba != null){
            System.out.println("TBA Initialized");
        }
        //A list of all 160 FRC Competitions
        events = tba.getEvents(2017);
widavies commented 7 years ago

Yeah. I think you're right about the Android phone freaking out about not having an additional value returned. The .getEvents() method takes quite a well to pull every event from a year. The first thing I would try is to create a separate thread in your Android application that loads the events, and then you can just display a "downloading" dialog to the user. I can possibly the method to return each event as soon as it gets it if you'd like. But first thing I'd do is try to put it in a separate thread because Android doesn't like load times within the onCreate() method as far as I know.

JDaniel41 commented 7 years ago

So, the thread creation is working. I just need to add the Downloading box for users, but what would really be nice is if there was a function that searches the events by the names instead of the event codes. That's what I'm trying to accomplish with the thread, and it would speed up the process. Thanks again for the advice!

widavies commented 7 years ago

Awesome. I love the idea of some sorting methods. I think I'll create a new class called Sort that can sort through lists by name, number, date, etc. Would you mind putting together a list of things for me that'd you like to see sortable, as well as different things you can sort with, if you don't mind.