Results 1 to 12 of 12
- 04-28-2011, 11:20 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 14
- Rep Power
- 0
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
- 04-28-2011, 11:49 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The stack trace will be helpful, please copy paste it.
- 04-28-2011, 11:55 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 14
- Rep Power
- 0
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)")
- 04-29-2011, 12:20 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-29-2011, 12:28 AM #5
Member
- Join Date
- Dec 2009
- Posts
- 14
- Rep Power
- 0
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
- 04-29-2011, 12:55 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-29-2011, 12:57 AM #7
Member
- Join Date
- Dec 2009
- Posts
- 14
- Rep Power
- 0
hmmm it seems that all the slots of the arrays are empty, when i output the array it output "null" once for each dot...
- 04-29-2011, 01:00 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-29-2011, 01:01 AM #9
Member
- Join Date
- Dec 2009
- Posts
- 14
- Rep Power
- 0
ok, thank you for your help, greatly appreciated :)
- 04-29-2011, 01:03 AM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-30-2011, 04:06 AM #11
Member
- Join Date
- Dec 2009
- Posts
- 14
- Rep Power
- 0
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
- 04-30-2011, 06:15 AM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Glad you solved your problem. Please mark your thread solved with the thread tools at the top.
Similar Threads
-
Null Pointer Exception in an Array of JCheckBoxes
By eLancaster in forum New To JavaReplies: 4Last Post: 04-02-2011, 04:50 PM -
Best way to solve this? (Multidimentional array with strings as "keys")
By webpler in forum New To JavaReplies: 11Last Post: 03-15-2011, 11:36 PM -
Null Pointer
By theen3my in forum AWT / SwingReplies: 3Last Post: 10-03-2009, 02:10 PM -
null pointer help
By mayhewj7 in forum New To JavaReplies: 5Last Post: 02-17-2009, 11:51 PM -
null pointer exeption
By mj23brm in forum New To JavaReplies: 4Last Post: 02-10-2009, 11:50 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks