my dog learned polymorphism*
The moose likes Beginning Java and the fly likes how to realize the function as a copy constructor in java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to realize the function as a copy constructor in java?" Watch "how to realize the function as a copy constructor in java?" New topic
Author

how to realize the function as a copy constructor in java?

Shawn Ma
Greenhorn

Joined: Feb 18, 2012
Posts: 16
I don't know how to realize a copy constructor in Java. I just want to make a copy of the data fields and I wrote a naive method geoCopy:
-------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------

my Geometry class is:
----------------------------------------


......methods.......
---------------------------------------

then I run with the following in main code:



where


and got tons of exception errors.

Can you help me solve the problem..to realize the simple function of copy a object, ,,,,just data fields is ok....I am a beginner, can you give me a simplist solution? Thanks.


[Thumbnail for javaerror.png]

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

That's this line of code, right?



That could throw the NPE if:

(1) geo is null (we know it isn't because it refers to a Geometry object)

(2) geo.vertices is null

(3) geo.vertices[i] is null

(4) original is null (but it was used in an earlier line so we know it isn't)

(5) original.vertices is null (but it was used earlier so we know it isn't)

(6) original.vertices[i] is null (but it was used earlier so we know it isn't)

In other words, when you create a new Geometry, that doesn't initialize the "vertices" variable so that it refers to an array of the right length. Perhaps the Geometry class has a different way for adding elements to "vertices", or setting them up, or whatever.

By the way I don't understand why your transformAndRender method creates two copies of a Geometry object and does different things to each of them.
Shawn Ma
Greenhorn

Joined: Feb 18, 2012
Posts: 16
Thank you so much!!!

I think I know what's wong now: my Geometry class does not have a constructor, so "vertices""faces" were initialized by default to null?

and you are right, I was suppose to use ...I was randomly testing so messed it up.




Paul Clapham wrote:That's this line of code, right?



That could throw the NPE if:

(1) geo is null (we know it isn't because it refers to a Geometry object)

(2) geo.vertices is null

(3) geo.vertices[i] is null

(4) original is null (but it was used in an earlier line so we know it isn't)

(5) original.vertices is null (but it was used earlier so we know it isn't)

(6) original.vertices[i] is null (but it was used earlier so we know it isn't)

In other words, when you create a new Geometry, that doesn't initialize the "vertices" variable so that it refers to an array of the right length. Perhaps the Geometry class has a different way for adding elements to "vertices", or setting them up, or whatever.

By the way I don't understand why your transformAndRender method creates two copies of a Geometry object and does different things to each of them.
Shawn Ma
Greenhorn

Joined: Feb 18, 2012
Posts: 16
Sorry but I still do not know why my code is not playing the role of initializer???

In the Geometry class, the values are signed by methods like this:



I really do not know what to add to my code?




Paul Clapham wrote:

(2) geo.vertices is null

(3) geo.vertices[i] is null
>
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Okay. So that buildGlobe() method is how you set up the vertices and faces variables in a Geometry object. So therefore your copy constructor should call it (with the correct parameters) to set up those variables in the copy, rather than trying to replicate that logic yourself.
Shawn Ma
Greenhorn

Joined: Feb 18, 2012
Posts: 16
I don't want to use that method because it will not give me correct values from there.

I want to copy values from an existing object, ,,,what do I need to do before assigning values of array elment by element ???

thnks

Paul Clapham wrote:Okay. So that buildGlobe() method is how you set up the vertices and faces variables in a Geometry object. So therefore your copy constructor should call it (with the correct parameters) to set up those variables in the copy, rather than trying to replicate that logic yourself.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Shawn Ma wrote:I don't want to use that method because it will not give me correct values from there.


How can it not give you the correct values? It only takes the two parameters, all of the values are derived from them.

I want to copy values from an existing object, ,,,what do I need to do before assigning values of array elment by element ???


However I suppose you haven't mentioned yet that there are other methods which allow you to fiddle with the contents of those arrays after they are assigned. Fine. Then your copy constructor should (1) create the arrays (2) copy the correct values into them. You already have step (2) programmed. I already told you how to do step (1). Do them in the right order.
Shawn Ma
Greenhorn

Joined: Feb 18, 2012
Posts: 16
Thanks you! how to create an array? Isn't that calling the default constructor " new Geomety()" antomatically create the arrays as instance variables? The default constructor initialize the arrays as nulls. Why cant i assign values to these null arrays? Then how to assign values to these null arrays? Sorry for my stupid questions . I need your help! Thanks
Shawn Ma
Greenhorn

Joined: Feb 18, 2012
Posts: 16
Paul Clapham wrote:
Shawn Ma wrote:I don't want to use that method because it will not give me correct values from there.


How can it not give you the correct values? It only takes the two parameters, all of the values are derived from them.

I want to copy values from an existing object, ,,,what do I need to do before assigning values of array elment by element ???


However I suppose you haven't mentioned yet that there are other methods which allow you to fiddle with the contents of those arrays after they are assigned. Fine. Then your copy constructor should (1) create the arrays (2) copy the correct values into them. You already have step (2) programmed. I already told you how to do step (1). Do them in the right order.


Thank you, I changed my copy method to:

And it now works. Than you so much~>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to realize the function as a copy constructor in java?