NullPointerException when accessing the list of user calendars using the Google...
source link: https://www.codesd.com/item/nullpointerexception-when-accessing-the-list-of-user-calendars-using-the-google-calendar-api.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
NullPointerException when accessing the list of user calendars using the Google Calendar API
I want to use the Google Calendar Api to pull the calendars, events, etc. I have the authentication token from the AccountManager class in android. So this way if a user is logged in the mobile using his google account, then i can easily retrieve the authentication token related to him. After thati do the following:
public Calendar useCalendarAPI(final String accessToken) {
try{
HttpTransport transport = new NetHttpTransport();
AccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(accessToken);
Calendar.Builder builder = Calendar.builder(transport, new JacksonFactory());
builder.setApplicationName("My App Name");
builder.setHttpRequestInitializer(accessProtectedResource);
JsonHttpRequestInitializer json = new JsonHttpRequestInitializer() {
public void initialize(JsonHttpRequest request) {
CalendarRequest calRequest = (CalendarRequest) request;
calRequest.setKey(Common.API_KEY);
calRequest.setOauthToken(accessToken);
}
};
builder.setJsonHttpRequestInitializer(json);
Calendar calendarService= builder.build();
if(calendarService!= null)
{
return calendarService;
}
else
return null;
}catch (Exception e) {
// TODO: handle exception
e.getMessage();
}
After this i receive a Calendar object called as "calendarService".
public void getCalendarsList()
{
try
{
CalendarList calendarList = calendarService.calendarList().list().execute();
while (true) {
for (CalendarListEntry calendarListEntry : calendarList.getItems()) {
System.out.println(calendarListEntry.getSummary());
}
String pageToken = calendarList.getNextPageToken();
if (pageToken != null && !pageToken.equalsIgnoreCase("")) {
calendarList = calendarService.calendarList().list().setPageToken(pageToken).execute();
} else {
break;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This is copied from the Google Calendar API Documentation. Check this link here
But i am getting NullPointer Exception at the following line
CalendarList calendarList = calendarService.calendarList().list().execute();
Please suggest something.
Thanks
Hey I have this small working sample which was made using the same calendar doc you referred too. Also it would help if you can give the stack trace along with the code.
public static void main(String[] args) throws IOException {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
String clientId = "CLIENT ID";
String clientSecret = "CLIENT SECRET";
String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
String scope = "https://www.googleapis.com/auth/calendar";
String authorizationUrl = new GoogleAuthorizationRequestUrl(clientId, redirectUrl, scope)
.build();
System.out.println("Go to the following link in your browser:");
System.out.println(authorizationUrl);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What is the authorization code?");
String code = in.readLine();
AccessTokenResponse response = new GoogleAuthorizationCodeGrant(httpTransport, jsonFactory,
clientId, clientSecret, code, redirectUrl).execute();
GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
response.accessToken, httpTransport, jsonFactory, clientId, clientSecret,
response.refreshToken);
Calendar calendarService = Calendar.builder(httpTransport, jsonFactory)
.setApplicationName("YOUR_APPLICATION_NAME")
.setHttpRequestInitializer(accessProtectedResource)
.build();
try
{
com.google.api.services.calendar.model.CalendarList calendarList = calendarService.calendarList().list().execute();
while (true) {
for (CalendarListEntry calendarListEntry : calendarList.getItems()) {
System.out.println(calendarListEntry.getSummary());
}
String pageToken = calendarList.getNextPageToken();
if (pageToken != null && !pageToken.equalsIgnoreCase("")) {
calendarList = calendarService.calendarList().list().setPageToken(pageToken).execute();
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK