我试图从java中的excel获取数据。如果我执行它作为正常的java程序我从excel获取数据,它将在控制台中打印。但是当我把它放在我的Robotium测试项目时,我得到空指针异常(文件或目录未找到)。
public void testCanOpenSettings() throws IOException, Exception {
loginvalid();
}
public void Eula() {
if(solo.searchText("Accept"))
{
System.out.println(" Eula accept");
solo.clickLongOnText("Accept");
}
}
public void loginvalid() throws IOException {
String username;
String password;
solo.sendKey(Solo.MENU);
solo.sleep(3000);
solo.clickLongOnText("Sign In");
solo.sleep(3000);
//solo.clearEditText(0);
username = Readexcel(0,0);
password = Readexcel(0,1);
solo.enterText(0,username);
solo.enterText(1, password);
solo.clickOnCheckBox(0);
solo.clickLongOnText("Sign In");
}
public static String Readexcel(int row,int col) throws IOException {
String filename = "C://Documents//Robo//robo1.xls";
String s = "";
try
{
FileInputStream fis = new FileInputStream(filename);
HSSFWorkbook myWorkBook = new HSSFWorkbook(fis);
HSSFSheet mySheet = myWorkBook.getSheetAt(0);
HSSFRow row1 = mySheet.getRow(row);
HSSFCell cell1 = row1.getCell(col);
s = cell1.getRichStringCellValue().getString();
System.out.println("Cell 1 : "+s);
Log.d("cell value",s );
System.out.println(s);
return s;
}
catch (IOException e) {
e.printStackTrace();strong text
return null;
}
}
}
答案 0 :(得分:1)
您正在阅读存储在您的手机无法访问的计算机HardDrive上的文件C://Documents//Robo//robo1.xls
。您需要将文件放在手机SD卡上,然后使用uri到该位置:
File storage = Environment.getExternalStorageDirectory() //Returns sd-card
String filename = "robo1.xls"; //file name, may have path separators
File myfile = new File(storage, filename);
//myfile is now a File object that you can use, to get the path, use myfile.getAbsolutePath()