File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes and yet another recursive problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "and yet another recursive problem" Watch "and yet another recursive problem" New topic
Author

and yet another recursive problem

Momo Sawada
Greenhorn

Joined: Dec 10, 2010
Posts: 24
i triedx another recursion exercise... but unfortunately... the program runs.. until the end of the inputs... it displays nullpointerexception... its the first time i've encountered this and i just want to know... because i don't know how to handle a nullPointer exception... thanks in advance!!!
here it is...
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
I have not figured out everything you are doing in this program, but I can tell you what your error is.

You have the statement



This code allocates space for an array of a number of class3 objects, but it does not put any objects in that array. Therefore, every one of those slots in class3 is equal to null. When you get to the statement in your merge method that attempts to do a compare on two elements of class3, neither of them have values, and attempting to call the method compareTo() on one of them fails.

If you mean to copy class1 and class2 elements into class3, you need other statements to do that.

Now, about null pointer exceptions -- they mean very much what they sound like. When you dereference an object reference (which is the technical term for putting a period after it and referring to one of its properties, variables, or methods), that object reference must exist. If it is equal to null, then the runtime has no idea what to do with that reference, and throws a null pointer exception. If you have not trapped null pointer exceptions in an enclosing scope, the program ends.

rc
Momo Sawada
Greenhorn

Joined: Dec 10, 2010
Posts: 24
i think i get what you're saying.... so i will try it... A great many thanks!!!
 
GeeCON Prague 2014
 
subject: and yet another recursive problem