Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-07-2009, 04:39 PM
jon80's Avatar
Senior Member
 
Join Date: Feb 2008
Posts: 195
Rep Power: 3
jon80 is on a distinguished road
Default [SOLVED] [newbie] java.lang.NullPointerException
I'm getting a Null.PointerException when I try to iterate through the array.



Code:
import java.util.*;
public class TestFour {

	public static void main(String[] args) {
		
	try {
				
		Integer[] arrayOne = new Integer[100];
		
		/*
		 Generate random numbers and iterate through 
		 the array assigning a random number 
		 within arrayOne[] 
		 */
		Random randomNumber = new Random(1000);
		for (int i : arrayOne ) //breaks here...
		{
			//arrayOne[i] = randomNumber.nextInt();
			arrayOne[i] = 1;
		}
		
		//iterate through array and display contents
		for (int i : arrayOne )
		{
			System.out.println(i);
		}
	}
	catch (AssertionError ex)
	{
System.out.println(ex.toString() + ": variable has a " + ex.getMessage() + " value.");
	}

	}

}
NOTE: It would be nice to have html tags (e.g. <code> </code> that distinguish code snippets from text.

Last edited by jon80; 05-07-2009 at 04:59 PM. Reason: update 2
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 05-07-2009, 04:53 PM
Senior Member
 
Join Date: Apr 2009
Posts: 944
Rep Power: 1
Tolls is on a distinguished road
Default
You can:

Code:
import java.util.Random;

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

		try {

			Integer[] arrayOne = new Integer[100];

			/*
			 * Generate random numbers and iterate through the array assigning a
			 * random number within arrayOne[]
			 */
			Random randomNumber = new Random(1000);
			for (int i : arrayOne) // null?
			{
				// arrayOne[i] = randomNumber.nextInt();
				arrayOne[i] = 1;
			}

			// iterate through array and display contents
			for (int i : arrayOne) {
				System.out.println(i);
			}
		} catch (AssertionError ex) {
			System.out.println(ex.toString() + ": variable has a "
					+ ex.getMessage() + " value.");
		}

	}

}
The hash button sticks code tags around.

You ought to say where the NPE is being thrown from.

ETA: The line you've marked isn't doing what you think. Try writing it as a full for loop instead of using the shorthand and you might see the problem.

Last edited by Tolls; 05-07-2009 at 04:57 PM. Reason: Just noticed
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-07-2009, 04:57 PM
jon80's Avatar
Senior Member
 
Join Date: Feb 2008
Posts: 195
Rep Power: 3
jon80 is on a distinguished road
Default
Thanks, I'll add tags from now on.

The code breaks where I have commented //null ?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-07-2009, 05:01 PM
Senior Member
 
Join Date: Apr 2009
Posts: 944
Rep Power: 1
Tolls is on a distinguished road
Default
Originally Posted by jon80 View Post
Thanks, I'll add tags from now on.

The code breaks where I have commented //null ?
As I say, in the edit (you probably posted before that), try expanding the for loop, rather than using the shorthand.

The int i you're using is actually the value of whatever part of the array you've iterated to, and not the index into the array.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-07-2009, 05:06 PM
jon80's Avatar
Senior Member
 
Join Date: Feb 2008
Posts: 195
Rep Power: 3
jon80 is on a distinguished road
Default
Originally Posted by Tolls View Post
As I say, in the edit (you probably posted before that), try expanding the for loop, rather than using the shorthand.

The int i you're using is actually the value of whatever part of the array you've iterated to, and not the index into the array.
Code:
...
Random randomNumber = new Random(1000);
		int idx = 0;
		for (int i : arrayOne ) //still breaks here
		{
			arrayOne[idx] = randomNumber.nextInt();
			idx++;
		}
...
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-07-2009, 05:19 PM
Senior Member
 
Join Date: Apr 2009
Posts: 944
Rep Power: 1
Tolls is on a distinguished road
Default
OK, you're still using the shorthand.

What
Code:
for (int i : arrayOne)
{}
Expands into is something along the lines of:

Code:
for (int a = 0; a < arrayOne.length; a++)
{
	int i = arrayOne[a];
}
(this isn't actually correct, since in reality it uses Iterators I think, but for the purposes of this explanation it'll do).

As you can see, you're 'i' isn't the index, which is "hidden" from you (it's 'a').

Now, the line:
Code:
int i = arrayOne[a];
is itself shorthand, since arrayOne[a] is an Integer and not an int...it expands to:
Code:
int i = arrayOne[a].intValue();
And this is where you're null pointer exception comes from.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
java.lang.NullPointerException vasavi.singh New To Java 3 02-28-2009 06:41 AM
java.lang.NullPointerException vasavi.singh New To Java 1 02-27-2009 01:36 PM
java.lang.NullPointerException vasavi.singh New To Java 2 02-27-2009 11:11 AM
java.lang.NullPointerException ravian New To Java 1 01-13-2008 08:39 PM
java.lang.NullPointerException Felissa Advanced Java 1 07-05-2007 07:02 AM


All times are GMT +2. The time now is 06:18 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org