It's not a secret anymore!*
The moose likes Java in General and the fly likes Cloning Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Cloning Question" Watch "Cloning Question" New topic
Author

Cloning Question

Sree Potluri
Ranch Hand

Joined: Aug 12, 2009
Posts: 62
I have a class Book, the code can be null. When I am using clone like this, I am getting NullPointerException, when code is null. But I want to copy even when code is null.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19680
    
  19

No you're not. You're getting null from somewhere, but not from the cloning. I've copied your code, added a main method and ran it successfully:
Sure, the output is a lot of nulls, but that's what I expected.

So, why don't you use the stack trace and find the real cause of the NullPointerException. Most likely you are using the result of the clone's getCode() method without checking if that's null. And that would cause the same error you would get if you would call the same code on the original.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sree Potluri
Ranch Hand

Joined: Aug 12, 2009
Posts: 62
Thanks Rob, let me check that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cloning Question