static reference in a non-static method

  • Thread starter TheTravellingSalesman
  • Start date
T

TheTravellingSalesman

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?
 
K

Knute Johnson

TheTravellingSalesman said:
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?

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

Hal Rosser

TheTravellingSalesman said:
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?

Hi
If you declare your variable "tokenizer" inside your main method, it
should resolve that error.
HTH
 
A

Andreas Leitgeb

TheTravellingSalesman said:
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();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,979
Messages
2,570,184
Members
46,721
Latest member
DollieSawt

Latest Threads

Top