Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1808
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the simplest way is

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

or something like that...
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic