Java excel static/non static help

Started by
6 comments, last by SimonForsman 12 years, 6 months ago
Sorry if this is a really nub/stupid question but I'm trying to simply read in an excel file with Java.

This is my code:


import java.io.File;
import java.util.Date;
import jxl.*;

public class EXCEL{

public static void main(String args[]){
EXCEL testEX = new EXCEL();
}

Workbook test1;
Sheet sheet1;

Cell a5;

String stringa5;

public EXCEL(){

try{

test1 = Workbook.getWorkbook(new File("EXCEL.xls"));
sheet1 = Workbook.getSheet(0); //line that's giving me the error
}catch
(Exception e){
System.out.println("Not working");
}

a5 = sheet1.getCell(0,5);

stringa5 = a5.getContents();

}
}

This is the error I get:

"non-static method getSheet(int) cannot be referenced from a static context" error.

I tried reading several mini sites where people asked the same question but redundantly they all say something along the lines of not being able to call non static from static and I have to either make everything static or make non static methods. Very confusing as I don't know what is what. So if anyone could explain with simple examples so I can understand better it'd be really helpful thanks.
Advertisement

Sorry if this is a really nub/stupid question but I'm trying to simply read in an excel file with Java.

This is my code:


import java.io.File;
import java.util.Date;
import jxl.*;

public class EXCEL{

public static void main(String args[]){
EXCEL testEX = new EXCEL();
}

Workbook test1;
Sheet sheet1;

Cell a5;

String stringa5;

public EXCEL(){

try{

test1 = Workbook.getWorkbook(new File("EXCEL.xls"));
sheet1 = Workbook.getSheet(0); //line that's giving me the error
}catch
(Exception e){
System.out.println("Not working");
}

a5 = sheet1.getCell(0,5);

stringa5 = a5.getContents();

}
}

This is the error I get:

"non-static method getSheet(int) cannot be referenced from a static context" error.

I tried reading several mini sites where people asked the same question but redundantly they all say something along the lines of not being able to call non static from static and I have to either make everything static or make non static methods. Very confusing as I don't know what is what. So if anyone could explain with simple examples so I can understand better it'd be really helpful thanks.


getSheet is not a static method, you can't call it without providing a class instance, it should be test1.getSheet(0); not Workbook.getSheet(0);

