无法比较值

我有一个名为"Read"的类,在这里我正在读取Excel工作表的特定单元格A1的值,然后将该值放入一个variables"Cellresult" 。 在另一个类"Patient"我尝试将文本字段的值与"txtVFNAME"的值进行比较,该值与针对特定单元格创build的variables进行比较。 我正在做如下细胞阅读和variables存储(它工作正常)。

 public class Read { public String Cellresult; public static void main(String[] args) { Workbook workbook = null; Sheet sheet = null; String Cellresult; try { workbook = Workbook.getWorkbook(new File("D:\\rptViewer.xls")); } catch (BiffException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } sheet = workbook.getSheet(0); int columNumber = 0; int rowNumber = 1; Cell a1 = sheet.getCell(columNumber,rowNumber); Cellresult = a1.getContents(); System.out.println(Cellresult); if(a1.getContents().equalsIgnoreCase("HAJI NIGAR")){ System.out.println("Found it"); } } } 

为了比较值,我在下面的"Patient"类中尝试过,但是没有印刷:

 Read compare = new Read (); WebElement a = idriver.findElement(By.id("txtVFNAME")); if (a.equals(compare.Cellresult)){ System.out.println("text is equal"); }; 

我正在使用Java,IE 10,Win 8,Eclipse。

你有两个不同的String Cellresult; 宣布。 main里面的一个是用值初始化的,但是类成员Cellresult仍然是null。 所以当你尝试从另一个class级访问它时,它没有任何价值。

也许删除里面的一个,使外面的一个static

只要删除您的String Cellresult;

在你的主目前你使用Cellresult = a1.getContents();

这当前受到您的本地variablesCellResult 。 在你的比较部分,你指的是不同的属性Cellresult 。 您已经设置了本地variables的值。 你的财产仍然是空的,导致一个比较方法比较没有