I am new to java, trying to write a program to access the data from the excel file. But getting message
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot make a static reference to the non-static field
Coding:
package xl;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import jxl.*;
import jxl.Workbook.*;
import jxl.read.biff.BiffException;
public class xl {
public String path = "C:/Workbook.xls";
public File wb = new File(path);
public static void main(String[] args) throws IOException, BiffException {
Workbook work;
work = Workbook.getWorkbook(new java.io.File(wb));
Sheet sheet1 = work.getSheet(0);
Cell c1 = sheet1.getCell(0,0);
String xreader = c1.getContents();
System.out.println(xreader);
}
}
Any ideas, where i am wrong...
The main method is a static method. Your instance variable wb cannot be access in a static method.
This is because instance variable does not exist before you create an object of the class but a static method can always be accessed without an object being created.
To fix, declare both instance variable as static:
Even though, this is not a good design. But it will make it compile at least.