【问题标题】:Cannot Find Symbol: Variable >object> of type <Class>?找不到符号:<Class> 类型的变量 >object>?
【发布时间】:2013-10-09 03:41:03
【问题描述】:

我正在处理一些额外的信用项目,当我尝试通过我的程序的自动检查服务器提交它时,它给了我一个 Symbol Not found 错误并停止运行我的代码......我不完全确定原因,因为所有内容似乎都在范围内并且拼写正确。

有什么想法吗?

public class Chap72 {    
    public static void main(String[] args) throws IOException {

        //User inputs url and name of file to create.
        WebReader instance = new WebReader();

        Scanner console = new Scanner(System.in);
        System.out.println("Enter a URL");
        String url = console.nextLine();
        System.out.println("Enter name of file");
        Scanner location = new Scanner(System.in);
        String fileName = location.next();
        String filename = "PARSEDRESULT.txt";

        try {
            //uses both saveURL for the unaltered HTML
            //uses SaveToURLPage for extracting links.
            instance.SaveToURLPage(url, filename);
            instance.saveURL(instance.Navigate(url), fileName);


        } 
        catch (MalformedURLException e) {
            //catches MalformedURLException
            e.printStackTrace();
        }
    }
}

错误:

error: cannot find symbol
instance.SaveToURLPage(url, filename);
        ^
symbol:   method SaveToURLPage(String,String)
location: variable instance of type Chap72
1 error

我不完全确定为什么会收到此错误...

WebReader

public class WebReader implements WebPage {

/**
 *
 * @param url to search through
 * @return pageLocation
 * @throws MalformedURLException
 */
public URL Navigate(String url) throws MalformedURLException {
    //Creates a URL object
    URL pageLocation = new URL(url);
    return pageLocation;
}

/**
 *
 * @param location url hypertext link
 * @param fileName name of text file to save to
 * @throws IOException
 */
public void saveURL(URL location, String fileName) throws IOException {
    Scanner in = new Scanner(location.openStream());
    PrintWriter out = new PrintWriter(fileName);
    //Scans the website
    while (in.hasNextLine()) {
        //prints out Information from URL
        out.println(in.nextLine());

    }
    in.close();
    out.close();
}

/**
 *
 * @param url to search through
 * @param filename to save to
 * @throws IOException
 */
public void SaveToURLPage(String url, String fileName) throws IOException {

    // Creates a new URL object to retreive information.
    URL pageLocation = new URL(url);
    Scanner in = new Scanner(pageLocation.openStream());
    PrintWriter out = new PrintWriter(fileName);

    while (in.hasNext()) {
        //Cycles through each character
        String line = in.next();
        if (line.contains("href=\"http://")) {
            //if it has an <a> tag, the link is extracted
            int from = line.indexOf("\"");
            int to = line.lastIndexOf("\"");
            out.println(line.substring(from + 1, to));
        }
    }
    in.close(); //closes program
    out.close();
}

}

【问题讨论】:

  • 方法不存在?
  • 错误显示“Chap72 类型的变量实例”很可疑。这意味着您有一个名为instance 的变量,其类型为Chap72;换句话说,不是您向我们展示的instance,其类型为WebReader。您是否在其他地方有另一个instance,您尝试调用SaveToURLPage
  • 我不确定你的意思。我创建的唯一实例是 WebReader 类的一个名为 instance 的对象,因此我可以使用 SaveToURLPage 方法。
  • 我不确定这里的问题是什么;就像提到的@ajb 一样,类型应该是WebReader 而不是Chap72。您能否尝试使用this.instance 而不是简单的instance 并告诉我们您得到了什么?
  • 你说的“自动检查服务器”是什么?

标签: java syntax symbols


【解决方案1】:

这个错误意味着没有这样的方法SaveToURLPage接受两个String对象作为WebReader类中声明的参数。如需进一步帮助,您需要发布 WebReader 类的内容(或 JavaDoc),供我们查看。

【讨论】:

  • 用 WebReader 类更新了 OP。
猜你喜欢
  • 2016-04-30
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 2019-04-10
  • 2015-09-14
  • 2016-07-01
  • 2016-03-27
  • 2010-10-12
相关资源
最近更新 更多