• 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

Sorting a multidimensional array?

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you may have guessed from the title i am trying to sort a two dimensional array, now in general i can do this.i.e. with a set array that i create.

From playing with my current code it would appear that when trying to do this with a custom created array based on variable data input i get a NullPointerException error.

So a short example of the code that works:-




And the code i am trying to use:-


The error code i get is
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ColumnComparator2.compare(CalculateData.java:170) In the example code it relates to line 41
at java.util.TimSort.countRunAndMakeAscending(TimSort.java:329)
at java.util.TimSort.sort(TimSort.java:203)
at java.util.TimSort.sort(TimSort.java:173)
at java.util.Arrays.sort(Arrays.java:659)
at CalculateData.actionPerformed(CalculateData.java:138) In the example code that relates to line 18
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
at java.awt.EventQueue.access$000(EventQueue.java:102)
at java.awt.EventQueue$3.run(EventQueue.java:662)
at java.awt.EventQueue$3.run(EventQueue.java:660)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:676)
at java.awt.EventQueue$4.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)


From what i can tell the array is created but the sort/comparator is reading it as null but there are elements in the array at that point as i have used a system.out.println() to check.

I am guessing it is to do with the way i am creating the array.e.g. is it not really a two dimensional array?









 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is really doing it the hard way. Much simpler to design an object and give the object knowledge of how it should be sorted.

That said, clearly (at least) one of name, rank or score is null.
 
wayne morton
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As i am still learning and this is my first foray into sorting a multidimentional array, you couldn't give me an idea of how to set that up?

In regard to the null, which i have been looking for, as i mentioned i have put in a separate System.out.println() method in between the creation of the array and the array sort and all elements are present so my only assumption would be that my comparitor can't understand the array when i build it that way although it works fine when i hard build it, setting the elements as a set array?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

wayne morton wrote:As i am still learning and this is my first foray into sorting a multidimentional array, you couldn't give me an idea of how to set that up?



If you do it as suggested, you won't have a multidimensional array. You'll have a 1D array.

What do the individual elements in represent? What do the represent when taken together as a whole? The answer to the second question tells you the name of the class you'll be defining. The answers to the first question tell you the names of the member variables in that class, and give you hints as to their types.

In regard to the null, which i have been looking for, as i mentioned i have put in a separate System.out.println() method in between the creation of the array and the array sort and all elements are present so my only assumption would be that my comparitor can't understand the array when i build it that way although it works fine when i hard build it, setting the elements as a set array?



Dont' just assume. Observe. You need to put print statements right before the line that's giving you the NPE, to find out what's null, and then work backwards from there to find out how it got to be null:


Although that will become irrelevant if you get away from the 2D array approach and use a proper object instead.
 
wayne morton
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies.

The word assume was probably a wrong choice to use but in the context of the whole which was as a question i thought it would be clearer that i was guessing because i had tried to find what was causing the null by using print statements after every line and nothing was showing null up to the point of the error, and then after the error there was no print statement as there was an error which interrupted the code. I even removed the code which caused the error and allowed the code to carry on, still using print statements and everything was as expected so there was just an error at those points, so my only conclusion was that i wasn't creating the array in a form that the comparator could understand which as i have now got it working, seems to have been born out to be true?

People have suggested using an object instead, but as i already said i am learning and basically have no idea where to start in that regard and i have tried searching for it with little success as, i guess what is due to normally the case, i don't even know what terminology to use to find what i am searching for. Hence i have done what will likely be an inefficient and dirty workaround and got it working. That workaround is creating a hard copy array (as in the original working version of the code) and then modifying it with the second version which didn't work by itself.i.e. a combination of the two methods i used in my original post.

Unless someone is willing to at least point me in the right direction of how to create and use this object that has been mentioned it is going to stay that way as just telling someone they should be using something is like telling someone who can't drive that driving a car would be quicker than walking.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your frustration is understandable, but recognize you've chosen a fairly difficult problem to cut your teeth on, especially as you haven't yet been exposed to working with classes. I would consider that a prerequisite to understanding comparators. Since your program deals with soldiers, a Soldier class would be very useful. Read through this tutorial. It should take 30 or 40 minutes. http://docs.oracle.com/javase/tutorial/java/javaOO/classes.html By the end of it you should have a very good idea what a Soldier class should look like.

This is not to say, btw, that 'multidimensional' arrays don't have their uses. As you gain experience in Java you'll come to recognize approaches that are more appropriate to whatever task is at hand.
 
wayne morton
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the link Dennis, that looks interesting.

The experience, especially of the different ways something can be achieved is most of my problem as i have only really just begun to delve into the world of java and thus am limited to working with what i have learnt up to now and making the best of that when creating programs. Having said that i appreciate when people point out better or alternate ways to achieve my goal as that increases my scope of learning and points me in new directions to improve my knowledge, although that can often be a struggle as it is often assumed that i know what they are talking about.i.e. i have been thinking in the wrong way rather than me simply not having encountered that alternate way yet.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic