• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



>
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the simplest way is

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

or something like that...
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 )
reply
    Bookmark Topic Watch Topic
  • New Topic