[java] NullPointerException: need to help ironing it out

Started by
13 comments, last by Fred304 18 years ago
Hey guys, I have the following code for reading in a first name, a second name, and a third name. But every time I compile this, it says there is a null pointer exception on line 46. Anyway, here it is. I can't figure it out for the life of me.
import java.util.*;

class Person {
	
	String firstName;
	String secondName;
	String phoneNumber;
}

	

class Population {

}



class Directory {
	
	public static void main(String[] args) {		

		String [] firstNames = new String[1000];
		String [] secondNames = new String[1000];
		String [] phoneNumbers = new String[1000];

		int i = 0;

		while(!(Console.endOfFile())) {

			Person newContact = new Person();

			newContact.firstName = Console.readString();
			firstNames = newContact.firstName;

			newContact.secondName = Console.readString();
			secondNames = newContact.secondName;

			newContact.phoneNumber = Console.readString();
			phoneNumbers = newContact.phoneNumber;

			i++;
		}

		for(int j = 0; j < firstNames.length; j++) {

			if(firstNames[j].compareTo(firstNames[j + 1]) > 0) {
		
				String temp = firstNames[j];
				firstNames[j] = firstNames[j + 1];
				firstNames[j + 1] = temp;

				String temp2 = secondNames[j];
				secondNames[j] = secondNames[j + 1];
				secondNames[j + 1] = temp2;

				String temp3 = phoneNumbers[j];
				phoneNumbers[j] = phoneNumbers[j + 1];
				phoneNumbers[j + 1] = temp3;
	
			}
		}
		
		for(int k = 0; k < firstNames.length; k++) {

			System.out.println(secondNames[k] + ", " + firstNames[k] + phoneNumbers[k] + ".");
		}

	}
}
Thanks guys! Hauk
Advertisement
Which is line 46?

Dave
if(firstNames[j].compareTo(firstNames[j + 1]) > 0) {

that little one there, sorry for not pointing it out :(

thanks

Hauk
Quote:Original post by Hauk
if(firstNames[j].compareTo(firstNames[j + 1]) > 0) {


The only thing that can throw a NullPointerException on that line is the dereference to one of the elements of the firstNames array. Make sure that your array doesn't have any null values in it.
Hmm, thanks for replying

So your saying I need to have the entire array filled?

Thanks

Hauk
Quote:Original post by Hauk
Hmm, thanks for replying

So your saying I need to have the entire array filled?

Thanks

Hauk


for(int j = 0; j < firstNames.length; j++) {	if(firstNames[j].compareTo(firstNames[j + 1]) > 0) {


What do you think will happen when the index j is equal to firstNames.length-1

It will try compare itself to itself?

Thanks

This is confusing me...

Hauk
This is possibly the least object oriented Java code I have ever seen :)

Why do you keep three seperate String arrays instead of one Person array? And please use the JCF (Java Collection Framework) to sort your persons. No need to sort manually in Java.
Good idea, i'll code in a new person array. And its part of an assignment, we have to manually sort it. we'll lose marks if we don't. stupid, i know.

but i really need help getting rid of that NullPointerException!

Thanks

Hauk
Quote:Original post by Hauk
It will try compare itself to itself?


It will attempt to compare itself with a non-existant item after the last item in the array (which will throw an ArrayOutOfBounds or similar, can't remember right now).

Since it isn't throwing an ArrayOutOfBounds, it means you null pointer exception is being thrown before it reaches the end of the array.

Edit: on second thoughts, it may be throwing that null pointer to indicate you are out side of array bounds, not sure if Java has a out-of-bounds exception, anyone?

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement