Basically, I'm attempting to create an activity to pull data from Google Calendar via FirebaseAuth. I've already successfully converted the FirebaseUser object to a proper Account Object to properly invoke the Events methods from Google Calendar. However, I'm facing the issue when the Events object would be null and here is my code:
// Breakthrough 1
// Miguel has created a wonderful Google Cal API Kickstarter Repo to refer to.
// https://github.com/miguelarauj1o/GoogleCalendarQuickStart
/**
* Breakthrough 2
*
* Several issues such as Firebase Support was fixed by attempting to convert FirebaseUser
* data into an Android.Account Object so that CalendarProvider is able to identify the user
* properly.
*
* However, issues regarding threading came up with an IllegalArgumentException from the start
* and lots of research was done and these are the helpers to the solution.
* http://stackoverflow.com/questions/20749044/how-to-get-rid-of-java-lang-illegalstateexception-while-trying-to-getauthtoken-f
* http://stackoverflow.com/questions/17547019/calling-this-from-your-main-thread-can-lead-to-deadlock-and-or-anrs-while-getti
*/
public class CalendarProvider extends AsyncTask<Void, Void, Events> {
static final HttpTransport transport = AndroidHttp.newCompatibleTransport();
static final JsonFactory jsonFactory = GsonFactory.getDefaultInstance();
static final int REQUEST_ACCOUNT_PICKER = 1000;
static final int REQUEST_AUTHORIZATION = 1001;
static final int REQUEST_GOOGLE_PLAY_SERVICES = 1002;
private static Events events;
private static DateTime dt;
private static final String PREF_ACCOUNT_NAME = "accountName";
private static final String[] SCOPES = { CalendarScopes.CALENDAR_READONLY };
public synchronized static void InitializeCal(SharedPreferences sp, GoogleAccountCredential credential, FirebaseUser user, Context AppContext) {
credential = GoogleAccountCredential.usingOAuth2(
AppContext, Arrays.asList(SCOPES))
.setBackOff(new ExponentialBackOff())
.setSelectedAccountName(sp.getString(PREF_ACCOUNT_NAME, user.getEmail()));
mService = new com.google.api.services.calendar.Calendar.Builder(
transport, jsonFactory, credential)
.setApplicationName("Google Calendar API Android Quickstart")
.build();
}
/**
* Fetch a list of the next 10 events from the primary calendar.
* @return List of Strings describing returned events.
* @throws IOException
*/
public static List<String> getDataFromApi() throws IOException {
// List the next 10 events from the primary calendar.
List<String> eventStrings = new ArrayList<>();
try {
if (events.getItems() != null) { // If there are events on this date
List<Event> items = events.getItems();
for (Event event : items) {
DateTime start = event.getStart().getDateTime();
if (start == null) {
// All-day events don't have start times, so just use
// the start date.
start = event.getStart().getDate();
}
eventStrings.add(
String.format("%s (%s)", event.getSummary(), start));
}
}
} catch (Exception e) {
// Else we add a string to tell'em that nothing is here today
eventStrings.add("Nothing Today!");
// Debugging String
eventStrings.add(e.toString());
}
return eventStrings;
}
@Override
protected Events doInBackground(Void... params) {
dt = new DateTime(selectedDate);
try {
events = mService.events().list("primary")
.setMaxResults(10)
.setTimeMin(dt)
.setOrderBy("startTime")
.setSingleEvents(true)
.execute();
return events;
} catch (Exception e) {
}
return null;
}
@Override
protected void onPostExecute(Events events) {
try {
System.out.println(getDataFromApi());
} catch (IOException e) {
e.printStackTrace();
}
}
}
I was wondering how to properly handle such NullPointerExceptions because I have no idea if the Error was deliberate or not.
If it was, then it's simple for me, I'll just assume that the particular date that the user has selected has nothing up.
But what if it isn't?