(static methods don't act on instances of a class and thus they are called by doing ClassName.method(arguments) , non static methods operate on instances and thus you call them using instanceOfClass.method(arguments)

(test1 = Workbook.getWorkbook(...) creates an instance of the Workbook class using the static getWorkbook method)

I'd also recommend against creating instances of the class containing your main function, its just .. ugly. (Personal opinion).
excel.java

public class Excel {
private Workbook test1;
private Sheet sheet1;

private Cell a5;

private String stringa5;

public Excel(){ //Java classnames tend to be in UpperCamelCase while instance, variable and method names are lowerCamelCase

try{

test1 = Workbook.getWorkbook(new File("EXCEL.xls"));
sheet1 = Workbook.getSheet(0); //line that's giving me the error
}catch
(Exception e){
System.out.println("Not working");
}

a5 = sheet1.getCell(0,5);

stringa5 = a5.getContents();
}
}


ExcelTest.java (i usually name the mainclass after the program having it contain nothing apart from the main method (This might just be due to my C++ background though, i'm not sure whats idiomatic Java )

public class ExcelTest {
static void main(String args[]) {
Excel testEx = new Excel();
}
}
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
I've never programmed in Java before, nor do I have the environment to test this, but I think I see your issue. Referencing the documentation for the jexcel library http://jexcelapi.sou...l/Workbook.html the getWorkbook() is a static member of the Workbook class, which is fine but the getSheet() is not static. Are you familiar with the static keyword?

Lets look at the offending lines of code.
test1 = Workbook.getWorkbook(new File("EXCEL.xls"));
sheet1 = Workbook.getSheet(0); //line that's giving me the error

Line one is fine, as was said but, since the getSheet() method is not a static method, it needs an instance of Workbook to call it; not the Workbook class itself. Line two should be as follows
sheet1 = test1.getSheet(0); //line that's no longer giving me the error
Oddly enough, further down the code, you correctly use a non-static method when you write
a5 = sheet1.getCell(0,5);

getSheet is not a static method, you can't call it without providing a class instance, it should be test1.getSheet(0); not Workbook.getSheet(0);

(static methods don't act on instances of a class and thus they are called by doing ClassName.method(arguments) , non static methods operate on instances and thus you call them using instanceOfClass.method(arguments)



Thanks, this cleared up some things but lead me to more questions at the same time.

So after reading your post, I would assume that all the methods in the program, the .getWorkbook(), .getSheet(), .getContents(), .getCell() would all be non static since they're all not ClassName.method(arg) but instead they're all methods from another class somewhere in the jxl.api.

But then you said



I don't know if maybe I'm understanding it wrong but on top you said .getSheet is a non static method but then why whould .getWorkbook be static? Aren't they both methods being called from another class rather than ClassName.method(arg);?

[quote name='SimonForsman']
I'd also recommend against creating instances of the class containing your main function, its just .. ugly. (Personal opinion).


by this you mean the "Excel testEX = new Excel();" ?
[font="Arial"] [/font]

I've never programmed in Java before, nor do I have the environment to test this, but I think I see your issue. Referencing the documentation for the jexcel library http://jexcelapi.sou...l/Workbook.html the getWorkbook() is a static member of the Workbook class, which is fine but the getSheet() is not static. Are you familiar with the static keyword?

Lets look at the offending lines of code.
test1 = Workbook.getWorkbook(new File("EXCEL.xls"));
sheet1 = Workbook.getSheet(0); //line that's giving me the error

Line one is fine, as was said but, since the getSheet() method is not a static method, it needs an instance of Workbook to call it; not the Workbook class itself. Line two should be as follows
sheet1 = test1.getSheet(0); //line that's no longer giving me the error
Oddly enough, further down the code, you correctly use a non-static method when you write
a5 = sheet1.getCell(0,5);


Ok alright I understand it a bit better now.

Non static methods needs to be called from an instance of the class, whereas static methods can be called using the class itself.

So how do you tell the difference between a static and a non static method?

.getSheet and .getWorkbook I would assume comes from the same class and in my eyes I can see no difference as to how you can tell what is static and non static.

Thanks.

[quote name='AverageMidget' timestamp='1318014998' post='4870229']
I've never programmed in Java before, nor do I have the environment to test this, but I think I see your issue. Referencing the documentation for the jexcel library http://jexcelapi.sou...l/Workbook.html the getWorkbook() is a static member of the Workbook class, which is fine but the getSheet() is not static. Are you familiar with the static keyword?

Lets look at the offending lines of code.
test1 = Workbook.getWorkbook(new File("EXCEL.xls"));
sheet1 = Workbook.getSheet(0); //line that's giving me the error

Line one is fine, as was said but, since the getSheet() method is not a static method, it needs an instance of Workbook to call it; not the Workbook class itself. Line two should be as follows
sheet1 = test1.getSheet(0); //line that's no longer giving me the error
Oddly enough, further down the code, you correctly use a non-static method when you write
a5 = sheet1.getCell(0,5);


Ok alright I understand it a bit better now.

Non static methods needs to be called from an instance of the class, whereas static methods can be called using the class itself.

So how do you tell the difference between a static and a non static method?

.getSheet and .getWorkbook I would assume comes from the same class and in my eyes I can see no difference as to how you can tell what is static and non static.

Thanks.
[/quote]

You can see if a method is static or not by looking at the API documentation:

Here is the documentation for the Workbook class:

http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/Workbook.html

as you can see the createWorkbook, getWorkbook and getVersion methods are static, the rest are non static.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!


You can see if a method is static or not by looking at the API documentation:

Here is the documentation for the Workbook class:

http://jexcelapi.sou...l/Workbook.html

as you can see the createWorkbook, getWorkbook and getVersion methods are static, the rest are non static.


Oh ok, so you can't tell just by looking at it. You always have to check the API accordingly to find out if something is static or not. Ok thanks a lot!

[quote name='SimonForsman' timestamp='1318016858' post='4870241']
You can see if a method is static or not by looking at the API documentation:

Here is the documentation for the Workbook class:

http://jexcelapi.sou...l/Workbook.html

as you can see the createWorkbook, getWorkbook and getVersion methods are static, the rest are non static.


Oh ok, so you can't tell just by looking at it. You always have to check the API accordingly to find out if something is static or not. Ok thanks a lot!
[/quote]

Your IDE might be able to pull out the relevant information for you aswell.(Try moving your mouse over the method in the code or in the autocomplete list and see if it says anything for example)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement