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?

Pisike007
  • 101
  • 1
  • 6
  • possible duplicate of [What is a Null Pointer Exception?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) – Paul Bellora Feb 16 '13 at 02: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. – Stephen C Feb 16 '13 at 03:06

2 Answers2

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.

A--C
  • 36,351
  • 10
  • 106
  • 92
  • 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? – Pisike007 Feb 16 '13 at 02:47
  • @PatriciaQuintanilla simply `break;` once you come across a `null` reference. Edited the answer. – A--C Feb 16 '13 at 02:50
  • 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! – Pisike007 Feb 16 '13 at 02:53
  • 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. – A--C Feb 16 '13 at 02:56
  • 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. – A--C Feb 16 '13 at 03:02
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 :)

phcoding
  • 608
  • 4
  • 16
  • 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. – A--C Feb 16 '13 at 03:05
  • 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! – Pisike007 Feb 16 '13 at 03:09