While writing my program I got “cannot find symbol message” when I tried to invoke the method setTextArea() from another class. After compiling the program I am getting the following error message:
Uncompilable source code – Erroneous sym type: gui.setTextArea
Here is my code:
public class LinkExtractor {
public static void main(String[] args) throws IOException {
//Validate.isTrue(args.length == 1, "usage: supply url to fetch");
String base = "http://uk.ask.com/web?q=";
String keyword ="flowers";
String tale="&search=&qsrc=0&o=312&l=dir";
String url =base+keyword+tale;
print("Fetching %s...", url);
Document doc = Jsoup.connect(url).get();
Elements links = doc.select("a[href]");
print("\nLinks: (%d)", links.size());
for (Element link : links) {
print(" * a: <%s> (%s)", link.attr("abs:href"), trim(link.text(), 35));
AssistantGUI gui=new AssistantGUI();
}
}
public static void print(String msg, Object... args) {
***//here is the problem line***
gui.setTextArea(String.format(msg, args));
}
private static String trim(String s, int width) {
if (s.length() > width)
return s.substring(0, width-1) + ".";
else
return s;
}
}
And here is my second class:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class AssistantGUI {
JFrame frame= new JFrame("TextArea frame");
JPanel panel=new JPanel();
JTextArea text= new JTextArea("",5,20);
public AssistantGUI(){
frame.add(panel);
panel.add(text);
frame.setSize(250,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void setTextArea(String myString){
text.append(myString);
}
public static void main(String[] args){
AssistantGUI gui= new AssistantGUI();
gui.setTextArea("Hello");
}
}
I read many posts with a similar issue, but couldn’t find a solution. Everything looks fine to me. I am creating an instance of AssistantGUI and then I am using it to call the method setTextArea(), why it does not recognise it? Any ideas? I will really appreciate your help.
There is now a correct answer of @RameshK. In Object Oriented style: do something like this (making things non-static):