Results 1 to 12 of 12
  1. #1
    applewood13 is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

    Question Multidimentional array-null pointer

    hey, im working on a pacman applet and i lined up dots on the game board all spaced out, but i am trying to only display the dots that are on pacman's path and not the ones outside his boundries. To solve this i made a multidimentional array and set it up in my init() method as follows:
    int [][] grid = {
    {1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1},
    {1,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,1},
    {1,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,1},
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,1},
    {1,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1},
    {0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0},
    {0,0,0,1,0,1,1,1,1,1,1,1,0,1,0,0,0},
    {0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0},
    {1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1},
    {0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0},
    {0,0,0,1,0,1,1,1,1,1,1,1,0,1,0,0,0},
    {0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0},
    {1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1},
    {1,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,1},
    {1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1},
    {0,1,0,1,0,1,0,0,0,0,0,1,0,1,0,1,0},
    {1,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1},
    {1,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,1},
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
    };
    (1 being visible and 0 being hidden)

    then in my paint routine i made an if statement as follows:
    for(int x=0;x<17; x++)
    {
    for(int y=0;y<20; y++)
    {
    if(grid[x][y]==1)
    {
    (draw dots)
    }
    }
    }

    but when i run my program it is saying that there is a null pointer exception on the line "if(grid[x][y]==1)"

    any help would be greatly appreciated

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The stack trace will be helpful, please copy paste it.

  3. #3
    applewood13 is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

    Default

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Pacman.paint(Pacman.java:229)
    at sun.awt.RepaintArea.paintComponent(Unknown Source)
    at sun.awt.RepaintArea.paint(Unknown Source)
    at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    (line 229 being "if(grid[x][y]==1)")

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Before that if statement add a print statement which prints the value of x, y and the value at that position and see what you get. It seems that somethig isn't initialized, this should point out if any of the array is initialized or just certain parts.

  5. #5
    applewood13 is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

    Default

    i replaced the if statement with "System.out.println(grid[x][y]);" and i get the following error:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Pacman.paint(Pacman.java:229)
    at sun.awt.RepaintArea.paintComponent(Unknown Source)
    at sun.awt.RepaintArea.paint(Unknown Source)
    at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    im thinking i did something wrong with the declaration or in the setting of the values... im not 100% because this is my first time using a multidimensional array in java

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your array declaration seems fine to me, what happens if you print just the array without any coordinates. I'm wondering if the array ever gets initialized, it appears that it isn't.

  7. #7
    applewood13 is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

    Default

    hmmm it seems that all the slots of the arrays are empty, when i output the array it output "null" once for each dot...

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I am not especially familiar with applets but now you know what's causing the error. The paint method is trying to draw using information that hasn't been initialized. You need to figure out what approach is going to allow the initialization to occur.

  9. #9
    applewood13 is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

    Default

    ok, thank you for your help, greatly appreciated :)

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Is the code especially large? If it isn't, you should post it here with code tags
    [code ]<-omit space
    YOUR CODE HERE
    [/code]

    Also, consider trying to make a smaller program that uses a 2d array to do some drawing and post it here. Working on a small program may help you find what's wrong.

  11. #11
    applewood13 is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

    Default

    i found the problem, it was my declaration of the array, i declared it as "grid = new int[][]{" and declared it at the top of my program as "int[][] grid;" and it solved my null pointer

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Glad you solved your problem. Please mark your thread solved with the thread tools at the top.

Similar Threads

  1. Null Pointer Exception in an Array of JCheckBoxes
    By eLancaster in forum New To Java
    Replies: 4
    Last Post: 04-02-2011, 04:50 PM
  2. Replies: 11
    Last Post: 03-15-2011, 11:36 PM
  3. Null Pointer
    By theen3my in forum AWT / Swing
    Replies: 3
    Last Post: 10-03-2009, 02:10 PM
  4. null pointer help
    By mayhewj7 in forum New To Java
    Replies: 5
    Last Post: 02-17-2009, 11:51 PM
  5. null pointer exeption
    By mj23brm in forum New To Java
    Replies: 4
    Last Post: 02-10-2009, 11:50 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
  •