Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Load Searialzation to a Array  RSS feed

 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All i can Write and Read from a Serial File, But just Wondering

How can i go about Writing that Serialization Text File to an ArrayList


 
Tony Docherty
Bartender
Posts: 3200
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i go about Writing that Serialization Text File to an ArrayList

I'm not sure what you mean.
ObjectOutputStream will write out whatever object you pass to it (assuming it is Serializable) and ObjectInputStream will recreate the object that was written out, so if you write out an ArrayList you will read in an ArrayList.
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to create a new arraylist

And populate that array list with the serial file,
 
Tony Docherty
Bartender
Posts: 3200
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But ObjectInputStream does create a new ArrayList. If you want another new ArrayList pass the one that has been created by ObjectInputStream to an ArrayList constructor ie:
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I'll give this a go..

And see if I get

getID()
getfirstname() working with the serial file
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im having a Problem with this Code..

What i am Trying to do is DeSerialization which in Turn OverRide the Current Array List Person,

So that way i can then Insert the ArrayList to the SQL Database. but for i cant seem to be able to overRide the List<person> that its still inputting the Current Array to the Database.

 
Tony Docherty
Bartender
Posts: 3200
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What i am Trying to do is DeSerialization which in Turn OverRide the Current Array List Person,

I'm not at all sure what you mean by "override the current ArrayList". Do you mean you want the instance variable 'result' to contain the deserialized ArrayList? If so then don't declare a local result variable in your OpenPatientFileactionPerformed method (which BTW should be called openPatientFileactionPerformed - method and variable names should start with lower case letters. If this is not what you mean can you explain what you want to achieve in plain English rather than how you are trying to achieve.
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Plain English the Best i can Explain..

Save the Array of People to a Serial File, Once a User Clicks on Load Serial File, It should Open the Serial File and Populated the List<Person>

In Turn Anything that was in the List<Person> should now be Removed and repopulated with the information that was inside the Serial Text File.

From their Using this Method. I should now be able to Reload the Database. with the Strings inside the ArrayList Person.

 
Tony Docherty
Bartender
Posts: 3200
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I assume the instance variable which references the list is called 'result' as you are iterating over an instance variable called result in the BackUpDatabaseactionPerformed method. If that is the case then your problem (as I alluded to in my previous post is that when you read in the serialized file you are assigning it to a local variable called result and not the instance variable called result. They may have the same name but they are different variables, the local one is masking the instance one whilst it is in scope and then when it drops out of scope it becomes available for garbage collection. The solution is simply not to declare a local variable called result but just to assign the deserialized object to the instance variable called result ie: instead of doing:
do:
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I think i am after finding my Issue...

I think its with the CurrentEntry That its still Taken the values inside the Text Fields and putting them back into the Array.




 
Tony Docherty
Bartender
Posts: 3200
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think its with the CurrentEntry That its still Taken the values inside the Text Fields and putting them back into the Array.

You don't have an Array that I can see, you have a couple of List variables which are probably referencing objects of type ArrayList. BTW having two variables called 'result' and 'results' is asking for trouble and does not make it easy to read your code.

From disjointed snippets of code it's hard to give any meaningful answer. I suggest you add some print statements to your code and print out the value of key variables so you can see the flow of execution and what values those variables have.
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
variables called 'result' and 'results' is asking for trouble and does not make it easy to read your code.



i have to List one is list<Person> results and the other list<History> Result... which are pointing the the Person.java and History.java in the Model Package

with the

I tried Casting the Person, but it was telling me that i could not cast the Person Object.
code=java]public static int insertPatient(ActionEvent evt ){

Person r;
int resultSet = 0;

int sID = r.getsID();
String sFirstName = ((Person)r).getFirstName();
String sLastName = ((Person)r).getLastName();
String sAddress = ((Person)r).getAddress();
String sPhone = ((Person)r).getPhoneNumber();


String sql = "INSERT INTO Patient VALUES('"+sID+"','"+sFirstName+"','"+sLastName+"','"+ sAddress+"','"+sPhone+"')";

System.out.println(sql); [/code]

My Problem i feel is with the DBSQLQueries.insertPatient(evt, currentEntry); the CurrentEntry is taken the Information that is currently being displayed inside the TextFields and Sending that Information back to the Database. I added some System.out Statements inside
and this has confirmed the same thing. I tried doing DBSQLQueries.insertPatient(evt); but would not work as it will not allow me to Cast the Person Object.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!