erp erp - 2 months ago 7
Java Question

Can't call non static method

I am trying to use this

wavRead(filename)
but am getting the message
cannot make a static reference to a non static method
.

I could simply make it static and that solves my problem, but how would do it without going that route. I would like to keep the method non static.

Here is a bit of the code to let you see whats going on:

public class Sound {

double [] mySamples;
public static void main(String[] args){

String filename = null;
System.out.println("Type the filename you wish to act upon.");
Scanner scanIn = new Scanner(System.in);
filename = scanIn.next();
wavRead(filename);


}
public void wavRead(java.lang.String fileName){
mySamples = WavIO.read(fileName);
}

Answer

Create an instance of your class

public static void main(String[] args){

    String filename = null;
    System.out.println("Type the filename you wish to act upon.");
    Scanner scanIn = new Scanner(System.in);
    filename = scanIn.next();
    Sound sound = new Sound();
    sound.wavRead(fileName);
}

It's an instance method, it requires an instance to access it. Please go through the official tutorials on classes and objects.