aspose file tools*
The moose likes I/O and Streams and the fly likes little problem in 2 classes' serializaiton&deserialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "little problem in 2 classes Watch "little problem in 2 classes New topic
Author

little problem in 2 classes' serializaiton&deserialization

sarrah gerard
Greenhorn

Joined: Nov 06, 2009
Posts: 18
Hi everybody,
I am confused again, why it gives that java.lang.ClassCastException ? I have default constructors for Profile class and School class. What can be the problem? i think my brain become juicy!
Exception in thread "main" java.lang.ClassCastException: Profile cannot be cast to MyClassToBePersisted
at DeserializeMyClass.main(DeserializeMyClass.java:30)

now it is
Exception in thread "main" java.lang.NullPointerException
at DeserializeMyClass.main(DeserializeMyClass.java:43)
Java Result: 1

System.out.println("School: " + mySchool.toString());

What is wrong with this statement?
Java Result: 1






sarrah gerard
Greenhorn

Joined: Nov 06, 2009
Posts: 18

now it is
Exception in thread "main" java.lang.NullPointerException
at DeserializeMyClass.main(DeserializeMyClass.java:43)
Java Result: 1


System.out.println("School: " + mySchool.toString());

What is wrong with this statement?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
For starters, the Profile.getProfile and School.getSchool methods do nothing. And the MyClassToBePersisted.setProfile and MyClassToBePersisted.setSchool methods do nothing either, and they're not even called by the SerializeMyClass class (which I think should happen, if I guess correctly what the code should do). So the answer to the question of why there's a NullPointerException is: Because "mySchool" is null, and the reason it's null is because the object that gets persisted never has a School (or a Profile) object set.


Ping & DNS - my free Android networking tools app
sarrah gerard
Greenhorn

Joined: Nov 06, 2009
Posts: 18
Ulf Dittmer wrote:For starters, the Profile.getProfile and School.getSchool methods do nothing. And the MyClassToBePersisted.setProfile and MyClassToBePersisted.setSchool methods do nothing either, and they're not even called by the SerializeMyClass class (which I think should happen, if I guess correctly what the code should do). So the answer to the question of why there's a NullPointerException is: Because "mySchool" is null, and the reason it's null is because the object that gets persisted never has a School (or a Profile) object set.

Thank you Ulf.
But ,I understand like this;

I add to Profile class


I add to School class


Then,I erased the set methods in MyClassToBePersisted.java

Now, when ı run , the output is:
<run:
null
null


ı had these
in SerializeMyClass.
Why it gives null null?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
You need the setter methods that you deleted. Think about this: What happens to the objects that are returned by School.getSchool and Profile.getProfile? Do they end up where you expect them to end up?
sarrah gerard
Greenhorn

Joined: Nov 06, 2009
Posts: 18
Ulf Dittmer wrote:You need the setter methods that you deleted. Think about this: What happens to the objects that are returned by School.getSchool and Profile.getProfile? Do they end up where you expect them to end up?

ı add the set methods, but the conclusion doesn't change. I just want to see the values of School.getSchool("...",..); and Profile.getProfile("mary", 11, "paint") when ı run DeserializeMyClass.java
but , it gives null null
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
How are you using the setter methods? If the school and profile fields of the MyClassToBePersisted object are null before you even serialize (and I suspect they are), then something is still missing.
sarrah gerard
Greenhorn

Joined: Nov 06, 2009
Posts: 18
Ulf Dittmer wrote:How are you using the setter methods? If the school and profile fields of the MyClassToBePersisted object are null before you even serialize (and I suspect they are), then something is still missing.


When i serialize, output is:
run:
Current is saved into MyClassToBePersist.ser
BUILD SUCCESSFUL (total time: 0 seconds)

When i deserialize, output is:
null
null

you defines well but i'm veery beginner.and not understand your opinions clearly sorry
What can i do now?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
You only showed how you defined the setter methods, not how you're using them; in particular, which arguments are you passing to them?

And again:
If the school and profile fields of the MyClassToBePersisted object are null before you even serialize (and I suspect they are), then something is still missing.
sarrah gerard
Greenhorn

Joined: Nov 06, 2009
Posts: 18
Ulf Dittmer wrote:You only showed how you defined the setter methods, not how you're using them; in particular, which arguments are you passing to them?

And again:
If the school and profile fields of the MyClassToBePersisted object are null before you even serialize (and I suspect they are), then something is still missing.

in SerializeMyClass.java




Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62


This is the key section. You're not using the objects returned by the getSchool/getProfile methods - you're throwing them away. You need to pass those to the MyClassToBePersist.setSchool and setProfile method calls.

By the way, you should get in the habit of giving your variables names that start with lowercase letters (like "myClassToBePersist") - it's what every Java programmer in this world expects. You'll confuse people if you use names that start with uppercase letters - those are used for classes.
sarrah gerard
Greenhorn

Joined: Nov 06, 2009
Posts: 18
Ulf Dittmer wrote:

This is the key section. You're not using the objects returned by the getSchool/getProfile methods - you're throwing them away. You need to pass those to the MyClassToBePersist.setSchool and setProfile method calls.

By the way, you should get in the habit of giving your variables names that start with lowercase letters (like "myClassToBePersist") - it's what every Java programmer in this world expects. You'll confuse people if you use names that start with uppercase letters - those are used for classes.
Do you mean like that?
MyClassToBePersisted MyClassToBePersist=new MyClassToBePersisted();

MyClassToBePersist.getProfile().setName("mary");
MyClassToBePersist.getProfile().setAge(11);
MyClassToBePersist.getProfile().setHobby("paint");


MyClassToBePersist.getSchool().setnameOfSchool("harvard");
MyClassToBePersist.getSchool().setYearStarted(2000);
this gives same NullPointerException , my fault
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
No, that's not what I meant. In your very first post you had these methods in your MyClassToBePersisted class:

You need to call those methods on your myClassToBePersist object, and you need to pass along the objects you get from the getSchool/getProfile methods. And, obviously, those setter methods need to do something - they need to store the objects you pass into them.
sarrah gerard
Greenhorn

Joined: Nov 06, 2009
Posts: 18
Ulf Dittmer wrote:No, that's not what I meant. In your very first post you had these methods in your MyClassToBePersisted class:

You need to call those methods on your myClassToBePersist object, and you need to pass along the objects you get from the getSchool/getProfile methods. And, obviously, those setter methods need to do something - they need to store the objects you pass into them.


Yes, i have set methods in MyClassToBePersisted class
And i changed sth. like that :
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
You're getting closer, but you're still not calling the setSchool and setProfile methods of the MyClassToBePersisted class. You're calling the setProfile/setSchool methods of the Profile and School classes. Remember that the setSchool/setProfile methods of the MyClassToBePersisted class take a School/Profile object as parameter, not the individual values. Your code should look like this:

sarrah gerard
Greenhorn

Joined: Nov 06, 2009
Posts: 18
Ulf Dittmer wrote:You're getting closer, but you're still not calling the setSchool and setProfile methods of the MyClassToBePersisted class. You're calling the setProfile/setSchool methods of the Profile and School classes. Remember that the setSchool/setProfile methods of the MyClassToBePersisted class take a School/Profile object as parameter, not the individual values. Your code should look like this:


is it in SerializeMyClass class? If so, it gives error. Did you mean profile or school with "single parameters" ?
sarrah gerard
Greenhorn

Joined: Nov 06, 2009
Posts: 18
Ok i got it, now it runs
Your explanation help me, thanks Ulf,have a nice day
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: little problem in 2 classes' serializaiton&deserialization