Okay, sorry about that. I didn't post the full code because it's pulling data from a private spreadsheet, and wasn't sure if you'd be able to run it anyways. Here it is though, in addition to the full error code (this is mostly taken from a tutorial on blog.blprnt.com). If it helps, the spreadsheet has two columns: one for age (obviously a number), and sex (M or F). Occasionally, there are blank cells where we don't have all the information. I highlighted the line that throws the error; look at the bottom for the full text of the error.
- //This is the Google spreadsheet manager and the id of the spreadsheet that we want to populate, along with our Google username & password
SimpleSpreadsheetManager sm;
String sUrl = "pEAyAv6KI8MBIFtOls_ZrMA";
String googleUser = "guser";
String googlePass = "gpwd";
void setup() {
//This code happens once, right when our sketch is launched
size(800,600);
background(0);
smooth();
int[] numbers = getNumbers();
String[] sex = getSex();
fill(255,40);
noStroke();
for (int i = 0; i < numbers.length; i++) {
if( (sex[i].equals("F")) && (numbers[i] > 0) ) { //This is the line that gets highlighted when the error triggers
ellipse(numbers[i] * 8, width/2, 8,8);
};
};
};
void draw() {
//This code happens once every frame.
};
- //Function to get an Array of integers from a Google Spreadsheet
int[] getNumbers() {
println("Asking Google for numbers...");
sm = new SimpleSpreadsheetManager();
sm.init("RandomNumbers", googleUser, googlePass);
sm.fetchSheetByKey(sUrl, 0);
int n = sm.currentListEntries.size();
int[] returnAge = new int[n];
for (int i = 0; i < n; i++) {
if( sm.getCellValue(5,i) != null ) {
returnAge[i] = int(sm.getCellValue(5, i));
};
};
return(returnAge);
};
String[] getSex() {
println("Asking Google for sex...");
sm = new SimpleSpreadsheetManager();
sm.init("Sex", googleUser, googlePass);
sm.fetchSheetByKey(sUrl, 0);
int m = sm.currentListEntries.size();
String[] returnSex = new String[m];
for (int i = 0; i < m; i++) {
if( sm.getCellValue(6,i) != null ) {
returnSex[i] = sm.getCellValue(6, i);
};
};
return(returnSex);
};
- /*
SimpleSpreadsheetManager Class
blprnt@blprnt.com
July, 2009
This is a quick & dirty class for accessing data from the Google Spreadsheet API.
*/
import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SimpleSpreadsheetManager {
SpreadsheetService myService;
SpreadsheetEntry spreadsheetEntry;
SpreadsheetFeed sheetFeed;
WorksheetEntry worksheetEntry;
List spreadsheets;
String user;
String pass;
ListFeed currentListFeed;
CellFeed currentCellFeed;
List currentCells;
List currentListEntries;
int currentTotalRows;
int currentTotalCols;
String currentTitle;
String[] tagArray;
URL listFeedUrl;
SimpleSpreadsheetManager() {
};
/*
INIT FUNCTION
Opens session, uses username & password for authentication
*/
void init(String sessionName, String u, String p) {
user = u;
pass = p;
myService = new SpreadsheetService(sessionName);
try {
myService.setUserCredentials(user, pass);
}
catch (Exception e) {
println("ERROR IN AUTHENTICATION");
};
};
/*
FETCH SHEET BY KEY
Gets a spreadsheet listfeed using the unique key - this is in the URL of the spreadsheet
The retrieved sheet is both returned and set as the currentListFeed
*/
ListFeed fetchSheetByKey(String k, int wi) {
ListFeed f = new ListFeed();
CellFeed cf = new CellFeed();
WorksheetFeed w = new WorksheetFeed();
//GET WORKSHEETS FEED
try {
URL worksheetFeedUrl = new URL("http://spreadsheets.google.com/feeds/worksheets/" + k + "/private/full");
WorksheetFeed wf2 = new WorksheetFeed();
w = myService.getFeed(worksheetFeedUrl, wf2.getClass());
}
catch(Exception e) {
println("ERROR RETRIEVING WORKSHEET FEED");
};
List worksheets = w.getEntries();
WorksheetEntry we = (WorksheetEntry) worksheets.get(wi);
println("RETRIEVED WORKSHEET " + we.getTitle().getPlainText());
//GET LIST FEED URL
try {
listFeedUrl = we.getListFeedUrl();//new URL("http://spreadsheets.google.com/feeds/list/" + k + "/od6/private/full");
ListFeed lf2 = new ListFeed();
f = myService.getFeed(listFeedUrl, lf2.getClass());
}
catch(Exception e) {
println("ERROR RETRIEVING LIST FEED");
};
//GET CELL FEED
try {
URL cellFeedUrl = we.getCellFeedUrl();//new URL("http://spreadsheets.google.com/feeds/cells/" + k + "/od6/private/full");
CellFeed lf2 = new CellFeed();
cf = myService.getFeed(cellFeedUrl, lf2.getClass());
}
catch(Exception e) {
println("ERROR RETRIEVING LIST FEED");
};
currentListFeed = f;
currentCellFeed = cf;
currentCells = cf.getEntries();
currentListEntries = f.getEntries();
currentTitle = we.getTitle().getPlainText();
currentTotalRows = currentListEntries.size();
if (currentListEntries.size() > 0) {
ListEntry le = (ListEntry) currentListEntries.get(0);
currentTotalCols = le.getCustomElements().getTags().size();
Set<String> tags = le.getCustomElements().getTags();
tagArray = new String[tags.size()];
tagArray = tags.toArray(tagArray);
};
return(f);
};
/*
GET CELL VALUE
Returns the value held in an individual sheet cell.
*/
String getCellValue(int c, int r) {
ListEntry le = (ListEntry) currentListEntries.get(r);
Set<String> tags = le.getCustomElements().getTags();
String[] tagArray = new String[tags.size()];
tagArray = tags.toArray(tagArray);
return(le.getCustomElements().getValue(tagArray[c]));
};
String getCellValue(String tag, int r) {
ListEntry le = (ListEntry) currentListEntries.get(r);
return(le.getCustomElements().getValue(tag));
};
void setCellValue(String tag, int r, String val) {
ListEntry le = (ListEntry) currentListEntries.get(r);
le.getCustomElements().setValueLocal(tag, val);
try {
ListEntry updatedRow = le.update();
}
catch (Exception e){
};
};
void setNewCellValue(String tag, int r, String val) {
ListEntry le = new ListEntry();
try {
le.getCustomElements().setValueLocal(tag, val);
ListEntry insertedRow = myService.insert(listFeedUrl, le);
ListEntry updatedRow = insertedRow.update();
}
catch (Exception e){
};
};
};
This is the error code that's printed:
- Asking Google for numbers...
RETRIEVED WORKSHEET Current 2.0
Asking Google for sex...
RETRIEVED WORKSHEET Current 2.0
processing.app.debug.RunnerException: NullPointerException
at processing.app.Sketch.placeException(Sketch.java:1543)
at processing.app.debug.Runner.findException(Runner.java:582)
at processing.app.debug.Runner.reportException(Runner.java:558)
at processing.app.debug.Runner.exception(Runner.java:498)
at processing.app.debug.EventThread.exceptionEvent(EventThread.java:367)
at processing.app.debug.EventThread.handleEvent(EventThread.java:255)
at processing.app.debug.EventThread.run(EventThread.java:89)
Exception in thread "Animation Thread" java.lang.NullPointerException
at FT_age.setup(FT_age.java:100)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:619)
Thanks a lot for the help!