Results 1 to 9 of 9
Thread: Cloneable Error!
- 04-01-2012, 10:44 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
Cloneable Error!
Could someone tell me why I keep getting the exception error in Clone() method?
You are Awesome!.gif)
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."); } }
- 04-01-2012, 01:55 PM #2
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.
- 04-01-2012, 04:26 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,397
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-01-2012, 07:21 PM #4
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
Re: Cloneable Error!
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)
- 04-01-2012, 07:23 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
- 04-01-2012, 07:27 PM #6
Re: Cloneable Error!
Which line is 225?at Person.Clone(Person.java:225)
Have you read the API doc for the Object class's clone() method?
It explains when and why the exception is thrown.If you don't understand my response, don't ignore it, ask a question.
- 04-01-2012, 07:42 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,397
- Blog Entries
- 7
- Rep Power
- 17
- 04-01-2012, 07:50 PM #8
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
- 04-01-2012, 07:56 PM #9
Similar Threads
-
Cloneable interface
By diamonddragon in forum New To JavaReplies: 1Last Post: 02-01-2012, 09:55 AM -
> Operator cannot be applied error and return incompatible types error
By corney_16 in forum New To JavaReplies: 1Last Post: 03-10-2010, 01:53 PM -
Thread: Error 500--Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12-05-2009, 07:28 AM -
java.lang.Error: Error opening DSound for capture
By NARs in forum NetworkingReplies: 1Last Post: 10-26-2009, 04:38 PM -
Diference Between compiler error Garbage collection and Runtime Error?
By makpandian in forum New To JavaReplies: 3Last Post: 01-23-2009, 08:53 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks