-1

Here is my code in question:

public File openInputFile(Scanner kb)
{
  if(kb == null)
  {   
     throw new RuntimeException("ERROR! Scanner Not Detected.");
  }
  System.out.print("Enter Name of File: ");
  String fn = kb.nextLine();
  File inf = FileUtil.openInputFile(fn); 
  return inf;
}

I am getting the following error:

FileUtil.java:33: error: non-static method openInputFile(String) cannot be referenced from a static context
      File inf = FileUtil.openInputFile(fn); 
                         ^

Nothing is static, so I'm not sure why I'm getting this error. Can anyone help?

2 Answers2

0

File inf = FileUtil.openInputFile(fn); tells the compiler that openInputFile(fn) is static method of class FileUtil because you are trying to make a call to the method from the class name itself. This is why compiler said

FileUtil.java:33: error: non-static method openInputFile(String) cannot be referenced from a static context

So, all you need to do it is, make an object of file util and call the method from that object.

Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72
0

FileUtil class has a method openInputFile() that seems to be non static, hence you have to call it by creating instance of FileUtil

FileUtil fileut= new FileUtil();
fileut.openInputFile();
Mustafa sabir
  • 4,130
  • 1
  • 19
  • 28