• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

RandomAccessFiles

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

It is my first time here and I was wondering if someone could take a look at my code and help me to solve this problem. I am trying to create a Random Access File with Read and Write functions, but when I'm trying to add an account to .dat file it gives me an error instead of adding in to the file. It is quite difficult to find any help online that uses .dat file instead of .txt and that implements GUI. Your help is greatly appreciated and thank you in advance. I have also attached the rest of the files.


***************************Write file**************************************************************



**************************************Read File**********************************************************************************************


************************************************Create File**********************************************************************

*****************************************************Record File*****************************************

 
Marshal
Posts: 68069
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think I shall move you to out IO forum, where you might ge more attention. Have you been through the Java™ Tutorials? That section contains a page about random access files. Is an XYZ.dat file a random access file? I don't know anything about XYZ.dat files.
You will find your problem easier to sort out if you separate it from the GUI. Please also give us full details about the error before we can help.
 
Laurynas Sadeckas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I haven't yet, but I will do. I don't know if you can see but I have attached a picture and in the picture you can see the window with a bunch of lines, and that is the problem because instead of giving the message "your account has been added" it prints these lines.


Glad to be here.
 
Campbell Ritchie
Marshal
Posts: 68069
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We don't like such pictures because text is easier to handle and read. What's more, the real problem is off the top of the screen in your picture. Please copy the text; this link may help.
 
Laurynas Sadeckas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:We don't like such pictures because text is easier to handle and read. What's more, the real problem is off the top of the screen in your picture. Please copy the text; this link may help.



Ritchie,

Sorry for this and please see below, that's the problem. Also thank you for the link it was very helpful.

Thank you again.

In first try block
In second try block
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
       at WriteRandomFile.addRecord(WriteRandomFile.java:91)
       at WriteRandomFile.actionPerformed(WriteRandomFile.java:135)
       at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
       at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
       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:6535)
       at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
       at java.awt.Component.processEvent(Component.java:6300)
       at java.awt.Container.processEvent(Container.java:2236)
       at java.awt.Component.dispatchEventImpl(Component.java:4891)
       at java.awt.Container.dispatchEventImpl(Container.java:2294)
       at java.awt.Component.dispatchEvent(Component.java:4713)
       at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
       at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
       at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
       at java.awt.Container.dispatchEventImpl(Container.java:2280)
       at java.awt.Window.dispatchEventImpl(Window.java:2750)
       at java.awt.Component.dispatchEvent(Component.java:4713)
       at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
       at java.awt.EventQueue.access$500(EventQueue.java:97)
       at java.awt.EventQueue$3.run(EventQueue.java:709)
       at java.awt.EventQueue$3.run(EventQueue.java:703)
       at java.security.AccessController.doPrivileged(Native Method)
       at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
       at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
       at java.awt.EventQueue$4.run(EventQueue.java:731)
       at java.awt.EventQueue$4.run(EventQueue.java:729)
       at java.security.AccessController.doPrivileged(Native Method)
       at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
       at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
       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)
 
Campbell Ritchie
Marshal
Posts: 68069
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately that stack trace is incomplete. We do however know that something is null which oughn't to be. We don't know where the exception was thrown. Please check whether there is any more stack trace, showing a line number in your code; that would make it much simpler to correct the problem.
I also suspect it might have nothing to do with your writing to the file, but the problem might be elsewhere. How much code did you write since the last time you ran your application? If the previous run worked all right, it is likely that the error is in the new code.
 
Laurynas Sadeckas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Unfortunately that stack trace is incomplete. We do however know that something is null which oughn't to be. We don't know where the exception was thrown. Please check whether there is any more stack trace, showing a line number in your code; that would make it much simpler to correct the problem.
I also suspect it might have nothing to do with your writing to the file, but the problem might be elsewhere. How much code did you write since the last time you ran your application? If the previous run worked all right, it is likely that the error is in the new code.



And when compiling it gives me no errors at all.

In first try block
In second try block
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
       at WriteRandomFile.addRecord(WriteRandomFile.java:91)
       at WriteRandomFile.actionPerformed(WriteRandomFile.java:135)
       at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
       at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
       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:6535)
       at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
       at java.awt.Component.processEvent(Component.java:6300)
       at java.awt.Container.processEvent(Container.java:2236)
       at java.awt.Component.dispatchEventImpl(Component.java:4891)
       at java.awt.Container.dispatchEventImpl(Container.java:2294)
       at java.awt.Component.dispatchEvent(Component.java:4713)
       at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
       at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
       at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
       at java.awt.Container.dispatchEventImpl(Container.java:2280)
       at java.awt.Window.dispatchEventImpl(Window.java:2750)
       at java.awt.Component.dispatchEvent(Component.java:4713)
       at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
       at java.awt.EventQueue.access$500(EventQueue.java:97)
       at java.awt.EventQueue$3.run(EventQueue.java:709)
       at java.awt.EventQueue$3.run(EventQueue.java:703)
       at java.security.AccessController.doPrivileged(Native Method)
       at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
       at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
       at java.awt.EventQueue$4.run(EventQueue.java:731)
       at java.awt.EventQueue$4.run(EventQueue.java:729)
       at java.security.AccessController.doPrivileged(Native Method)
       at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
       at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
       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)
 
Laurynas Sadeckas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Unfortunately that stack trace is incomplete. We do however know that something is null which oughn't to be. We don't know where the exception was thrown. Please check whether there is any more stack trace, showing a line number in your code; that would make it much simpler to correct the problem.
I also suspect it might have nothing to do with your writing to the file, but the problem might be elsewhere. How much code did you write since the last time you ran your application? If the previous run worked all right, it is likely that the error is in the new code.



Dear Ritchie,

I have fixed the problem.  I had the following:

1) I forgot to add .dat ending in my "Read" program.
2) I added to my Write file so the code is as follows:



Thank you very much for your time!
 
Campbell Ritchie
Marshal
Posts: 68069
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done

I presume your exception has gone away now.
 
Laurynas Sadeckas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Well done

I presume your exception has gone away now.



Thanks, I am very happy.  😀

Well I have tested the whole program, and surprisingly everything is working fine.!

 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!