aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Problem reading database. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Problem reading database." Watch "Problem reading database." New topic
Author

Problem reading database.

Tom S Lee
Greenhorn

Joined: Apr 01, 2012
Posts: 5
Hi all,

This is my first post with questions.

I basically tried to use database reader files that Roberto, Roel and someone else here shared in this forum
and I have been tweaking things around to fit my requirement but I am not having much of luck progressing further with the implementation.

After running the application in debug mode, I found that I am getting false value of record length to start with
and it affects my find() method run in unexpected sequence and therefore I get no data record display in the table.

Can anyone please spot me where I am doing it wrong?
I am not an experienced developer and I have no friend around who has programming knowledge.

I have read through my assignment requirements many times and referred to faq page here but cannot find myself a solution.
Your tip of advice will be very much appreciated!




This is the data file format that is given to me.


******************************************************************************

Data file Format
The format of data in the database file is as follows:

Start of file
4 byte numeric, magic cookie value. Identifies this as a data file
4 byte numeric, offset to start of record zero
2 byte numeric, number of fields in each record

Schema description section.
Repeated for each field in a record:
2 byte numeric, length in bytes of field name
n bytes (defined by previous entry), field name
2 byte numeric, field length in bytes
end of repeating block

Data section. (offset into file equal to "offset to start of record zero" value)
Repeat to end of file:
2 byte flag. 00 implies valid record, 0x8000 implies deleted record
Record containing fields in order specified in schema section,
no separators between fields, each field fixed length at maximum specified in schema information

End of file

All numeric values are stored in the header information use the formats of the
DataInputStream and DataOutputStream classes.
All text values, and all fields (which are text only), contain only 8 bit characters,
null terminated if less than the maximum length for the field.
The character encoding is 8 bit US ASCII.


******************************************************************************



This is my data constructor

[edit] do not post complete snippets of actual code
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1507
    
    5

Hello Tom S Lee,

Welcome to CodeRanch!

I haven't gone through the complete code, but here is what I think:

1) Data file format is : 4 bytes cookie, followed by 4 bytes offset value, followed by 2 bytes number of fields
2) The first thing you are doing is - read 4 bytes and treat it as offset
3) You should've treated it as cookie, then again read 4 bytes which would be offset

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Tom S Lee
Greenhorn

Joined: Apr 01, 2012
Posts: 5
Hi Anayonkar,

Thanks for the warm welcome and also for the reply. It certainly seems that I was missing something important.
I made some changes according to your point but it is still not coming along.
Now I am getting runtime error.

Here is the code.

[edit] do not post complete snippets of actual code
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1507
    
    5

Tom S Lee wrote:Now I am getting runtime error.

Do you mean to say runtime exception? Please TellTheDetails. Can you please provide the complete exception stack?

Since data file differ from candidate to candidate (and I don't even have data file with me right now), it is difficult to take your code and debug on my machine, so it'll be helpful if you provide more details about what is happening.

I hope this helps.
Tom S Lee
Greenhorn

Joined: Apr 01, 2012
Posts: 5
Thank you for being very supportive.
Below is what I get in the output when I press the search button to display all the data records in the table.
I won't get these exceptions If I hardcode record length and assign some value to it but that's not what record length is for.
Any idea how to get correct record length from my implementation and display appropriate data record without getting these exceptions?

Thank you again.


java.lang.ArrayIndexOutOfBoundsException: 0
at com.suncertify.db.Data.find(Data.java:454)
at com.suncertify.remote.ServerImpl.findAndReturnData(ServerImpl.java:136)
at com.suncertify.remote.ServerImpl.getAllContractors(ServerImpl.java:65)
at com.suncertify.client.ClientController.refreshView(ClientController.java:366)
at com.suncertify.client.ClientController.doSearchContractors(ClientController.java:340)
at com.suncertify.client.ClientController.actionPerformed(ClientController.java:201)
at com.suncertify.client.gui.BasePanel.postUserActionEvent(BasePanel.java:59)
at com.suncertify.client.gui.ControlPanel.actionPerformed(ControlPanel.java:132)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
at java.awt.Component.processMouseEvent(Component.java:6504)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6269)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
5? 07, 2012 10:52:56 ?? com.suncertify.remote.ServerImpl findAndReturnData
??: Exception in findAndReturnData()
java.lang.NullPointerException
at com.suncertify.remote.ServerImpl.findAndReturnData(ServerImpl.java:137)
at com.suncertify.remote.ServerImpl.getAllContractors(ServerImpl.java:65)
at com.suncertify.client.ClientController.refreshView(ClientController.java:366)
at com.suncertify.client.ClientController.doSearchContractors(ClientController.java:340)
at com.suncertify.client.ClientController.actionPerformed(ClientController.java:201)
at com.suncertify.client.gui.BasePanel.postUserActionEvent(BasePanel.java:59)
at com.suncertify.client.gui.ControlPanel.actionPerformed(ControlPanel.java:132)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
at java.awt.Component.processMouseEvent(Component.java:6504)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6269)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5282
    
  13

First of all, you are not allowed to post complete snippets of actual code (that's why those snippets were deleted).

Secondly, you are taking the developer certification. So you should be able to debug the code by yourself instead to just posting your code and database schema and expecting someone else to do your (development) job. Please ShowSomeEffort and TellTheDetails (which runtime exception)!


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5282
    
  13

This is a GUI exception (caused by a NPE in your service implementation, which is probably caused by a (faulty) DB implementation. Maybe you should first try to have a working implementation of your Data class (using some test cases) before trying to show your records into the JTable.
Tom S Lee
Greenhorn

Joined: Apr 01, 2012
Posts: 5
Hi Roel,

I apologize for violating the forum rule. I was not fully aware of that.
I will be careful not to do it again.

I am actually doing this assignment for a school project and this is just too much for a student with no experience.
My school even regrets that they included this assignment in the curriculum and support I get from school is very limited as to the course structure.
I didnot intend to just paste my snippet of codes here expecting others to sort the problem for me.
It is more likely that I do not have solid understanding to build one complete application and lacking in the knowledge to debug a program properly.

I hope you understand.
Tom S Lee
Greenhorn

Joined: Apr 01, 2012
Posts: 5
Hi again Roel,

Yes, it looks like it comes from faulty db implementation.
I actually tried your codes that you shared in the post titled "working with database"
and I had luck loading data records in the command prompt but could not get it to load it into the table.
It is my lack of knowledge not being able to tweak around and fit in my own requirement.

I am struggling to implement codes that gets correct record length and an offset.
Is there any online resource for a greenhorn like me to take a look at?

Thank you.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1507
    
    5

Tom S Lee wrote:and I had luck loading data records in the command prompt but could not get it to load it into the table.

Again, as Roel has already mentioned, its seems that issue is in GUI
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5282
    
  13

If I understand correctly when you print out the records they appear in readible form on the console (command prompt), so you are able to process your database file correctly. But when trying to show them in the JTable you get the stack trace from some posts above. And you are not able to interpret this stack trace. I can easily tell you that there is a NullPointerException on line 137 of your ServerImpl class. So you are probably invoking a method on a data member or variable which is uninitialized.

I would suggest first finishing your Data class completely using some test cases (JUnit, TestNG,...) and only continue with your Server implementation when your Data class is completely finished and you know it's working as expected. And when you later make a change to the Data class these test cases will tell you if you have broken existing functionality. Then the same story applies to your Server implementation: finish that one before starting with the GUI. That's how I developed my assignment and so you don't lose valuable time deciding if it's a GUI, Server or Data error.

For Data class the only resource I can think of is documentation of RandomAccessFile (because that's the class which I used to access the data file).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem reading database.