如何解决这种类型的编译错误?

时间:2012-08-26 16:54:43

标签: java

import java.net.*;
import java.io.*;

public class DjPageDownloader {


    public URL url;
    public InputStream is = null;
    public DataInputStream dis;
    public String line;

    public static void main(String []args){
        try {
            url = new URL("http://stackoverflow.com/");
            is = url.openStream();  // throws an IOException
            dis = new DataInputStream(new BufferedInputStream(is));

            while ((line = dis.readLine()) != null) {
                System.out.println(line);
            }
        } catch (MalformedURLException mue) {
             mue.printStackTrace();
        } catch (IOException ioe) {
             ioe.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException ioe) {
                // nothing to see here
            }
        }

    }

这在编译时显示错误: -

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Cannot make a static reference to the non-static field url
    Cannot make a static reference to the non-static field is
    Cannot make a static reference to the non-static field url
    Cannot make a static reference to the non-static field dis
    Cannot make a static reference to the non-static field is
    Cannot make a static reference to the non-static field line
    Cannot make a static reference to the non-static field dis
    Cannot make a static reference to the non-static field line
    Cannot make a static reference to the non-static field is

    at djPageDownloader.DjPageDownloader.main(DjPageDownloader.java:16)

5 个答案:

答案 0 :(得分:6)

如果您只是从Main类中调用它们,请将所有字段设置为静态。您无法从静态位置调用实例字段,因为您当前不在实例中。如果你希望保持它们是非静态的,你必须通过将它放在一个构造函数中来创建一个DjPageDownloader实例,并调用它,如下所示:

public DjPageDownloader() {
    try {
        url = new URL("http://stackoverflow.com/");
        is = url.openStream();  // throws an IOException
        dis = new DataInputStream(new BufferedInputStream(is));

        while ((line = dis.readLine()) != null) {
            System.out.println(line);
        }
    } catch (MalformedURLException mue) {
         mue.printStackTrace();
    } catch (IOException ioe) {
         ioe.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException ioe) {
            // nothing to see here
        }
    }
}

然后在主方法中调用它:

public static void main(String []args){
    new DjPageDownloader();//If you use it more later, store to a variable, but atm you aren't
}

答案 1 :(得分:3)

public static URL url;
public static InputStream is = null;
public static DataInputStream dis;
public static String line;

public class DjPageDownloader {

public static void main(String []args){

    URL url;
    InputStream is = null;
    DataInputStream dis;
    String line;
    try {
        url = new URL("http://stackoverflow.com/");
        is = url.openStream();  // throws an IOException
        dis = new DataInputStream(new BufferedInputStream(is));

        while ((line = dis.readLine()) != null) {
            System.out.println(line);
        }
    ...

答案 2 :(得分:1)

您正在通过静态方法url呼叫isdislinemain()字段。所以它们要么必须是静态的,要么必须从非静态方法(如构造函数)调用它们,main()实例化DjPageDownloader类。

答案 3 :(得分:0)

将所有字段声明移至main方法中。你没有使用对象本身,只使用主要方法。

答案 4 :(得分:0)

静态方法只能从方法外部访问静态方法和静态变量。因此,从main方法中,如果要从方法外部访问任何方法或变量,则必须对它们进行静态引用。