-1

I am trying to call a non-static method from one file to another, but I am getting the error Cannot find symbol.

Code in TestFile

package project2;
public class TestFile{
     private boolean[ ][ ] edges;
     public void test(int x, int y){
          edges[x][y] = true;
     }
}

The code in the main file in which I am trying to using the method

package project2;
public static void main(String[] args){  
     TestFile test = new test();
}

I have tried changing parts of the code but I am unable to fix it. I cannot change the code in the TestFile so it's something wrong on my end.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

0

To call an instance method, you first need to create an instance of the class, then you can call a method of that instance. And as your method accepts parameters, you need to pass some values for x and y.

  TestFile test = new TestFile();
  test.test(0, 0);

I am assuming that the code provided is not the full code, cause you'll get NullPointException when trying to set edges[x][y], because edges array is not instantiated.

You might want to create edges with something like

private boolean[][] edges = new boolean[5][5];

depending on how many edges you need.

I would also recommend reading some books on Java.

Edit

It is unclear what exactly is the error, so here is working example

File TestFile.java

package package2;
public class TestFile
{
  private boolean[][] edges = new boolean[5][5];

  public void test(int x, int y) {
    edges[x][y] = true;
  }
}

File App.java

package package2;
class App
{
  public static void main(String[] args) {
    TestFile test = new TestFile();
    test.test(0, 0);
  }
}
uaraven
  • 1,097
  • 10
  • 16
  • I have edges created I just forgot to add it into the post. test.test(0,0); works fine but the line TestFile test = new TestFile(); is what is giving me the error. The "TestFile();" is returning the cannot find symbol. – anonaccount May 01 '21 at 02:01
  • Can you edit your question to include more code? If both your TestFile and the class containing main method are indeed in the same package there should be no problems creating new TestFile() from main() – uaraven May 01 '21 at 02:05
  • There isn't really any more code to add since I'm just starting. The TestFile contains a bunch of other methods but none that would stop test from working. The only other thing I could state is that the edges are created like this: private boolean[ ][ ] edges; Not sure if that changes anything really. – anonaccount May 01 '21 at 02:14
  • is your `main()` method inside a class? What is the exact compiler error? – uaraven May 01 '21 at 02:19
  • Yes my main method is inside a class, the error it gives when compiled is: error: cannot find symbol symbol: class test location: class Project2 Both files are inside the project2 files so I know for a fact it's something wrong with my code. – anonaccount May 01 '21 at 02:29
  • Have you actually changed this line `TestFile test = new test()`? This error means that you're trying to use class `test` when your class is named `TestFile`. Without more code, there's nothing else I can suggest. See the working example in the answer, based on the code you provided. – uaraven May 01 '21 at 02:41
  • I dont know what to say, there isn't anything else to actually show. I didn't change the line. – anonaccount May 01 '21 at 04:53