-1

I am creating a Login Form and Sign up Form using JFrames. The problem is every time when the user presses the Sign up button the user details should be serialized and added to the "users.ser" file. And if the user presses the Login button it has to deserialize the data from that file and check for granting access to the user.

Here is the code I tried and it causes EOFException.

For registering the user into the file.

    void registerUser(User user) {
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/Atom workspace/Java_programs/Form/users.ser"));
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/Atom workspace/Java_programs/Form/users.ser"));
            ArrayList<User> users = new ArrayList<>();
            try {
                users = (ArrayList<User>) ois.readObject();
            } catch (EOFException e) {
                //If the file is empty 
                users.add(new User("User1", "Pass1"));
                users = new ArrayList<User>();            
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {        
                //If the file has some contents add the new user into the file       
                users.add(user);
                oos.writeObject(users);  
                oos.close(); 
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }  

For Checking the user is in the file or not.

    boolean validateUser(User user) {
        ArrayList<User> users = new ArrayList<>();
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/Atom workspace/Java_programs/Form/users.ser"));
            users = (ArrayList<User>) ois.readObject();
            for (User tmpUser : users) {
                if(user.name.equals(tmpUser.name) && user.password.equals(tmpUser.password)) 
                    return true;
            }
        } catch(EOFException e) {
            System.out.println("EOFException occured"); 
            return false;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }  

Error Message when the registerUser(user) is called :

java.io.EOFException
        at java.base/java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2926)     
        at java.base/java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3421)
        at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:959)
        at java.base/java.io.ObjectInputStream.<init>(ObjectInputStream.java:397)
        at Java_programs.Form.LoginForm.registerUser(FormMain.java:119)
        at Java_programs.Form.MyFieldListener.actionPerformed(FormMain.java:73)
        at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
        at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
        at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
        at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
        at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
        at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626)
        at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
        at java.desktop/java.awt.Component.processEvent(Component.java:6391)
        at java.desktop/java.awt.Container.processEvent(Container.java:2266)
        at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
        at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
        at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
        at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
        at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
        at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
        at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
        at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
        at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
        at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)    
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)    
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)    
        at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
        at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

When calling validateUser(User user) :

   EOFxception Occured
   No access

I don't know why its if throwing even though I caught the exception in void registerUser(User user) method.

I am actually a newbie please explain it to me. And Thanks in advance :)

Prasanth
  • 3
  • 3
  • 1
    Does this answer your question? [EOFException when reading files with ObjectInputStream](https://stackoverflow.com/questions/12684072/eofexception-when-reading-files-with-objectinputstream) – DontKnowMuchBut Getting Better Jan 29 '22 at 19:00
  • In other words, it's supposed to do this. – DontKnowMuchBut Getting Better Jan 29 '22 at 19:01
  • First: one of your catch statements only returns false. WRONG: never "ignore" errors like that. Always print the stack trace at least. And then: when reading a file, and hitting EOF is unexpected in your case, thus returning false is just plain wrong. – GhostCat Jan 29 '22 at 19:01
  • @GhostCat: actually, hitting EOF *is* expected when reading from ObjectInputStream. The exception is expected to be thrown when the end of the data file is reached, no? ... unless I'm misunderstanding things – DontKnowMuchBut Getting Better Jan 29 '22 at 19:05
  • But yes, the OP is handling it wrong. – DontKnowMuchBut Getting Better Jan 29 '22 at 19:10
  • @GhostCat I have changed some code in "validateUser(User user)" please check to it. And Thank you. – Prasanth Jan 29 '22 at 19:27
  • Note: this isnt a code review place. It is not needed that you change your code on the input you get here, most of the time, that is not even a good idea. Because it might confuse peole who already read the initial content. And then: I suggest you step back and try to create a real [mcve]. Something that ONLY does: have a main method, and then invokes your methods to store some (hard coded users). And then reads them back. That is how you also debug things yourself: look at each part by itself, and think about tests that you can write to ensure just that small part works. – GhostCat Jan 30 '22 at 07:02
  • Then think about the next part. and so on. – GhostCat Jan 30 '22 at 07:02

1 Answers1

0

You are truncating the input file before you read it in, as you have setup file input and output on consecutive lines:

new FileInputStream("D:/Atom workspace/Java_programs/Form/users.ser"));
new FileOutputStream("D:/Atom workspace/Java_programs/Form/users.ser");

Move your writing code to a separate method - called after closing the input file - and never write to your master file directly. Always output to a temp file first and rename the file afterwards to replace the original.

Fix your exception handling to use try with resources.

DuncG
  • 12,137
  • 2
  • 21
  • 33