• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
javaerror.png
[Thumbnail for javaerror.png]
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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~>
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic