繁体   English   中英

在方法中使用实例变量Linkedlist()-为什么必须在方法主体中添加新变量

[英]Using instance variable Linkedlist() in method - why do I have to make new in method body

这是我在这里的第一篇文章。 最近,我开始对学习Java感兴趣,阅读了一些初学者级的教程,将http://docs.oracle.com保留为我的书签,并阅读了一些示例代码。

现在,我把自己弄乱了,去实践,我发现了一些奇怪的东西,在手册/教程/文档中找不到满意的答案。

我制作了一个小班来练习IO和队列样式对象。 它旨在创建一个包含文件名和一个空链表的对象。 然后,它具有一种读取给定文件的方法,并将该文件中的行逐一添加到链表队列中。

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.util.LinkedList;


    public class Handle
    {
    public File filehandle;
    public LinkedList<String> queue;

    Handle (File filename)
        {
        filehandle=filename;
        LinkedList<String> queue = new LinkedList<String>();    
        }

    public void addq()
        {
        try{
            FileReader ava;
            ava = new FileReader(filehandle);
//without initializing new linekedlist queue it'll give NPE in queue.add
//why can't it use class/instance variable queue it does fine with filehandle
            queue = new LinkedList<String>();
                BufferedReader br = null;
            String sCurrentLine;
            br = new BufferedReader(ava);
            while ((sCurrentLine = br.readLine()) != null) 
                {
                queue.add(sCurrentLine);
                }
            queue.offer("POISON");
            }
        catch (IOException e) {e.printStackTrace();}
        }

奇怪的是-当尝试使用在类中声明的类变量/实例变量队列(公共LinkedList队列)时,它也在构造函数中启动,在方法内部,它可以很好地编译,但在运行时将NPE放入queue.add行。 当我在方法内部初始化方法变量队列时,NPE消失了。 为什么该方法不能添加到类变量队列中? 似乎使用fielhandle变量就好了! 同样,如poll方法所示,代码也会运行该类(将其张贴下来)-似乎实际上仍在将行添加到实例变量队列中,而不仅仅是临时方法变量。 (这当然很好,但是我不知道如何以及为什么)这是我用来运行Handle类的代码。

    import java.io.File;
    import java.util.LinkedList;
    class Runner
    {

      public static void main(String[] args)
        {
        File file = new File("proovidest.csv");
        Handle handle =new Handle(file);
//using the constructor, now we have object with filehandle and empty queue variables
        handle.addq();
        String mison;
//so apparently the instance variable queue is still filled with lines (good) 
//but how? the method had to delcare its own variable (why), but still the class field is //now filled? how?
        while ((mison = handle.queue.poll()) != "POISON")
        {System.out.println(mison);}

        }
    }

因此,任何人都可以给我很好的解释,尽管我能够使用filehandle变量,但为什么在运行时无法在方法中访问类变量队列。 我应该怎么做才能访问它? 有人可以告诉我,尽管我在方法内部声明了一个新变量,但仍然如何填充类字段队列。 还是handle.queue.poll以某种方式检测方法中的变量?

问题在这里:

Handle (File filename) {
  filehandle=filename;
  LinkedList<String> queue = new LinkedList<String>();    
}

您无需初始化实例字段queue ,而是创建一个具有相同名称的新局部变量,该局部变量仅在构造函数中有效。 更改为:

Handle (File filename) {
  filehandle=filename;
  queue = new LinkedList<String>();    
}

而且它不应该抛出NPE。

在构造函数内部,您声明了一个局部变量队列,隐藏了您的类变量!

Handle (File filename)
        {
        filehandle=filename;
        this.queue = new LinkedList<String>();    
        }

问题是您的LinkedList的可见性。 它仅在您的私有构造函数中可见。 要使用队列LinkedList只需在构造函数中编写以下代码:

queue = new LinkedList<String>(); 

另外在addq删除它:

queue = new LinkedList<String>();

看起来没有地方在您的代码上触发NPE。 如果提到的文件不在适当的位置,它将引发File not found异常。

您可以将堆栈跟踪发布到更多调查中吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM