Results 1 to 6 of 6
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [SOLVED] [newbie] java.lang.NullPointerException

    I'm getting a Null.PointerException when I try to iterate through the array.

    :confused:

    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.");
    	}
    
    	}
    
    }
    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 03:59 PM. Reason: update 2

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    You can:

    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.");
    		}
    
    	}
    
    }
    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 03:57 PM. Reason: Just noticed

  3. #3
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Thanks, I'll add tags from now on.

    The code breaks where I have commented //null ?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote 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.

  5. #5
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Quote 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.
    Java Code:
    ...
    Random randomNumber = new Random(1000);
    		int idx = 0;
    		for (int i : arrayOne ) [B]//still breaks here[/B]
    		{
    			arrayOne[idx] = randomNumber.nextInt();
    			idx++;
    		}
    ...

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    OK, you're still using the shorthand.

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

    Java 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:
    Java Code:
    int i = arrayOne[a];
    is itself shorthand, since arrayOne[a] is an Integer and not an int...it expands to:
    Java Code:
    int i = arrayOne[a].intValue();
    And this is where you're null pointer exception comes from.

Similar Threads

  1. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 3
    Last Post: 02-28-2009, 05:41 AM
  2. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-27-2009, 12:36 PM
  3. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 2
    Last Post: 02-27-2009, 10:11 AM
  4. java.lang.NullPointerException
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 07:39 PM
  5. java.lang.NullPointerException
    By Felissa in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 06:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •