Java 尝试从公用Google电子表格读取数据时出现NullPointerException

Java 尝试从公用Google电子表格读取数据时出现NullPointerException,java,android,eclipse,google-spreadsheet-api,gdata-api,Java,Android,Eclipse,Google Spreadsheet Api,Gdata Api,我正在尝试使用谷歌电子表格作为一个简单的安卓数据库。但当我调用电子表格时,我会得到空点执行 当我尝试获取工作表的大小时,它返回0。 例如,当我调用getAllCells()时,我得到一个NullPointerExection。 我感谢你能提供的任何帮助/建议 (我把电子表格发布到网上,任何人都可以编辑) 这是电子表格 这是这个小项目的zip文件 这是apk 这里是错误 java.lang.NullPointerException 1at com.google.gdata.data.spread

我正在尝试使用谷歌电子表格作为一个简单的安卓数据库。但当我调用电子表格时,我会得到空点执行 当我尝试获取工作表的大小时,它返回0。 例如,当我调用
getAllCells()
时,我得到一个NullPointerExection。 我感谢你能提供的任何帮助/建议

(我把电子表格发布到网上,任何人都可以编辑)

这是电子表格

这是这个小项目的zip文件

这是apk

这里是错误

java.lang.NullPointerException
1at com.google.gdata.data.spreadsheet.WorksheetEntry.getFeedUrlString(WorksheetEntry.java:133)
2at com.google.gdata.data.spreadsheet.WorksheetEntry.getCellFeedUrl(WorksheetEntry.java:113)
3at com.example.tempo.util.SpDatabase.getAllCells(SpDatabase.java:67)
4at com.example.tempo.TempoMain.testDataBase(TempoMain.java:101)
5at com.example.tempo.TempoMain$1$1.doOnBackground(TempoMain.java:70)
6at com.example.tempo.util.AsyncJob$2.run(AsyncJob.java:59)
7at java.lang.Thread.run(Thread.java:841)
下面是我用来访问它的java类

package com.example.tempo.util;
import android.widget.Toast;

import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;

import java.io.IOException;
import java.net.*;
import java.util.*;



public class SpDatabase {

    WorksheetEntry worksheet;
    SpreadsheetService service;
    String spTitle;

    public SpDatabase()throws AuthenticationException, MalformedURLException, IOException, ServiceException {
                service = new SpreadsheetService("MySpreadsheetIntegration-v1");

                // TODO: Authorize the service object for a specific user (see other sections)

                // Define the URL to request.  This should never change.

                String key = "1arJFUxSghwdv0QpnJJ3lBu36X4I3d_uB4xx_xNGLKHU";


                URL SPREADSHEET_FEED_URL  = FeedURLFactory.getDefault().getWorksheetFeedUrl(key, "public", "basic");


                // Make a request to the API and get all spreadsheets.
                SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
                List<SpreadsheetEntry> spreadsheets = feed.getEntries();

                if (spreadsheets.size() == 0) {
                  // TODO: There were no spreadsheets, act accordingly.
                }

                // TODO: Choose a spreadsheet more intelligently based on your
                // app's needs.
                SpreadsheetEntry spreadsheet = spreadsheets.get(0);
                spTitle = spreadsheet.getTitle().getPlainText();

                // Make a request to the API to fetch information about all
                // worksheets in the spreadsheet.
                List<WorksheetEntry> worksheets = spreadsheet.getWorksheets();
//              WorksheetEntry worksheet = worksheets.get(0);
                worksheet = worksheets.get(0);

    }

    public int getRowCount(){
        int rowCount = worksheet.getRowCount();

        return rowCount;
    }
    public int getColumnCount(){
        int columnCount = worksheet.getColCount();

        return columnCount;
    }

    public ArrayList<CellEntry> getAllCells() throws IOException, ServiceException{
          // Fetch the cell feed of the worksheet.
        URL cellFeedUrl = worksheet.getCellFeedUrl();
        CellFeed cellFeed = service.getFeed(cellFeedUrl, CellFeed.class);

        return (ArrayList<CellEntry>) cellFeed.getEntries();
    }
                            // or getRow() or getCollumn()
    public ArrayList<CellEntry> getSpecificCells(int minRow, int maxRow, int minCol, int maxCol) throws IOException, ServiceException, URISyntaxException{

        //example: Fetch column 4, and every row after row 1 --> "?min-row=2&min-col=4&max-col=4"
        String pref =  "?";
        if(minRow != -1){
            pref = appendAndIfFirstValue(pref);
            pref += "min-row=" + minRow;
        }
        if(minCol != -1){
            pref = appendAndIfFirstValue(pref);
            pref += "&min-col=" + minCol;
        }

        if(maxRow != -1){
            pref = appendAndIfFirstValue(pref);
            pref += "&max-row=" + maxRow;
        }
        if(maxCol != -1){
            pref = appendAndIfFirstValue(pref);
            pref += "&max-col=" + maxCol;
        }

          // Fetch column 4, and every row after row 1.
        URL cellFeedUrl = new URI(worksheet.getCellFeedUrl().toString() + pref).toURL();
        CellFeed cellFeed = service.getFeed(cellFeedUrl, CellFeed.class);



        return (ArrayList<CellEntry>) cellFeed.getEntries();


    }

    public int getCellNumValue(CellEntry cell){
        return (Integer) cell.getCell().getNumericValue();
    }

    public String getCellStringValue(CellEntry cell){
        return  cell.getCell().getValue();
    }


    public void incrementCellValue(CellEntry cell) throws IOException, ServiceException{
          String cellID = cell.getTitle().getPlainText();
          cell.changeInputValueLocal("=SUM(" + cellID + ", 1)");
          cell.update();

    }
    public void changeCellValue(CellEntry cell, String value) throws IOException, ServiceException{
        cell.changeInputValueLocal(value);
        cell.update();
    }
    private String appendAndIfFirstValue(String str){
        if(!str.contains("&")){
            return "&" + str;
        }else{
            return str;
        }

    }

