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 Insertion Sort on array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Insertion Sort on array" Watch "Insertion Sort on array" New topic
Author

Insertion Sort on array

John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I have a class createArray, which ininitializes an object array of 100. I fill it with values which begin to overflow and go negative. Let's say theres only 20 numbers in this array of size 100 and negatives are at the end. I use an insertion sort to sort this array (also apart of the createArray class) but get null pointer exception. When adding integers to the array in my test program I use the Integer wrapper class. Does this happen because you can't use compareTo() with an int and null value? I don't want to reduce the size of my array. What about the case where
{10, 24, -4, null,null, null, -100)? not all of the final entries will be null...This is the code inside my method insertionSort..also do I have to implement Comparable? where do I do that, in my createArray class or my insertion sort method? Do I also have to write a compareTo() method since i'm using integers (i assumed I could use compareTo() and get an integer back, positive, negative or 0 without writing a compareTo method??

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I don't know how this code could throw an Exception, because it's a long ways from even compiling.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
why? what's wrong with it? I get it working up until it reaches a null value in the array...
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'd guess "v" is null at your compareTo() call. Test it for null before that line and just skip the compare if null.

Defraging the array to move all the non-nulls before the first null would be a different looking operation. Do you need to do that, too?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
ya v is null at the compareTo() call, I do have to move all the non-nulls before the first null...i'm completely lost at this point. How to you do compareTo with null values and integers which are apart of a class object??
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by John Lockheart:
why? what's wrong with it? I get it working up until it reaches a null value in the array...

The insertionSort method has no argument list (no parentheses)...

...and the variable "array" is not defined (maybe this is one of the missing arguments?).

With respect to the null problem, you might consider something like this...

[ May 24, 2007: Message edited by: marc weber ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

I don't know if you are confused about what exactly "null" means here or not, but just in case...

if you have a null object (which an Integer is), that means that no object has been created. There is a difference between having a Integer object with a value of zero and having a null reference. So when you say:
How to you do compareTo with null values and integers which are apart of a class object??
you can't - it doesn't make sense to try. this is basically saying "how do you compare nothing to something"?

what has been suggested is you to a test to make sure v is NOT null before you do the comparison...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Insertion Sort on array