| 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: 16811
|
|
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: 1612
|
|
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: 10043
|
|
the simplest way is
if (a != null && a.method())
or something like that...
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16811
|
|
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: 24081
|
|
|
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: 16811
|
|
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 )
|
 |
 |
|
|
subject: Ok, applauded a little too soon...sort problem
|
|
|