aspose file tools*
The moose likes Beginning Java and the fly likes Ok, applauded a little too soon...sort problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Ok, applauded a little too soon...sort problem" Watch "Ok, applauded a little too soon...sort problem" New topic
Author

Ok, applauded a little too soon...sort problem

Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
I thought I had the sort routine worked out, but now it is throwing a NullPointerException whenever I add an item to the array, then try to sort it. The array is loaded with 23 items, I add a 24th, then do the sort to put the array into sequence by Software name, that is when it crashes. The method involved is addApplication.



>
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18995
    
  40

Basically, you have null elements in the array -- so when a comparison with a null occurs, you can get an NPE, as you have in this case.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Ok, but if I declare an array large enough to hold what I have, plus allow for expansion, then I'm going to have null elements. How do I handle this situation? From what I've learned, once I've declared an array to be [24] I can't increase it to [25] to add another entry without recompiling the entire application.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

Is there a reason you're not using a List, ArrayList or LinkedList? Those are dynamically sized so you'll be able to add and delete items without caring about having enough space or comparing null values....

Janeice


When you do things right, people won't be sure you've done anything at all.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

the simplest way is

if (a != null && a.method())

or something like that...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18995
    
  40

Mike Lipay wrote:Ok, but if I declare an array large enough to hold what I have, plus allow for expansion, then I'm going to have null elements. How do I handle this situation?


You have two options... (1) you can use the version of sort() that allows you to sort a range in the array -- meaning only sort the area in the array that you are actually using. or (2) you can use the version of sort() that takes a comparator. A comparator allows either of the two elements, to be compared, to be null (well, you still have to write it so, but it is possible).

Henry
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Use an ArrayList. There's rarely a reason to use a plain array except when you need to pass one to an API that needs one.


[Jess in Action][AskingGoodQuestions]
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Regarding the lists and array lists...haven't learnt about them yet.

Fred, I have no idea where to put that, and is it a.method or do I replace method with something?

Henry, I thought I did have those instructions, did I do it wrong?

>
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18995
    
  40

Mike Lipay wrote:
Henry, I thought I did have those instructions, did I do it wrong?


Here is the code snippet that calls the sort...



This sorts the whole array, by natural ordering. Not part of the array. And not using a comparator.

And BTW, a comparator and comparable are two different things.

Henry
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Ok, right now comparators are beyond me, couldn't make heads or tails out of the doc for it, so I went with supplying the start and end point for the sort. Works. Thanks. (again )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ok, applauded a little too soon...sort problem