• Post Reply Bookmark Topic Watch Topic
  • New Topic

concurrency error  RSS feed

 
Ed Grace
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
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • 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:
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • 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: 56536
172
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!