Results 1 to 6 of 6
- 05-07-2009, 03:39 PM #1
[SOLVED] [newbie] java.lang.NullPointerException
I'm getting a Null.PointerException when I try to iterate through the array.
:confused:
NOTE: It would be nice to have html tags (e.g. <code> </code> that distinguish code snippets from text.Java 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."); } } }Last edited by jon80; 05-07-2009 at 03:59 PM. Reason: update 2
- 05-07-2009, 03:53 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You can:
The hash button sticks code tags around.Java 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."); } } }
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 03:57 PM. Reason: Just noticed
- 05-07-2009, 03:57 PM #3
Thanks, I'll add tags from now on.
The code breaks where I have commented //null ?
- 05-07-2009, 04:01 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
- 05-07-2009, 04:06 PM #5
- 05-07-2009, 04:19 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
OK, you're still using the shorthand.
What
Expands into is something along the lines of:Java Code:for (int i : arrayOne) {}
(this isn't actually correct, since in reality it uses Iterators I think, but for the purposes of this explanation it'll do).Java Code:for (int a = 0; a < arrayOne.length; a++) { int i = arrayOne[a]; }
As you can see, you're 'i' isn't the index, which is "hidden" from you (it's 'a').
Now, the line:
is itself shorthand, since arrayOne[a] is an Integer and not an int...it expands to:Java Code:int i = arrayOne[a];
And this is where you're null pointer exception comes from.Java Code:int i = arrayOne[a].intValue();
Similar Threads
-
java.lang.NullPointerException
By vasavi.singh in forum New To JavaReplies: 3Last Post: 02-28-2009, 05:41 AM -
java.lang.NullPointerException
By vasavi.singh in forum New To JavaReplies: 1Last Post: 02-27-2009, 12:36 PM -
java.lang.NullPointerException
By vasavi.singh in forum New To JavaReplies: 2Last Post: 02-27-2009, 10:11 AM -
java.lang.NullPointerException
By ravian in forum New To JavaReplies: 1Last Post: 01-13-2008, 07:39 PM -
java.lang.NullPointerException
By Felissa in forum Advanced JavaReplies: 1Last Post: 07-05-2007, 06:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks