aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "NullPointerException" Watch "NullPointerException" New topic
Author

NullPointerException

ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
My code for the client is as follows :
int count = db.getRecordCount();
rowData = new String[count][9];
System.out.println("The Number of records in the database = "+count);
System.out.println("***************************************");
for ( int i = 1; i < count; i++ ) {
try {
DataInfo row = db.getRecord(i);
rowData[i] = row.getValues();
} catch(DatabaseException dbe)
{
System.out.println(dbe.getMessage());
}

}
When I run this code , I get the following results
The Number of records in the database = 38
***************************************
Exception in thread "main" java.lang.NullPointerException
at suncertify.client.FlightInformationSystem.<init>(FlightInformationSystem.java:57)
at suncertify.client.FlightInformationSystem.main(FlightInformationSystem.java:87)
If I hardcode the count to 23 in the for loop, It works fine..
I am trying to display all the flight information in a JTable.
Any solution ???
-- Ravindra


SCJP, SCJD, SCWCD, SCBCD, SCEA
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

for ( int i = 1; i < count; i++ )

and
If I hardcode the count to 23 in the for loop, It works fine..


hmm. My guess would be in the loop. Pardon for my memory beign off, but try things like initializing the int i to 0 or something along those lines, and maybe if it needs to start at 1 have it start at 0 then just add one to i in the

These are just guesses.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
Hi Mark
When I try to invoke
DataInfo row = db.getRecord(0);
I got the following output :

***************************************
Connection to DB obtained
***************************************
The Number of records in the database = 39
***************************************
Record number must be greater than 1


What I am trying to do is to just get the total number of records in db.db database then I use a for loop to retrieve each of these records.
So the i in for loop is correctly initialized to 1 and not 0

-- Ravindra
ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
Hi Mark
I got it working but making a simple check for null before adding it to JTable. I understand now that the records in the database might not be arranged in a serial order for me to just loop count times and retrieve each of the records.
DataInfo row = null;
for ( int i = 1; i < count; i++ ) {
try {
if ( null != ( row = db.getRecord(i) ) ) {
rowData[i] = row.getValues();
}
} catch(DatabaseException dbe)
{
System.out.println(dbe.getMessage());
}
}
But now my question is how to I get all the record numbers in the database ? Data class doesn't provide any such method.
These also means that to get the initial flight information about all the airlines and their flight details , I should be using a RandomAccessFile to read all the records from the flat file.
-- Ravindra
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

If I recall, I query the data length, then loop through that calling getRecord
The following is in my Data class, so the recordCount can be gotten from the getRecordCoutn method of the Data class.

currentRecord is an instance of DataInfo class

These also means that to get the initial flight information about all the airlines and their flight details , I should be using a RandomAccessFile to read all the records from the flat file.

No, all the means necessary is already there.
Mark
Rajesh Matti
Ranch Hand

Joined: Jan 07, 2002
Posts: 121
Unfortunately, the delete method does not decrement the recordcount, but readRecord() returns null if you try to read a 'DELETED' record. That may be why you were getting that exception, but, I still do not understand why it was running when you hardcoded, may be the record count was more than 23, and all the records beyond 23 were deleted. Anyway, check your loop too, make it i=0 ;i<count or i=1;i<=count, but must pass a record number bw 1 and 'n' to getRecord().
-Rajesh
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

I would only say that you do not want to use the delete method. There is nothing in the assignment that will have you use this method. Plus like Rajesh said, it doesn't change the recordCount.
If you have deleted records, restore your db.db with the downloaded file from Sun.
Mark
ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
I have updated the db.db file from the sun download.
but the exception is repeating.
Could you query your db.db and let me know the total number of records ? I just want to ensure that I might not have accidentally added or deleted some record?
String[][] rowData = null;
int recordCount = db.getRecordCount();
rowData = new String[recordCount][9];
System.out.println("The Number of records in the database = "+recordCount);
System.out.println("***************************************");
for ( int recordNumber = 1; recordNumber <= recordCount; recordNumber++ ) {
try {
rowData[recordNumber-1] = ( db.getRecord(recordNumber) ).getValues();
} catch(DatabaseException dbe)
{
System.out.println(dbe.getMessage());
}
}
***************************************
Connection to DB obtained
***************************************
The Number of records in the database = 39
***************************************
Exception in thread "main" java.lang.NullPointerException
at suncertify.client.FlightInformationSystem.<init>(FlightInformationSystem.java:56)
at suncertify.client.FlightInformationSystem.main(FlightInformationSystem.java:86)

Thanks
-- Ravindra
Rajesh Matti
Ranch Hand

Joined: Jan 07, 2002
Posts: 121
Look at the code.
db.getRecord(recordNumber) ).getValues();
the documentation for getRecord() says "returns DataInfo for the record or null if the record has been marked for deletion". So, getRecordCount() gives you count of all the records in the database irrespective of their status(deleted, live etc). But, getRecord() returns the record only if its status is 'NOT DELETED', else it returns null.
int recordCount = db.getRecordCount();
ArrayList records = new ArrayList();
for ( int recordNumber = 1; recordNumber <= recordCount; recordNumber++ ) {
try {
DataInfo record = db.getRecord(recordNumber);
if(record != null) {
records.add(record.getValues());
}
} catch(DatabaseException dbe)
{
System.out.println(dbe.getMessage());
}
String[][] rowData = records.toArray(new String[0]);
}

Hope this helps.
ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
Rajesh
Thanks a lot !
I had incorporated the null test before but I was not clear that why a record would be null? I am clear about it now.
This also means that when in doubt one should go through the documentation given for the methods.
Thanks once again.
-- Ravindra
Rajesh Matti
Ranch Hand

Joined: Jan 07, 2002
Posts: 121
No problem, you are most welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException