• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

concurrency error

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 64710
226
  • Mark post as helpful
  • send pies
  • 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: 64710
226
  • Mark post as helpful
  • send pies
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!