• Advertisement
Sign in to follow this  

[java] NullPointerException: need to help ironing it out

This topic is 4345 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
It will try compare itself to itself?

Thanks

This is confusing me...

Hauk

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
Quote:
Original post by swiftcoder
not sure if Java has a out-of-bounds exception, anyone?

java.lang.ArrayIndexOutOfBoundsException exists.

This is my first attempt at improving your code. It does not compile however because Console is not declared.


import java.util.Collection;
import java.util.TreeSet;

class Person implements Comparable
{

private String firstName;

private String secondName;

private String phoneNumber;

public Person(String first, String second, String phone)
{
firstName = first;
secondName = second;
phoneNumber = phone;
}

public String toString()
{
return secondName + ", " + firstName + " " + phoneNumber;
}

public int compareTo(Object o)
{
Person p = (Person) o;
int c = secondName.compareTo(p.secondName);
if (c == 0)
{
c = firstName.compareTo(p.firstName);
}
return c;
}
}

class Directory
{

public static void main(String[] args)
{
Collection<Person> persons = new TreeSet<Person>();

while (!(Console.endOfFile()))
{
String first = Console.readString();
String second = Console.readString();
String phone = Console.readString();

Person person = new Person(first, second, phone);
persons.add(person);
}

for (Person person : persons)
{
System.out.println(person);
}
}
}


Share this post


Link to post
Share on other sites
Java has an ArrayOutOfBounds exception.

It might have to do with the new String[1000] and your use of the firstName.length in your for loop. I'm not sure if .length will return 1000 even if you haven't actually entered 1000 first names using your console input loop. If that's the case, it's probably throwing a Null Pointer exception when trying to access one past.

Another (and probably more correct guess) is that you are not creating new Strings when you place them into your name arrays. I'm not sure if the array declaration actually creates 1000 new String objects. You may have to declare new String when you add a String to your arrays.

Anywho, just my guesses on limited time. Off to lunch.

Share this post


Link to post
Share on other sites
Yikes, that codes pretty complicated. I don't mean to sound like I don't appreciate it, but if they mark that, they'll know i've gotten help. We haven't used any of the Collection library yet, and we haven't been thought it yet. So if they ask me to step through the code, i'll be like 'Ehm, about that'. I'm really sorry about that! :(

Can anyone do it, just by fiddling with those two or three lines?

Thanks again, and forgive me!

Hauk

Share this post


Link to post
Share on other sites
It'll happen because the length of the array is 1000. However, just because you initialized the array doesn't mean that the array members point to anything (they'll all be null pointers until you assign them). So the loop will zip through the array until it hits the first element which has a following null array element, and then throw your null pointer exception. Simple fix is to use 'i' as follows:


for(int j = 0; j < (i-1); j++)


Daire.

Share this post


Link to post
Share on other sites
Quote:
Original post by Doriath
I'm not sure if .length will return 1000 even if you haven't actually entered 1000 first names using your console input loop.

You are right, it will return 1000.

length is declared as a public final int and never changes, no matter what you put in the array.

And it makes a lot of sense, because "String[]" does NOT mean an array of string objects, but an array of REFERENCES to string objects. And null is a reference to an object of any type ("null instanceof String" is true).

Share this post


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

  • Advertisement