Jump to content
  • Advertisement
Sign in to follow this  
Hauk

[java] NullPointerException: need to help ironing it out

This topic is 4618 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
if(firstNames[j].compareTo(firstNames[j + 1]) > 0) {

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

thanks

Hauk

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Hmm, thanks for replying

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

Thanks

Hauk

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!