Results 1 to 9 of 9
  1. #1
    nikkka is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default Can't create clone

    What's the problem? :S

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at Person.main(Person.java:6)
    Java Code:
    import java.util.*;
    public class Person {
    	public static void main(String[] args) {
    		Employee Stive = new Employee("Stive Jobs", 42, 90000);
    		Employee Jobs = (Employee)Stive.clone();
            System.out.println(Jobs.getDescription());
    	}
    }
    class Employee extends Human {
    	public Employee(String n, int a, int s) {
    		super(n);
    		salary=s;
    		age=a;
    	}
    	public Object clone() {
    		try {
    		    return super.clone();
    		}
    		catch(Exception e) {
    			return null;
    		}
    	}
    	public String getDescription() {
    		return "an employee with a salary of $"+salary;
    	}
        //no need to write getName() function - its already defined in superclass
    	public int getAge() {
    		return age;
    	}
    	public int getSalary() {
    		return salary;
    	}
    	public void setAge(int a) {
    		age=a;
    	}
    	public void setSalary(int s) {
    		salary=s;
    	}
    	private int salary;
    	private int age;
    }
    class Student extends Human {
    	public Student(String n, String m) {
    		super(n);
    		major=m;
    	}
    	public String getDescription() {
    		return "a student majoring in "+major;
    	}
    	private String major;
    }
    class Human {
    	public Human(String n) {
    		name=n;
    	}
        public String getName() {
    		return name;
    	}
    	private String name;
    }

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

    Default

    Quote Originally Posted by nikkka View Post
    What's the problem? :S
    Have you read the API documentation for the clone() method in the Object class? Also, don't just return null when an exception is thrown in your clone() method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    nikkka is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    So, what? I still can't get it... :S Where is the mistake??

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

    Default

    Quote Originally Posted by nikkka View Post
    So, what? I still can't get it... :S Where is the mistake??
    The mistake is in your code; first read the documentation I mentioned.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    nikkka is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    I read it... But if clone() method type is Object, how can I assign x.clone() to another class object?... So, you mean it's impossible? :S

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Look into implementing the Cloneable interface and overriding the clone method with a public version. This tutorial might help you: Object class

  8. #8
    nikkka is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    thnx everybody, i managed to to it!!! i wrote implements Cloneable in declaration of Employee class and it worked! thank Jos and Fubarable!!

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

    Default

    Quote Originally Posted by nikkka View Post
    thnx everybody, i managed to to it!!! i wrote implements Cloneable in declaration of Employee class and it worked! thank Jos and Fubarable!!
    Yep, that's the way to do it; and you can cast the return value of the clone() method to the class type where your clone() overriding method is implemented. As you already noticed: it is all in the documentation.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. clone().?
    By Dayanand in forum New To Java
    Replies: 3
    Last Post: 02-11-2011, 02:26 PM
  2. How to clone a JTable?
    By IamKira in forum New To Java
    Replies: 3
    Last Post: 07-09-2009, 05:28 PM
  3. How to clone an Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:46 PM
  4. clone method
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 09:47 AM
  5. clone method
    By gapper in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 08:46 AM

Posting Permissions

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