Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Norm

Thread: Cloneable Error!

  1. #1
    NatProg is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Cloneable Error!

    Could someone tell me why I keep getting the exception error in Clone() method?
    You are Awesome!

    Java Code:
    public class Person
    {
        private String name;
        private Date born;
        private Date died;//null indicates still alive.
    
        public Person(){
        	name = null;
        	born = new Date();
        	died = new Date();
        }
        public Person(String initialName, Date birthDate, Date deathDate)
        {
            if (consistent(birthDate, deathDate))
            {
                name = initialName;
                born = new Date(birthDate);
                if (deathDate == null)
                    died = null;
                else
                    died = new Date(deathDate);
             }
             else
             {
                 System.out.println("Inconsistent dates. Aborting.");
                 System.exit(0);
             }
        }
    .
    .
    .
        public Object Clone()
        {
        	try
        	{
        		Person copy;
        		copy = (Person)super.clone();
        		copy.name = (String)super.clone();
        		copy.born = (Date)super.clone();
        		copy.died = (Date)super.clone();
        		return copy;
        	}catch(CloneNotSupportedException e){
        		System.err.print("Error on Cloning!");
        		e.printStackTrace();
        		return null;
        	}
        }
    }

    Java Code:
    public class PersonDemo
    {
        public static void main(String[] args)
        {
        	Date date = new Date();
            Person bach = 
                new Person("Johann Sebastian Bach",
                     new Date("March", 21, 1685), new Date("July", 28, 1750));
            Person stravinsky = 
                new Person("Igor Stravinsky",
                     new Date("June", 17, 1882), new Date("April", 6, 1971));
            Person adams = 
                new Person("John Adams",
                     new Date("February", 15, 1947), null);
    
            System.out.println("A Short List of Composers:");
            System.out.println(bach);
            System.out.println(stravinsky);
            System.out.println(adams);
    
            Person bachTwin = (Person)bach.Clone();//cloning
            System.out.println("Comparing bach and bachTwin:");
            if (bachTwin == bach)
                 System.out.println("Same reference for both.");
            else
                 System.out.println("Distinct copies.");
            if (bachTwin.equals(bach))
                 System.out.println("Same data.");
            else
                 System.out.println("Not same data.");
         }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Cloneable Error!

    Please post the full text of the error message.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: Cloneable Error!

    Clone() != clone() (Java is case sensitive). Also read the API on cloning() (your class also needs to implement a marker interface).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    NatProg is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: Cloneable Error!

    Quote Originally Posted by Norm View Post
    Please post the full text of the error message.
    Here it is:

    Error on Cloning!java.lang.CloneNotSupportedException: Person
    at java.lang.Object.clone(Native Method)
    at Person.Clone(Person.java:225)
    at PersonDemo.main(PersonDemo.java:22)
    Exception in thread "main" java.lang.NullPointerException
    at PersonDemo.main(PersonDemo.java:28)

  5. #5
    NatProg is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: Cloneable Error!

    Quote Originally Posted by JosAH View Post
    Clone() != clone() (Java is case sensitive). Also read the API on cloning() (your class also needs to implement a marker interface).

    kind regards,

    Jos
    I changed the
    public Object Clone()
    to
    public Object clone()
    And also in the main method. But I still keep getting the same CloneNotSupportedException !
    Last edited by NatProg; 04-01-2012 at 07:29 PM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Cloneable Error!

    at Person.Clone(Person.java:225)
    Which line is 225?

    Have you read the API doc for the Object class's clone() method?
    It explains when and why the exception is thrown.
    NatProg likes this.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: Cloneable Error!

    Quote Originally Posted by NatProg View Post
    I changed the
    public Object Clone()
    to
    public Object clone()
    And also in the main method. But I still keep getting the same CloneNotSupportedException !
    Read what I wrote between parentheses.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    NatProg is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: Cloneable Error!

    Quote Originally Posted by Norm View Post
    Which line is 225?

    Have you read the API doc for the Object class's clone() method?
    It explains when and why the exception is thrown.
    Thanks you so much Norm!
    I read the doc and I realized I haven't added the implemens Cloneable to Person class.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Cloneable Error!

    When in doubt, read the manual.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Cloneable interface
    By diamonddragon in forum New To Java
    Replies: 1
    Last Post: 02-01-2012, 09:55 AM
  2. Replies: 1
    Last Post: 03-10-2010, 01:53 PM
  3. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  4. Replies: 1
    Last Post: 10-26-2009, 04:38 PM
  5. Replies: 3
    Last Post: 01-23-2009, 08:53 AM

Tags for this Thread

Posting Permissions

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