• 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Issues using MergeSort with an object array pulled from a CSV file. Null Pointer Exceptions. Solved

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, solved the issue. Couldn't figure out what was wrong with my original attempt but went back using
as a template and it worked.


Pastebin link to the code below (not the driver class but can add that if needed)

So, our algorithms class decided to take a huge step up for an assignment and it's left me completely stranded in a lake of not knowing what to do at all. Every example I've seen has used either simpler inputs (i.e. not object arrays) or things that are out of our purview like iterators. In debug mode the temporary array does start to fill up but will stop after 7 or so iterations. The ones that do fill up before the program terminates with the exception are filling up correctly. The import from the CSV file is working, it can be seen working clearly while running in debug mode and checking what's in the array.

I had thought the problem is occurring on this line
which has now been altered to but neither work.

However, upon further debugging I'm starting to think the problem may actually be the if and while loops failing and the project resetting z to zero before it should, reaching this line much earlier than anticipated. Presumably if this is causing issues then the line above it, is also a gremlin


Pastebin link to code.
NB. The example has been obscured somewhat for stealth purposes so the original Employee has been changed to Customer but I missed one here. the original is still an employee object so that problem does not exist in my original code and is not the issue that's causing the problem



Any help would be extremely appreciated. Was doing very well in college but this assignment and gRPC in general has me questioning life choices again.
 
Marshal
Posts: 26523
81
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Solved? Great! Glad we could help!

(It's actually true that explaining a problem to somebody else can cause the person doing the explaining to realize what's going on.)
 
Marshal
Posts: 72612
317
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Well done getting this far Does the exception occur because you have nulls in your array, or because you have a “real” object with a field pointing to null?
Don't use compareTo(Object). Comparable is a parametrised interface, so you should make Customer implement Comparable<Customer>:-You will see from the link that Comparable imposes a “total ordering” on its implementing type. I interpret that as meaning you always compare objects by the same criteria. That means two kinds of sorting:-
  • 1: Ordering by those criteria.
  • 2: Ordering by the very same criteria, but backwards.
  • If you are sorting by multiple criteria, or your objects might provide different ways of being sorted, I think you shouldn't use Comparable, but create a Comparator object. This is one way to do it, with “anonymous classes”:-Needless to say, there is an easier way to write that code with λ expressions, but I don't know whether you are familiar with λs. You can use Comparators to get much more flexibility than you would for compareTo(). For example, you can allow for elements in the array being null like this:-I think that is right to sort all nulls first, but it won't work if getLastName() returns null.
    There is a quite short, but good, section in the Java™ Tutorials telling you about some of the errors that can occur with object comparison. Unfortunately, it doesn't tell you that you can join several Comparator λs together and there are methods to sort nulls first and nulls last.
     
    Tobias Totti
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ritchie, thank you very much. That's an unbelievable response, think the Ranch will get me over the trauma of using StackOverflow. It's a great first impression

    There were no nulls in the array, honestly have no idea why it was causing the issue but I'm having trouble adapting code designed for ints over to the object array and found that the compareTo method I was using worked fine when I changed the method to fit the template in the video. I think it's probable that it was looping through incorrectly and nulling values that had already been filled but that's a relatively uneducated guess from a supremely confusing debug. But it was filling up for a bit before replacing added values with nulls and indicating OutofBounds exceptions within the debug window when hovered over the TempArray. Yeah, I've never even heard of Lambda codes .

    I'm in the middle of an assignment overload but might run through the answer next weekend and see if I can get the code I posted here to work
     
    Campbell Ritchie
    Marshal
    Posts: 72612
    317
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Tobias Totti wrote:. . . the Ranch will get me over the trauma of using [website XYZ] . . .

    I am going to say something I shouldn't:-
    Hahahahahahahahaha!

    . . . compareTo method I was using worked fine when I changed the method to fit the template in the video.

    Which video? There are videos out there by the thousand and some aren't any good. They may give you an out of date solution or an incomplete solution or insuffifent explanation so you end up writing something and not knowing what you wrote Generics was introduced 16½ years ago, so anybody writing compareTo(Object) is horribly out of date. Very briefly, generics obviates the tempation to write casts, which you had to use before.

    I think it's probable that it was looping through incorrectly and nulling values that had already been filled . . .

    We'd have to see your code to know about that.

    OutofBounds exceptions w. . .

    An index out of bounds exception suggests an index ≥ theArray.length or < 0, and a common way to get such errors is by not writing a loop correctly; a correctly written for loop will not overflow the bounds of an array. I think we'd have to see your code to know for certain; you are probably not alowed to show us any code.

    Yeah, I've never even heard of Lambda codes . . .

    I haven't got the time to explain λs; that would take an hour or so, but λs are very nice to use and not at all difficult once you have grasped the concept. Find yourself a book like Urma Fusco and Mycroft Java8 in Action/Java8 and 9 in Action/Modern Java in Action (Manning 2015/2017) (=different edtions of the same thing with different names), and read chapter 3.
     
    Campbell Ritchie
    Marshal
    Posts: 72612
    317
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Always use this form of loop if you are iterating an array with a for:-That loop will not exceed the bounds of an array of any size, so always consider that the default form to start writing for loops with. You will sometimes need to vary the form of a loop; for example, if you need to access an element and the element at the preceding index try this form of loop:-The following is the standard form to iterate an array backwards:-


    Addition: The following form of loop can be relied upon to go wrong:-
     
    Campbell Ritchie
    Marshal
    Posts: 72612
    317
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Did you mean the video you embedded? That show correct code, which is unfortunately useless to beginners because it doesn't explain how it is copying elements over. You will end up having to learn the code off by heart. You want to see a diagram showing how the elements are moved from array to array in order to understand what is happening. You also want to see an array with two values that sort as “the same”. , and to see what happens to them. That is an important feature when you are sorting collections. What if you have two elements which return 0 from compare(), but have other fields different from each other?
    I would prefer to see most of those methods marked private. It is possible to write merge sort back into the original array, but that is very awkward, so we usually use a temporary array. Since memory is cheap nowadays, that usually isn't a problem.

    The idea behind merge sort is that you divide your array/list into smaller arrays/lists until you get to one that is guaranteed to have all its elements in the right order. That means size ≤ 1 Then you join the two arrays together so whichever element of the two smaller arrays is “smaller” first (etc), provided there are actually elements to merge. Once you have got your small arrays/lists in order, you go back up the call stack and merge them into larger arrays/lists, etc., until you get back to the size of the original
     
    Campbell Ritchie
    Marshal
    Posts: 72612
    317
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This video is a lot better. He uses a different technique, using multiple smaller arrays, which is expensive on garbage collection. He also says that merge sort is faster than bubble sort; in fact bubble sort is faster for small arrays (not sure of the size, but probably < 50 or < 100). I would prefer to see a starting array with an odd number of elements, so you can see how to avoid problems with 0‑length arrays, and two values the same, but it is a much better video than what you found. You can see the elements being merged because they change colour on screen.
     
    Tobias Totti
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The reason behind the selection of Mergesort was heavily influenced by making one of the following questions about Big O notation much easier but tbh I ended up causing more problems than i had thought.

    Originally I was trying to adapt this code for it but that caused the issues described in my OP but I had had no issues with it previously.



    I had thought I was quite good at the searching algorithms and the logic behind them but for me the biggest issue at the moment seems to be that as soon as an example moves away from the specific datatype or applies to a more complicated variant I lose confidence extremely quickly and end up editing based on trial and error more than anaylsis of the code
     
    Campbell Ritchie
    Marshal
    Posts: 72612
    317
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Please indent your code correctly; if you don't, all sorts of weird and wonderful errors can creep in without anybody being able to see tham. When writing arrays, the [] is part of the type and therefore belongs before the identifier: int[] f please, rather than int f[].
    Let's start by putting some indentation in your code, and a few other formatting improvements:-I shall let you go back to that code; I can't remember merge sort off by heart, but I can't  at present see anything other than formatting problems. I am a bot worried seeing so many variables, however. Does that code work at all? What goes wrong with it?

    . . . trial and error more than anaylsis of the code

    Trial and error won't work and that's that. You can try 1,000,000× and some of your atempts will be correct. Identifying thse attempts is the real problem. If you are using objectss and a Comparator, you only need one change:-Challenge: find out what happens if you change <= to < in line 17.

    What does line 41 do? Are you right to re‑assign anythiing to 0 there? I am suspicious about that. Please check the algorithm carefully; you can find it on Wikipedia or similar. Or the second video. Please check line 40 similarly.

    I think I wasn't quite right earlier, saying you can implement merge sort wihtou a temporary array. That should be you can use multiple small arrays. Some of those will be throwaway objects created with a local scope and may not need garbage collection.
     
    Tobias Totti
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ritchie, you have my word I'll come back for those challenges once my deadline dies its death tomorrow
     
    a wee bit from the empire
    ScroogeXHTML - a fast and small RTF to HTML5 and XHTML converter library
    https://coderanch.com/t/742149/ScroogeXHTML-RTF-HTML-XHTML-converter
    reply
      Bookmark Topic Watch Topic
    • New Topic