-2

I currently have a class Printer which is accessed statically, but when I try to compile the project after adding a function to the class I get error: cannot find symbol.

I know this is generally caused by typos, out-of-scope references and bad declarations, but the odd thing here is that the old methods work just fine.

This code has exactly the same structure as my own code, and it works:

import java.util.*;
import java.lang.*;
import java.io.*;

class Printer {
    private static String errorTitle;
    private static String regularTitle;

    Printer(String regularTitle_) {
        errorTitle = "Some error: ";
        regularTitle = regularTitle_;
    }

    public static void printError(Exception e) {
        System.out.println(errorTitle + e.getMessage());
    }

    public static void print(String message) {
        System.out.println(regularTitle + message);
    }
}

class Main {
    public static void main(String[] args) {
        new Printer("Message: ");

        try {
            throw new Exception();
        }
        catch(Exception e) {
            //This works
            Printer.print(e.toString());

            //This generates a cannot find symbol error when compiling
            Printer.printError(e);
            //     ^ here
        }
    }
}

The complete error message is:

[javac] Compiling 1 source file to C:\Javaprojects\MyProject\alpha\build
[javac] C:\Javaprojects\MyProject\alpha\src\Main.java:35 error: cannot find symbol
[javac] Printer.printError(e);
[javac]        ^
[javac] symbol: method printError(Exception)
[javac] location: class Printer
[javac] 1 error

This works fine if I change Printer.printError(e) to Printer.print(e.toString()).

What can possibly be the cause of this? Could it be that I am referring to some sort of cached version of the compiled class?

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
sweerpotato
  • 470
  • 2
  • 11
  • 22

3 Answers3

2

Your code is ok, program flow does not, but not the problem here. Only thing not necessary are the imports,


Remove imports:

import java.util.*;
import java.lang.*;
import java.io.*;

Clean the project, and rebuild it.


If this does not work, start eclipse in clean mode, clean and rebuild project again.


ADD-ON

The problem is solved. In fact, it were the * imports which caused the compilation error.
I could access Printer.print(String) but not Printer.print(Exception).
Why was this the case?

As long as I cannot find any Printer class in util, lang or io packages, only scenario I can imagine you get this error, is: Eclipse was importing a previous version of your Printer class where the method Printer.printError(Exception) didn't exists yet, sum this to imports causing bad building so you have problem persisting for several cleanings.

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • I have tried removing imports, cleaning the project and rebuilding it. The problem still persists though – sweerpotato Sep 18 '15 at 07:59
  • The problem is solved. In fact, it *were* the * imports which caused the compilation error. If this would be my question I'd give this answer the check. – Binkan Salaryman Sep 18 '15 at 08:34
  • @JordiCastilla why were the * imports causing problems? I could access `Printer.print(String)` but not `Printer.print(Exception)`. Why was this the case? – sweerpotato Sep 18 '15 at 08:52
  • As long as I cannot find any `Printer` class in `util`, `lang` or `io` packages, only scenario I can imagine you get this error, is: Eclipse was importing a previous version of your `Printer` class where the method `printError(Exception)` didn't exists yet, sum this to imports causing bad building so you have problem persisting for several cleanings... – Jordi Castilla Sep 18 '15 at 09:17
  • 1
    I see! Thank you very much for the explanation! – sweerpotato Sep 18 '15 at 11:10
2

What can possibly be the cause of this?

Unclear. It could potentially be many things ... including some kind of misconfiguration or corruption of your IDE's workspace, etc. Or an IDE bug.

Could it be that I am referring to some sort of cached version of the compiled class?

It could be.


The code you have shown us seems to consist of two classes in the same source file. This is legal, but not recommended, and it could be part of the cause of your problems.

I suggest that you put the two classes into separate source files (if they aren't already).


Apart from that, I don't think there is enough information for a proper diagnosis. And I see from the comments that people are unable to reproduce your problem.

(I don't see how removing the unnecessary imports would make any difference. There is no Printer class in those packages that could be imported by accident.)


A couple of off-the-wall ideas:

  • You haven't declared your own class called Exception, have you?

  • You aren't being bitten by a "homoglyph" problem in your source code, are you?

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

Just rename your class Printer to something else and it should work.

zulkarnain shah
  • 971
  • 9
  • 20