Java 当我在文件名后写入.txt时,我得到nullpointerexception

Java 当我在文件名后写入.txt时,我得到nullpointerexception,java,eclipse,Java,Eclipse,每当我在文件名后写入.txt时,就会收到nullpointrexception错误。但是当我不写.txt时,程序似乎在运行,但它总是说系统找不到指定的文件。我不知道该怎么办。请引导我。 这是我的密码 public void signin(){ //System.out.println(System.getProperty("user.dir")); File file=new File("C:\\Workplace\\3rdLastLab\\file1.txt"); tr

每当我在文件名后写入.txt时,就会收到nullpointrexception错误。但是当我不写.txt时,程序似乎在运行,但它总是说系统找不到指定的文件。我不知道该怎么办。请引导我。 这是我的密码

public void signin(){
    //System.out.println(System.getProperty("user.dir"));
    File file=new File("C:\\Workplace\\3rdLastLab\\file1.txt");
    try{
        FileReader fr=new FileReader(file);
        BufferedReader br=new BufferedReader(fr);
        Scanner sc=new Scanner(br);
        String text1=field1.getText();
        String text2=field2.getText();
        String text3=text1+"."+text2;


        while(sc.hasNext()){

            String string= sc.next();   
            if(text3.equals(string)==true){
                JOptionPane.showMessageDialog(null, "You are logged in!");
            }
            else
                JOptionPane.showMessageDialog(null, "Wrong user name or password");
        }

        br.close();
    }
    catch (IOException e){
        JOptionPane.showMessageDialog(null, e.getMessage());
    }

}
我得到了这些错误

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Login.signin(Login.java:36)
at Login.actionPerformed(Login.java:122)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

当您输入正确的文件名时,会出现空指针异常

看起来您对
field1
field1
有问题(如果它们对应于第36行)


这可能是空的,这取决于您如何设置这些

我的心理调试能力告诉我,当您调用
getText
时,
field1
field2
为空

String text1=field1.getText();
String text2=field2.getText();
添加
.txt
时,您只会得到
nullpointrexception
异常的原因是因为您的代码在该点之前抛出了
fileNotfound
异常


您需要确保实例化了
field1
field2

我确信%100读取过程没有任何问题,因为
BufferedReader
FileReader
的非缓冲区也被
扫描程序
抛出到NullPointerException。
我确信您的一个文本字段有问题,并且我确信您没有定义其中一个(可能有多个),因为当引用数据类型未初始化时会发生NullPointerException

请阅读有关NullPointerException的内容。

哪一行是Login.java第36行?请尝试
“C:/Workplace/3rdLastLab/file1.txt”
如果您确定了
signin
方法中的哪一行是第36行,您将很好地了解错误所在。问题不在于文件的扩展名,而在于你如何读取文件,我相信不管扩展名是什么,问题都存在。当你不放置扩展名时,你的程序在到达NPE点之前就会中止。顺便说一句:“==true”是完全不需要的。我在这里和你在一起,但直到OP发布更多信息之前,我们无法确定。@PM77-1 OP刚刚告诉我们在他的帖子的评论部分出现异常的地方,但看起来他已经删除了评论。它位于
field2.getText()@user2382867-我希望你现在有你的答案。如果没有-请参阅我的建议,打印出
field1
field2
。好的。字段2是密码字段,这就是我遇到问题的原因
String text1=field1.getText();
String text2=field2.getText();