0

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

Dhruv2
  • 1
  • 2
  • One of the constructors of [ImageIcon](https://docs.oracle.com/javase/8/docs/api/javax/swing/ImageIcon.html#ImageIcon-byte:A-), the one you are using in the constructor of `SnakeGame` in line `775` is causing a NullPointerException, most likely because you are passing a `null` to it. – A4L Nov 18 '14 at 12:51

1 Answers1

2

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.

codebox
  • 19,927
  • 9
  • 63
  • 81
  • i changed the code there but the error is still there – Dhruv2 Nov 18 '14 at 13:02
  • I think the problem is that the jpg files can't be located so the getResource method is returning null - the answers here might help: http://stackoverflow.com/questions/4301329/java-class-getresource-returns-null – codebox Nov 18 '14 at 13:28
  • 1
    @Dhruv2: See [tag:embedded-resource] for additional guidance. – trashgod Nov 18 '14 at 13:58