Results 1 to 13 of 13
  1. #1
    xpl0rerchr is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default NullPointerException for no apparent reason?

    Java Code:
    public class World {
    
        public static final int ROWS = 10;
        public static final int COLS = 10; 
        private Organism [][] city;
        public static Random rand;
        World worldCopy;
    
        public World(int numTypeA, int numTypeB) {
            city = new Organism[ROWS][COLS];
            while (numTypeA > 0) {
                Coordinates temp = new Coordinates(rand.nextInt(ROWS), rand.nextInt(COLS));
                if (city[temp.row][temp.col] == null) {
                    city[temp.row][temp.col] = new TypeA(worldCopy, temp);
                    numTypeA--;
                }
            }
            while (numTypeB > 0) {
                Coordinates temp = new Coordinates(rand.nextInt(ROWS), rand.nextInt(COLS));
                if (city[temp.row][temp.col] == null) {
                city[temp.row][temp.col] = new TypeB(worldCopy, temp);
                numTypeB--;
                }
            }
        }
    
    public class Coordinates {
        int row;
        int col;
    
        Coordinates(int x, int y) {
            this.row = x;
            this.col = y;
        }
    
    }
    So at line 12 ( Coordinates temp = new Coordinates(rand.nextInt(ROWS), rand.nextInt(COLS)); ) I'm getting a NullPointerException and I just can't figure out why.
    What I'm trying to do, is randomly place a number of objects of TypeA and then TypeB in an array. This is only part of the code. Your help will be much appreciated. :)

    (Both TypeA and TypeB extend Organism)
    Last edited by xpl0rerchr; 04-10-2012 at 11:43 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: NullPointerException for no apparent reason?

    Refer to Tolls' response at #4 in your last thread. You're making a similar mistake here.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: NullPointerException for no apparent reason?

    Not a NetBeans question. Moving to New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    xpl0rerchr is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException for no apparent reason?

    But I did do

    Java Code:
    private Organism [][] city;
    and then, I did

    Java Code:
    city = new Organism[ROWS][COLS];
    which makes me think it must not be a similar mistake to the one you're referring to.

    Also, sorry for posting at the NetBeans section, it's just that I'm using NetBeans. Will know better next time.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: NullPointerException for no apparent reason?

    I don't see the variable city being used anywhere on line 12. What variables are used on that line? There are four; two of them are primitives and can't be null; one is being assigned a value (or would be if...); the remaining one hasn't been assigned a value and is null.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    xpl0rerchr is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException for no apparent reason?

    Java Code:
    Coordinates temp = new Coordinates(rand.nextInt(ROWS), rand.nextInt(COLS));
    I just can't see what's wrong with this. I'm creating a Coordinates object the way any object is typically created, right?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: NullPointerException for no apparent reason?

    What are the four variables on that line?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    xpl0rerchr is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException for no apparent reason?

    Quote Originally Posted by DarrylBurke View Post
    What are the four variables on that line?

    db
    1) temp, which is of type Coordinates
    2) a new Coordinates object, to be created and then assigned to temp
    3) rand.nextInt(ROWS)
    4) rand.nextInt(COLS)

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: NullPointerException for no apparent reason?

    temp is a variable
    a new coordinates object isn't a variable
    rand.nextInt(ROWS) isn't a variable.
    rand.nextInt(COLS) isn't a variable.

    Try again.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    xpl0rerchr is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException for no apparent reason?

    Quote Originally Posted by DarrylBurke View Post
    temp is a variable
    a new coordinates object isn't a variable
    rand.nextInt(ROWS) isn't a variable.
    rand.nextInt(COLS) isn't a variable.
    Quote Originally Posted by DarrylBurke View Post
    What variables are used on that line? There are four;
    Well there's nothing else on that line and you previously said there were 4 variables.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: NullPointerException for no apparent reason?

    rand?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    xpl0rerchr is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException for no apparent reason?

    Quote Originally Posted by JosAH View Post
    rand?
    Yes, as I want TypeA and TypeB objects to be created and randomly placed in the city array. So I'm generating random numbers from 0 to (and including) 9 for rows and columns.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: NullPointerException for no apparent reason?

    What's the value of rand?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Code stops for no reason
    By Bagzli in forum New To Java
    Replies: 9
    Last Post: 03-03-2011, 04:56 AM
  2. Best reason to not go to school ever
    By Zack in forum Forum Lobby
    Replies: 5
    Last Post: 11-28-2010, 05:09 AM
  3. Need a reason for output
    By Hemant16 in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-26-2010, 11:05 AM
  4. Getting errors for some reason
    By Swarvy in forum New To Java
    Replies: 7
    Last Post: 09-30-2008, 03:45 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
  •