This week's book giveaway is in the Design forum.
We're giving away four copies of Experimentation for Engineers: From A/B testing to Bayesian optimization and have David Sweet on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

concurrency error

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


hey everyone, i was wondering if someone could help me out with my code... i know some of it may be confusing and could probably be cut down a little bit but im ok with that.... my problem is that i need to sort my names array and as you can see i already have that coded and it compiles fine, but when i run the code i get the following errors:
Exception in thread "main" java.lang.NullPointerException
at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.sort(Arrays.java:1079)
at grades.main(finalfinal.java:59)
if anyone could help me out with this and help explain what i can do to fix this, my computer science teacher was completley stumped when i showed him this, because it works completely fine when the sorting code is taken out, so again any and all help would be appreciated and thank you in advanced
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You get that error because the first element in the array is null (has no value). You must not have any null values in the array in order to avoid a NullPointerException.

In Java, arrays are indexed from 0. So the first element in the names array is names[0]. However, when you fill the array you start looping from an index of 1:

Since you never assign to the first index in the array (names[0]) the value remains null. You should start the for loop at 0 rather than 1:


Note that you make the same mistake for all your other arrays. Also, if you want only 3 names (as it appears you do) you should change the array sizes to 3, rather than 4:
 
Marshal
Posts: 77558
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Once I found out where the Exception occurred, it became obvious why. Insert the following just before line 95, which appears to be where the Exception occurs:Then go back to where those objects are initialised and see if you can work out why it went wrong.
 
Campbell Ritchie
Marshal
Posts: 77558
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see Steve has beaten me to it.

It is not good design to have several parallel arrays; you ought to have a Student class incorporating the name and marks, and use a Student[] array.
 
Not looking good. I think this might be the end. Wait! Is that a tiny ad?
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic