可能尚未初始化局部变量rootcreat

时间:2015-06-11 20:44:07

标签: java

我正在尝试返回自定义类Rootcreator的对象,但是我收到此错误The local variable rootcreat may not have been initialized如何初始化我已经初始化它的自定义类。如何让它工作以使join_line方法返回此costum对象?

我感谢任何帮助。

private static RootCreator join_line(String path, String key) {
    RootCreator  rootcreat;
     .
     .
     .
     .
    try (PrintWriter writer = new PrintWriter(path + File.separator
            + newName);

            Scanner scanner = new Scanner(file)) {
        ArrayList<String> buffer = new ArrayList<String>();
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            buffer.add(line);

        }
        ArrayList<String> bufferDirection = new ArrayList<String>();

        String direction = bufferDirection.get(0);
        String route = bufferDirection.get(1);
        String day = bufferDirection.get(2);
        String startString = bufferDirection.get(3);
        String[] startStringSplitt = startString.split(":");
        String firstPart = startStringSplitt[0];
        String otherDirection = firstPart.substring(0,
                firstPart.length() - 2);


        bufferDirection.clear();
        LinkedHashMap<String, List<String>> first = new LinkedHashMap<String, List<String>>();
        LinkedHashMap<String, List<String>> second = new LinkedHashMap<String, List<String>>();

        for (String keyLine : direcList) {
            if (keyLine.startsWith("New direction")) {
                int index = direcList.indexOf(keyLine);
                List<String> firstDirec = direcList.subList(0, index);
                List<String> secondtDirec = direcList.subList(index,
                        direcList.size() - 1); // This part wih New
                                                // Direction.

                ArrayList<String> timeListFirst = new ArrayList<String>();
                for (String mergeLine : firstDirec) {
                    if (!(mergeLine.equals(direction))
                            && !(mergeLine.equals(route))
                            && !(mergeLine.equals(day))) {
                        timeListFirst.add(mergeLine);

                    }
                }
                first = merge_table(timeListFirst);

                ArrayList<String> timeListSecond = new ArrayList<String>();
                for (String mergeLine : secondtDirec) {
                    otherDirection = otherDirection.trim();

                    if (!(mergeLine.equals(otherDirection))
                            && !(mergeLine.equals(route))
                            && !(mergeLine.equals(day))
                            && !(mergeLine.equals("New direction"))) {

                        timeListSecond.add(mergeLine);

                    }
                }
                 second = merge_table(timeListSecond);

            }

        }

      rootcreat = new RootCreator(first, second, route, day, direction, otherDirection);


    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //The error is here.
    return rootcreat;

}

2 个答案:

答案 0 :(得分:2)

使用

RootCreator  rootcreat = null;

如果您的代码(按照书面形式)抛出FileNotFoundExceptionrootcreat将保持未初始化状态。

请注意,local引用(与class变量不同)默认情况下未初始化。

答案 1 :(得分:1)

变量rootcreat可能尚未初始化,因为它仅在try块的最后一行初始化。如果在该点之前抛出异常,则不会初始化rootcreat。然后在return语句中引用该变量,然后保证它被初始化,并且编译器返回错误。

try初始化后立即将return语句移动到rootcreat块中。但是如果抛出异常会返回什么?在这里,什么都不应该归还。应该从该方法抛出异常。这种方法不应该处理异常FileNotFoundException;它应该报告它。删除catch块,并将throws FileNotFoundException添加到方法声明中。这样,创建并返回rootcreat,或者抛出异常并且调用者需要捕获它。