Exception while accessing the Excel file

1.5k Views Asked by At

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...

3

There are 3 best solutions below

0
On BEST ANSWER

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:

private static String path = "C:/Workbook.xls";
private static File wb = new File(path);

Even though, this is not a good design. But it will make it compile at least.

0
On
 public class xl 
 {
     public static void main(String[] args) throws IOException, BiffException {
          String path = "C:/Workbook.xls";
          File wb = new File(path);
          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);
   }

}

0
On

The main method must be a static method but it cannot reference any instance variables as they are not static. You could change the instance variables to be class variables by declaring them as static but this is not good practice as in this case the variables correspond to a particular instance of the workbook.

A better solution would be to create an instance method which loads the workbook and prints out the contents. All the code from your current main method should go into this new method.

Then in the main method, you just need to create an instance of your class, and call the new method.

public void printWorkbook() 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);    
}

public static void main(String[] args) throws IOException, BiffException {
    xl instance = new xl();
    instance.printWorkBook();
}

Even better would be to let printWorkbook accept a String parameter for the path and then pass this in from the main method. You would then create the File object wb as a local variable of this method. You could then easily generalise your main method to print out any file by reading the path in as an argument from the command line.

It is also usual to give classes names that start with a capital letter and define the purpose of the class. So instead of xl you could call your class WorkbookPrinter.