Results 1 to 5 of 5
  1. #1
    coffee is offline Member
    Join Date
    Aug 2009
    Location
    ...
    Posts
    12
    Rep Power
    0

    Smile Null pointer exception? *SOLVED*

    I know what the exception itself means, but I'm confused at how I'm triggering it in this situation;
    Java Code:
    Star str3 = null;
    for (int i=0; i<20; ++i) {
    	str3 = new Star();
    	str3.x=(Math.random()*700-320);
    	str3.y=(Math.random()*700-240);
    	if (str3!=null) {
    		Driver.starary.add(i,str3);
    	}
    }
    I used code that was almost the EXACT same (just with more variables being set) and it works fine, so I'm confused at how this doesn't.
    str3 is not used anywhere else, and x and y are doubles.

    Can anyone see whats wrong?
    Last edited by coffee; 08-03-2009 at 03:22 AM. Reason: SOLVED

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    The stack trace in the console is the place to begin. It often has the line number where the exception was thrown. If you can't figure it out from this then insert one or more println statements to check the value of variables to pinpoint the problem. Chase it down.

  3. #3
    coffee is offline Member
    Join Date
    Aug 2009
    Location
    ...
    Posts
    12
    Rep Power
    0

    Red face

    Oh, sorry. The line where Eclipse said the exception was called was
    Driver.starary.add(i,str3);

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Then I would test both Driver and Driver.starary to see if they are null:
    Java Code:
    Star str3 = null;
    for (int i=0; i<20; ++i) {
        str3 = new Star();
        str3.x=(Math.random()*700-320);
        str3.y=(Math.random()*700-240);
        if (str3!=null) {
            System.out.println("Driver: " + Driver);
            System.out.println("Driver.starary: " + Driver.starary);
            Driver.starary.add(i,str3);
        }
    }

  5. #5
    coffee is offline Member
    Join Date
    Aug 2009
    Location
    ...
    Posts
    12
    Rep Power
    0

    Smile

    Driver is definitely not null, but I may have forgotten to create a new ArrayList for starary...

    *checks*

    Shoot! That was it.. Thanks for the help, hardwired!

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 03:30 AM
  3. Null Pointer Exception
    By Jacinth in forum New To Java
    Replies: 4
    Last Post: 01-22-2009, 01:47 PM
  4. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 06:33 PM
  5. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 07:22 PM

Posting Permissions

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