spacing
spacing

Reputation: 780

Error: cannot find symbol of Parser

So I have a class parser that goes something like this

public class Parser implements Serializable {

     //parse Programfile with name=filename
     public Program parseProgramFile(String filename){
         /*method defined*/
         return Program
     }

And on my main I am calling the parser to pass its return through another class constructor like this

public static void main(String[] args) {
      Manager manager = new Manager();
      String datafile = System.getProperty("import");
      if (datafile != null) {
        try {
    //Import file into Manager through Parser instance
    manager(parseProgramFile(datafile));

And this is where I keep getting it wrong

error: cannot find symbol
manager(parseProgramFile(datafile));
        ^

I'm really not sure of what I am doing wrong here. Is it the instantiation of parser gone wrong? I am currently creating a parser object in the manager constructor.

Upvotes: 0

Views: 3865

Answers (2)

nail fei
nail fei

Reputation: 2329

I dont understand what you mean completely. And I assume that the Parser Object is a parameter of your Manager Constructor


then an alternative solution is:

public static void main(String[] args) {
   Manager manager = null;
   String datafile = System.getProperty("import");
   if (datafile != null) {
     try {
         manager = new Manager(new Parser().parseProgramFile(datafile));
     ...

Upvotes: 0

Vasu
Vasu

Reputation: 22462

In Java, static methods (like main()) can directly access/invoke other static members where as to access non-static methods from static methods, you need an object of the class (which holds the non-static methods).

So, you need to create the object of the class Parser and invoke the method parseProgramFile (Option 1) or you can change parseProgramFile method to static (Option 2).

Option(1): Create object for Parser and call from main()

Parser parser = new Parser();
parser.parseProgramFile(parser.datafile);

Option(2): Make parseProgramFile method as static

public static Program parseProgramFile(String filename){
         /*method defined*/
         return Program
     }

and then in your main() you can invoke it as directly manager(parseProgramFile(datafile));

You can refer here for more on static.

Upvotes: 2

Related Questions