英文:
Error after I log in with my Google Account in Google Sheet API
问题
我正在尝试通过Java操作Google表格,但在使用我的Google账号登录后出现错误:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:657)
at java.util.ArrayList.get(ArrayList.java:433)
at com.esdras.caixaipm.DeskIPM.main(DeskIPM.java:75)
这是我的PSVM代码:
public static void main(String[] args) throws IOException, GeneralSecurityException {
sheetsService = getSheetsService();
String range = "A2:A4";
ValueRange response = sheetsService.spreadsheets().values()
.get(SPREADSHEET_ID, range)
.execute();
List<List<Object>> values = response.getValues();
if (values == null || values.isEmpty()){
System.out.println("未找到数据!");
}else{
for (List<Object> row : values) {
System.out.printf("%s %s %s \n", row.get(0), row.get(1), row.get(2));
}
}
}
我认为问题出现在for循环的结构,但我无法修复它。
英文:
I'm trying to manipulate a Google Sheet through Java, but there's a error after I log in with my Google Account:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:657)
at java.util.ArrayList.get(ArrayList.java:433)
at com.esdras.caixaipm.DeskIPM.main(DeskIPM.java:75)
In this is my PSVM:
public static void main(String[] args) throws IOException, GeneralSecurityException {
sheetsService = getSheetsService();
String range = "A2:A4";
ValueRange response = sheetsService.spreadsheets().values()
.get(SPREADSHEET_ID, range)
.execute();
List<List<Object>> values = response.getValues();
if (values == null || values.isEmpty()){
System.out.println("No data found!");
}else{
for (List row : values) {
System.out.printf("%s %s %s \n", row.get(1), row.get(2), row.get(3));
}
}
}
I think the problem is in the for structure, but I can't fix it.
答案1
得分: 0
因为"row.get()"而出现错误。
我将代码编辑为以下内容:
for (List row : values) {
System.out.println(row.get(0));
}
而不是:
for (List row : values) {
System.out.printf("%s %s %s \n", row.get(1), row.get(2), row.get(3));
}
谢谢...
英文:
The error I was getting was because of the "row.get()"
I edited the code to this:
for (List row : values) {
System.out.println(row.get(0));
}
Instead of:
for (List row : values) {
System.out.printf("%s %s %s \n", row.get(1), row.get(2), row.get(3));
}
Thanks...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论