I'm trying to run a simple program that read info from a file and
displays it on the screen. However, I am constantly getting the
following error.
==================================================================================
Exception in thread "main" java.lang.Error: Unresolved compilation
problems:
Cannot make a static reference to the non-static field tokenizer
Cannot make a static reference to the non-static field tokenizer
==================================================================================
I don't want to declare any variables static since I don't think I
need to. My code is given below.
================================================================
File myFile = new File("Info.txt");
Scanner tokenizer = new Scanner(myFile);
String str = null;
public static void main(String[] args)
{
while (tokenizer.hasNextLine())
{
System.out.println(tokenizer.next());
}
}
=========================================================================
I was getting a similar error when I was trying to read in standard
input from the user i.e. using input stream reader and buffer
reader.
Can anyone please help?
Knute Johnson - 29 Aug 2007 04:29 GMT
> I'm trying to run a simple program that read info from a file and
> displays it on the screen. However, I am constantly getting the
[quoted text clipped - 32 lines]
>
> Can anyone please help?
All the code in main() is static. Your Scanner, tokenizer, is an
instance variable of the class. The simplest solution is to make
tokenizer static or put its declaration inside main() and it will be static.

Signature
Knute Johnson
email s/nospam/knute/
Hal Rosser - 29 Aug 2007 05:24 GMT
> I'm trying to run a simple program that read info from a file and
> displays it on the screen. However, I am constantly getting the
[quoted text clipped - 32 lines]
>
> Can anyone please help?
Hi
If you declare your variable "tokenizer" inside your main method, it
should resolve that error.
HTH
Andreas Leitgeb - 29 Aug 2007 11:29 GMT
> Cannot make a static reference to the non-static field tokenizer
>================================================================
> File myFile = new File("Info.txt");
> Scanner tokenizer = new Scanner(myFile);
> String str = null;
These variables are declared non-static, so they exist only
in the context of an instance of your class.
> public static void main(String[] args)
As you see, "main" *is* static (and needs to be, if you
want it to be a program's entry-point.)
> while (tokenizer.hasNextLine())
> {
> System.out.println(tokenizer.next());
> }
to solve it,
either you create an instance of your class inside main():
MyClass inst = new MyClass();
before your while-loop and then acess the tokenizer as:
inst.tokenizer
or, you do declare the tokenizer (and also the variable myFile) static,
which I'd recommend for stuff as simple as the example
(which, otoh, I guess your real task at hand isn't)
or, which would be the most elegant:
move the while-loop into a non-static method (say myMethod() )
and inside static main(), you do only: new MyClass().myMethod();
Roedy Green - 29 Aug 2007 12:47 GMT
On Wed, 29 Aug 2007 03:08:42 -0000, TheTravellingSalesman
<saad.zaman@gmail.com> wrote, quoted or indirectly quoted someone who
said :
>Cannot make a static reference to the non-static field tokenizer
see
http://mindprod.com/jgloss/compileerrormessages.html#NONSTATICCANTBEREF

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
TheTravellingSalesman - 30 Aug 2007 02:16 GMT
On Aug 29, 7:47 am, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Wed, 29 Aug 2007 03:08:42 -0000, TheTravellingSalesman
> <saad.za...@gmail.com> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 6 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com
Thanks a lot eh!. This really helps. It works fine now