-2

Whenever I write .txt after a file name I recieve nullpointrexception error. But when I don't write .txt the program seems to run but it always says that the system cannot find the specified file. I have no idea what to do. Please guide me. This is my code

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());
    }

}

And i get these errors

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)
Faran Yahya
  • 288
  • 4
  • 17
  • 7
    which line is Login.java line 36? – Uncle Iroh May 14 '13 at 18:31
  • Try `"C:/Workplace/3rdLastLab/file1.txt"` – MalaKa May 14 '13 at 18:31
  • If you identify which line in your `signin` method is line 36, you'll have a good idea what your error is. The problem doesn't come from the file extension, but from how you read your file, most probably. – Laf May 14 '13 at 18:32
  • 2
    I believe that the problem is there **regardless** of the extension. When you do not put extension, your program simply aborts **before** it reaches the NPE point. – PM 77-1 May 14 '13 at 18:35
  • Btw.: that '==true' is completely unneeded. – machinery May 14 '13 at 18:39
  • Please put `System.out.println(field1);` and `System.out.println(field2);` in your code right before you use them. – PM 77-1 May 14 '13 at 18:43
  • it does not aborts, it shows the message that is in the catch. – Faran Yahya May 14 '13 at 18:45
  • @user2382867 - OK. If you want to play semantics, *it aborts its normal execution*. Anyway, what about `field1` and `field2`? – PM 77-1 May 14 '13 at 18:48

3 Answers3

3

my psychic debugging powers tell me that either field1 or field2 is null when you call getText on them.

String text1=field1.getText();
String text2=field2.getText();

The reason you're only getting the nullpointrexception exception when you add .txt is because you're code throws a fileNotfound exception before that point.

You need to make sure that field1 and field2 are instantiated.

2

Looking at behavior when you put the correct file name you get a null pointer exception

It looks like you have issue with field1 or field1 ( if they corresponds to line 36)

    String text1=field1.getText();
    String text2=field2.getText();

which may be null depending on how are you setting these

NullPointerException
  • 3,419
  • 5
  • 21
  • 53
0

I am sure %100 that the reading process don't have any problem, because non of BufferedReader and FileReader also Scanner thrown to NullPointerException.
I am sure you have problem with one of your text fields and I am sure that you didn't define one of them (maybe there are more than one), because NullPointerException occurs while a reference data type without initializing.
Please read this post about NullPointerException .

Community
  • 1
  • 1
Azad
  • 4,959
  • 18
  • 38