我收到了NullPointerException

时间:2014-12-30 23:41:16

标签: java file nullpointerexception

这是我的代码:

    public static List<String> matched_file_names=null;
//Constructor
public compareTwoTextualFiles() {
}

public String compareFiles(File File1, File File2) throws IOException {

    try {
        // Create FileReader & Writer Objects.
        FileReader File1Reader = new FileReader(File1.getPath());
        FileReader File2Reader = new FileReader(File2.getPath());

        // Create Buffered Object.
        BufferedReader File1BufRdr = new BufferedReader(File1Reader);
        BufferedReader File2BufRdr = new BufferedReader(File2Reader);

        // Get the file contents into String Variables.
        String File1Content = File1BufRdr.readLine();
        String File2Content = File2BufRdr.readLine();

        //New String Builder
        StringBuilder buffer = new StringBuilder();

        // Compare the Contents of the files.
        String startOfComparision = "---------START----------";
        buffer.append(startOfComparision).append("\n");

        boolean isDifferent = false;
        int lineNumber = 1;

        if (File1Content != null || File2Content != null) {

            // Check whether file1 contains data or not
            while ((File1Content != null)) {

                // Check whether file2 contains data or not
                if (((File2Content) != null)) {

                    // Check whether both the files have same data in the lines
                    if (!File1Content.equals(File2Content)) {
                        buffer.append("Difference in Line " + lineNumber + ":- " + File1.getName() + " contains :" + File1Content + "           " + File2.getName() + " Contains : " + File2Content).append("\n");
                        isDifferent = true;
                    }
                    lineNumber = lineNumber + 1;
                    File2Content = File2BufRdr.readLine();
                } else {
                    buffer.append("Difference in Line " + lineNumber + ":- " + File1.getName() + " contains :" + File1Content + "             " + File2.getName() + " Contains : " + File2Content).append("\n");
                    isDifferent = true;
                    lineNumber = lineNumber + 1;
                }

                File1Content = File1BufRdr.readLine();

            }

            // Check for the condition : if File2 has Data & File1 doesn't contain data.
            while ((File2Content != null) && (File1Content == null)) {
                buffer.append("Difference in Line " + lineNumber + " :- " + File1.getName() + " contains :" + File1Content + "           " + File2.getName() + " Contains : " + File2Content).append("\n");
                isDifferent = true;
                lineNumber = lineNumber + 1;
                File2Content = File2BufRdr.readLine();
            }
        } else {
            // Mention that both the files don't have Data.
            buffer.append(File1.getName() + " and " + File2.getName() + " do not contain any data.");
            isDifferent = true;
        }

        // Check is there any difference or not.
        String endOfComparision = "-----------END----------";
        if (isDifferent) {
            buffer.append(endOfComparision).append("\n");
        } else {
            buffer.append("No Difference Found \nThe Contents Of The Files Are      Identical.").append("\n");
            buffer.append(endOfComparision).append("\n");

问题出在以下这一行:

matched_file_names.add("Path: " + File2.getPath() + "\nFile Name: " + File2.getName());

        }

这是stacktrace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at compareTwoTextualFiles.compareFiles(compareTwoTextualFiles.java:93)
at comparisonForm.btntxtFilesCompareActionPerformed(comparisonForm.java:625)
at comparisonForm.access$400(comparisonForm.java:24)
at comparisonForm$4.actionPerformed(comparisonForm.java:176)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

当选择不同的文件时它工作正常但是当同一个文件作为file1和file2传递时它会给出异常。请纠正它。

3 个答案:

答案 0 :(得分:3)

确保在使用前初始化matched_file_names列表。

这可以解决您的问题。

答案 1 :(得分:0)

什么时候初始化matched_file_names

它为空,因此您将获得NullPoniterException

替换public static List<String> matched_file_names=null;
public static List<String> matched_file_names = new ArrayList<String>();

List接口的任何其他实现

答案 2 :(得分:0)

Matched_file名称尚未发布任何内容,它仍然是null