Results 1 to 4 of 4
  1. #1
    tngo is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Question Deep Copy Constructor

    I need some help in implementing deep copy constructor and not using the clone method. I understand that in order for the deep copy to work, you are making a copy of the object and not just the address pointing to that object. Otherwise if you make changes to one the original object will also observe the change. However, I am having problem with putting it into practice.

    Java Code:
     /**
      * This is the main method.
      * It creates a Team object and then tests the copy constructor.
      */
      public static void main(String[] args)
       	{
    		Team team1 = new Team("The Java Studs", "Bill", "Ted", "Carol", "Alice", 
    					 new Competition("ACM Programming Contest","The Java Studs", "Team University Java", 2008),
    					 new Competition("Antarctic Programming Contest","The Java Studs", "Frigid South", 2010) );
    		Team team2 = new Team(team1);	// Copy constructor
    
    		// Change competition information for team2
    		team2.getCompetition1().setYear(2011);
    		team2.getCompetition1().setWinner("Mmm Java");
    		team2.getCompetition1().setRunnerup("Java By Night Java By Day");
    		team2.getCompetition2().setYear(2012);
    
    		// Output information for both team1 and team2.
    		// Team1's should be unchanged from the original values.
    		team1.OutputTeamInfo();
    		team2.OutputTeamInfo();
    	}
    Java Code:
       	/**
    	* Team copy constructor;  Performs a deep copy from the Competition class
    	* instead of just copying a reference to the same Competition objects.
     	*/
     	public Team(Team otherTeam)
     	{
     		Team copyTeam = new Team();
     		copyTeam.setCompetition1(this.competition1);
     		copyTeam.setCompetition2(this.competition2);
     		copyTeam.setName1(this.name1);
     		copyTeam.setName2(this.name2);
     		copyTeam.setName3(this.name3);
     		copyTeam.setName4(this.name4);
     	
    	}
    This is my output from command line when I try to run it.
    Exception in thread "main" java.lang.NullPointerException
    at Team.main(Team.java:172)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default Re: Deep Copy Constructor

    You have things back-to-front in your copy constructor. You need to copy/set the attributes of the passed-in Team to the current instance ("this")

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    tngo is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: Deep Copy Constructor

    It could be that I need coffee this morning but could you help me understand this with an example? I tried fixing the codes but I feel that I'm only referencing the address because the changes are being observed by both team1 and team2.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Deep Copy Constructor

    It's a constructor, not a clone() method, so there is no copyTeam object.
    You need to do:
    Java Code:
    this.setCompetition1(otherTeam.competition1);
    etc etc
    That will give you a second Team object that references the same data that the original Team object references.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. deep copy in java
    By kmm1977 in forum New To Java
    Replies: 1
    Last Post: 12-09-2011, 06:59 PM
  2. [SOLVED] Deep copy
    By thorne_ in forum New To Java
    Replies: 5
    Last Post: 06-05-2009, 04:51 PM
  3. Deep copy?!
    By deepthought015 in forum New To Java
    Replies: 3
    Last Post: 05-07-2009, 07:31 PM
  4. Deep Copy Test
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 12:05 AM
  5. Replies: 0
    Last Post: 12-28-2007, 01:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •