*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes bug with records.put(currentKey, record) ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "bug with records.put(currentKey, record) ?" Watch "bug with records.put(currentKey, record) ?" New topic
Author

bug with records.put(currentKey, record) ?

Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
Hello Guys,

I have been trying to solve this problem for 3 hours but I don't know why..

private final Map<Integer,Record> records = new HashMap<Integer,Record>();

everytime I records.put(currentKey, record);

...

it will increase the Hashmap size but all the value will be overwritten to the last record..

so end up records will contain 31 keys but all keys point to the last record being "put" anyone knows the reason? I am really desperate..


Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5154
    
  12

So:

records.put(1, record1);
records.put(2, record2);
...
records.put(31, record31);

makes all entries point to record31? That's very hard to believe.

Maybe you should start adding some debug messages to your code, to investigate the cause of this problem. Maybe the createUniqueKey is not returning the expected value? ...

SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

The only way I could see this happening is if you were adding the same instance of the record class to the Map 31 times. So each time you think you are creating a new record, you are actually updating the single instance with the information from the record you have just read from the data file. From your code snippet, it doesn't appear that you are doing this.

I'd suggest using the debugger in your IDE (assuming you are using one!) and see exactly what is happening when the second record gets created - does the information in the first record get changed? Then see what happens when you add the second record to the Map.


SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
Sean Keane wrote:I'd suggest using the debugger in your IDE (assuming you are using one!) and see exactly what is happening when the second record gets created - does the information in the first record get changed?


yes of cos I use the eclipse debugger.

all the information in the records get change.. they only have different IDs
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5154
    
  12

Ixus See wrote:all the information in the records get change.. they only have different IDs

all information in records are changed, but they only have different IDs
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Ixus See wrote:all the information in the records get change.. they only have different IDs


I'm guessing English is not your first language ? But it's worth keeping in mind that it makes it harder for people to understand your problem if you don't describe it in more detail. One line statements make it very hard to understand what your problem is. The more effort you put into your posts, the more likely someone will read it and the more likelihood of a positive response.

From the sounds of it you are saying that when the second object (record) gets created the information in the first object changes. If my understanding is correct, then you are not creating a new object each time you are creating a new record. You are simply reusing the one single object. If that is the case, then I don't think anyone here will be able to help you - because we can't see your code. You need to do the job of a developer , debug through your code and find out where the problem is.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: bug with records.put(currentKey, record) ?
 
Similar Threads
Finding common characters of two strings in O(n)
Locking & DBConnection (Thread) Pooling
Locking confusion -- help
convert "forty five" to "45"
URLyBird: Please Help! Non-deterministick error with database record reading