    public String getSpreadSheetTitle() throws IOException, ServiceException, URISyntaxException{

    return spTitle;

    }

}
package com.example.tempo.util;
导入android.widget.Toast;
导入com.google.gdata.client.spreadsheet.*;
导入com.google.gdata.data.spreadsheet.*;
导入com.google.gdata.util.*;
导入java.io.IOException;
导入java.net。*;
导入java.util.*;
公共类数据库{
工作表工作表;
电子表格服务;
字符串标题;
public SpDatabase()引发AuthenticationException、MalformedURLException、IOException、ServiceException{
服务=新的电子表格服务(“MySpreadsheetIntegration-v1”);
//TODO:为特定用户授权服务对象(请参阅其他部分)
//定义要请求的URL。这永远不会更改。
字符串键=“1ARJFUXSGHWDV0QPNJ3LBU36X4I3D\U uB4xx\U xNGLKHU”;
URL电子表格_FEED_URL=FeedURLFactory.getDefault().getWorksheetFeedUrl(键“public”、“basic”);
//向API发出请求并获取所有电子表格。
SpreadsheetFeed=service.getFeed(电子表格\u feed\u URL,SpreadsheetFeed.class);
列表电子表格=feed.getEntries();
如果(电子表格.size()==0){
//TODO:没有电子表格,请相应地采取行动。
}
//TODO:根据您的需求更智能地选择电子表格
//应用程序的需求。
电子表格输入电子表格=电子表格.get(0);
spTitle=spreadsheet.getTitle().getPlainText();
//向API发出请求以获取有关所有API的信息
//电子表格中的工作表。
列表工作表=电子表格。getWorksheets();
//WorksheetEntry工作表=worksheets.get(0);
工作表=工作表。获取(0);
}
public int getRowCount(){
int rowCount=worksheet.getRowCount();
返回行计数;
}
public int getColumnCount(){
int columnCount=工作表.getColCount();
返回列计数;
}
公共ArrayList getAllCells()引发IOException,ServiceException{
//获取工作表的单元格提要。
URL cellFeedUrl=工作表。getCellFeedUrl();
CellFeed CellFeed=service.getFeed(cellFeedUrl,CellFeed.class);
return(ArrayList)cellFeed.getEntries();
}
//或getRow()或getCollumn()
公共ArrayList getSpecificCells(int-minRow、int-maxRow、int-minCol、int-maxCol)抛出IOException、ServiceException、URISyntaxException{
//示例:获取第4列,以及第1行之后的每一行-->“?最小行=2&最小列=4&最大列=4”
字符串pref=“?”;
如果(minRow!=-1){
pref=附录和FFirstValue(pref);
pref+=“最小行=“+minRow;
}
if(minCol!=-1){
pref=附录和FFirstValue(pref);
pref+=”&min col=“+minCol;
}
如果(maxRow!=-1){
pref=附录和FFirstValue(pref);
pref+=”&max row=“+maxRow;
}
如果(maxCol!=-1){
pref=附录和FFirstValue(pref);
pref+=”&max col=“+maxCol;
}
//获取第4列以及第1行之后的每一行。
URL cellFeedUrl=新URI(工作表.getCellFeedUrl().toString()+pref.toURL();
CellFeed CellFeed=service.getFeed(cellFeedUrl,CellFeed.class);
return(ArrayList)cellFeed.getEntries();
}
public int getCellNumValue(CellEntry单元格){
返回(整数)cell.getCell().getNumericValue();
}
公共字符串getCellStringValue(CellEntry单元格){
返回单元格.getCell().getValue();
}
public void incrementCellValue(CellEntry单元格)引发IOException、ServiceException{
字符串cellID=cell.getTitle().getPlainText();
cell.changeInputValueLocal(“=SUM(“+cellID+”,1)”);
cell.update();
}
public void changeCellValue(CellEntry单元格、字符串值)引发IOException、ServiceException{
单元格.changeInputValueLocal(值);
cell.update();
}
私有字符串appendandifirstvalue(字符串str){
如果(!str.contains(“&”)){
返回“&”+str;
}否则{
返回str;
}
}
公共字符串getSpreadSheetTitle()引发IOException、ServiceException、URISyntaxException{
归还所有权;
}
}

事实证明,问题在于投影的范围,根据谷歌所说的文档,电子表格提要只支持“私有”可见性和“完整”投影。

我使用的是
'public'
'basic'
因此,我解决这个问题的方法是直接访问工作表(支持更多可见性参数),如下所示:

public SpDatabase()throws AuthenticationException, MalformedURLException, IOException, ServiceException {
        service = new SpreadsheetService("Test");

        FeedURLFactory factory = FeedURLFactory.getDefault();

        String key = "1arJFUxSghwdv0QpnJJ3lBu36X4I3d_uB4xx_xNGLKHU";
        URL spreadSheetUrl = factory.getWorksheetFeedUrl(key, "public", "full");
        WorksheetFeed feed = service.getFeed(spreadSheetUrl, WorksheetFeed.class);

        worksheet = feed.getEntries().get(0);
        URL cellFeedURL = worksheet.getCellFeedUrl();
        CellFeed cellFeed = service.getFeed(cellFeedURL, CellFeed.class);

}

全部张贴实际错误。上面哪行代码导致了错误?@RobertHarvey我现在已经发布了错误。根据我认为的错误,可能没有任何数据表明您可能错误地创建了数据库,或者没有正确导入数据。建议您列出电子表格和工作表,如下:谷歌示例:gdata-java-client.googlecode.com/svn-history/r51/trunk/java/sample/spreadsheet/cell/CellDemo.java,但同意KRR loo的观点