Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java I';我得到了NullPointerException_Java_File_Nullpointerexception - Fatal编程技术网

Java I';我得到了NullPointerException

Java I';我得到了NullPointerException,java,file,nullpointerexception,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 Obje

这是我的密码:

    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");
这里是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传递时,它给出异常。请更正。

请确保在使用前初始化
匹配的\u文件名
列表


这应该可以解决您的问题。

您何时初始化匹配的文件名

它为空,因此您将获得
NullPoniterException

替换
公共静态列表匹配的\u文件\u名称=null带有

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

列表
接口的任何其他实现

匹配的\u文件
名称尚未启动,它仍然是
空的

尊敬的先生,我已经将其初始化为空。如果我不明白,请解释一下。Plz help。您没有定义任何对象,只是定义了引用,并将null值赋给它。null是一个null指针,指向未初始化内存的指针。空指针有一个保留值,用于指示指针未引用有效对象。@MohsinAli将其初始化为真正的非空对象(例如,初始化为
新ArrayList()
),而不是空。我得到了所有重复的成员。几张否决票和一堂新课。好的,先生,我现在得到你了。谢谢
public static List<String> matched_file_names = new ArrayList<String>();