0

I've been trying to create the first part of an asteroid game in java that simply displays 20 asteroids moving around in a field. When ever I go to start it I get these errors:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at blobzx.SandBox$1.actionPerformed(SandBox.java:82)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

I've looked through other articles here but cannot seem to find a solution for this scenario. Here is the code I have written:

    package AsteroidField;

import blobzx.*;
import java.awt.Point;
import java.util.Random;

public class AsteroidField implements BlobGUI{

    Random random;

    SandBox sand = new SandBox();

   public static void main( String[] args){


        new AsteroidField();
    }

    public AsteroidField(){

        this.random = new Random();

    }

    public void init(){

        sand.setSandBoxMode(SandBoxMode.FLOW);
        sand.setFrameRate(15);
        sand.init(this);

    }

    public void generate(){

        for(int i=0; i<20; i++){

           int idx = random.nextInt(6)-3;
           int idy = random.nextInt(6)-3;
           double rot = (random.nextInt(2)-1)*.1;

            PolyBlob a = new Asteroid(idx, idy, rot);
            sand.addBlob(a);
        }

        SandBox.simulate(sand);

    }
}

    package AsteroidField;

import blobzx.*;
import java.util.Random;
import java.awt.Point;

public class Asteroid extends PolyBlob {

    private Random random = new Random();

    public Asteroid(int idx, int jdx, double rot ){


        super( -100, -100, rot );


        setDelta(idx, jdx);

        int sides = random.nextInt(4)+5;
        int pix = random.nextInt(20)+10;

        int[] x = new int [-100];
        int[] y = new int [-100];
        double[] angle = new double [1];

        for(int i=0; i<sides; i++){

            double region = ((Math.PI*2)/sides);
            angle[i] = (i*region)+(Math.random()*region);

        }

        for(int j=0; j<sides; j++){

            int distance = random.nextInt(10)+5;
            Point point = BlobUtils.rotatePoint(distance, angle[j]);

            x[j] = point.x;
            y[j] = point.y;

        }

        setPolygon(x, y);

    }
}

Any help on this issue would be greatly appreciated.

  • The stack trace is pretty clear that your error is occurring in your `SandBox` class, so you should probably include that class's code (or at least all of the ActionListeners it creates) in your question. – VGR Feb 25 '16 at 23:59
  • The stacktrace shows you where the exception occurs, so check this/these code line(s)? – Tom Feb 25 '16 at 23:59

1 Answers1

0

The one thing I saw that struck out at me right away were the lines:

int[] x = new int [-100];
int[] y = new int [-100];

Error I got after compiling just these phrases embeded in a java Program.

Exception in thread "main" java.lang.NegativeArraySizeException
    at HelloWorld.main(HelloWorld.java:5)  

I assume that other basic syntax errors probably exist so you should check your basic syntax.

Tom
  • 16,842
  • 17
  • 45
  • 54
star2wars3
  • 83
  • 1
  • 10