2

while trying to clone an array of objects, i am getting an error when the array is not completely full. if the list is full, everything works just fine!

this is my code:

    public Object clone() throws CloneNotSupportedException  {
        EmployeeList listBackup = new EmployeeList();
        for (Employee employeeObj : listEmployee){
             listBackup.add( (Employee) employeeObj.clone() );  
        }
        return listBackup;
    }

is there any other way to rewrite this code, when the element in the array is null?

2
  • possible duplicate of What is a Null Pointer Exception? Commented Feb 16, 2013 at 2:36
  • 1
    It is NOT an "array" of objects. It is a "list" of objects. If the was really a Java array, you could just use the built-in array clone() method and it would cope with null just fine. Commented Feb 16, 2013 at 3:06

2 Answers 2

1

Do a null check.

 for (Employee employeeObj : listEmployee){
   if (employeeObj != null)
     listBackup.add( (Employee) employeeObj.clone());  
 }

Or if you want to break the loop as soon as a null value is found:

for (Employee employeeObj : listEmployee){
  if (employeeObj == null)
    break;
  listBackup.add( (Employee) employeeObj.clone());  
}

Or simply return as soon as you come across a null reference

for (Employee employeeObj : listEmployee){
      if (employeeObj == null)
        return listBackup;
      listBackup.add( (Employee) employeeObj.clone());  
    }

Of course, if you can, avoid having your array/list contain null values to begin with, and as @StephenC pointed out, if this is an actual array, you can clone it. However, this depends on the assignment specifications, other code implementation and what your professor allows you to do.

Sign up to request clarification or add additional context in comments.

5 Comments

is there any way to put this condition inside the loop? so when its null just breaks the loop and return the list? this works just perfectly find.. but i suppose if the list is too long the loop is not finish until the last element of the array no?
@PatriciaQuintanilla simply break; once you come across a null reference. Edited the answer.
i wish i could, this is for a homework, and i have absolute forbidden the use of break in side a loop. my professor rule is never use it if is not in a switch statement!
So he allows you to use an enhanced for loop, to use clone(), but not break inside a for? There are more "creative" ways to break without using break. I edited again to return listBackup outright.
No problem, and yes, just return. Of course, if your professor would only allow you to break, you wouldn't have two of the same return statements. To each their own I guess.
0

Im not sure that you understood A--C's answer, s/he gave you two seperate solutions, one with a break clause and one without. The 1st solution s/he gave answers your question exactly :)

2 Comments

He* :-) and the first solution will iterate over the whole array, which is bad to an extent. Returning is the "nicest" option without a break.
yes, i think i will go with that one.. hope he doesnt take points out because of that! thanks a lot.. i been going crazy with this problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.