File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cloning Question

 
Sree Potluri
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sree Potluri
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob, let me check that.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic