aspose file tools
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

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Cloning Question" Watch "Cloning Question" New topic

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

Joined: Oct 27, 2005
Posts: 19959

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.

How To Ask Questions How To Answer Questions
Sree Potluri
Ranch Hand

Joined: Aug 12, 2009
Posts: 62
Thanks Rob, let me check that.
Have you tried LearnNowOnline?
subject: Cloning Question