Dhruv2
Dhruv2

Reputation: 1

Exception in thread :Snake game

i am creating a snake game i am using a 2D array as my backframe. there is no syntax error. i am receiving a Exception in thread. this is where i think the error lies:

 java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new SnakeGame().setVisible(true);
        SnakeGame obj1;
        obj1= new SnakeGame();
        obj1.gameEngine();
                    }

or

case 908:
            x9y8= new JLabel(icon);
            break;
             case 9:
            x0y9= new JLabel(icon);
            break;

i am using netbeans and creating the labels using the palette. therefore the labels are already created. the exception is:

run:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    at snakegamex.SnakeGame.<init>(SnakeGame.java:775)
    at snakegamex.SnakeGame$1.run(SnakeGame.java:744)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
BUILD SUCCESSFUL (total time: 4 seconds)

what is the error and how should i rectify it? the error is supposed to be on line 775

int score=0;
int n=5,c=7;
ImageIcon image= new ImageIcon(getClass().getResource("1327691176237.jpg"));
ImageIcon image1= new ImageIcon(getClass().getResource("1327691176397.jpg"));

i have imported the image into the package and a default package was also created

Upvotes: 0

Views: 172

Answers (1)

codebox
codebox

Reputation: 20264

The exception is happening on line 775 of your SnakeGame class, inside the class constructor - it looks like you are trying to create an ImageIcon object, passing in a null reference instead of some image data.

Upvotes: 2

Related Questions