线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException Asteroid

时间:2016-02-25 20:38:02

标签: java netbeans nullpointerexception awt awt-eventqueue

我正在编写一个程序,它将成为小行星游戏的一部分,并将为游戏制作小行星。我似乎在制作任何东西时都遇到了麻烦。当我运行程序时,GUI显示出来,当我点击“开始”按钮时,我得到以下错误:

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)enter code here

我认为某些内容未正确初始化,但我似乎无法找到任何内容

谢谢

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

    class Asteroidfield{

       AsteroidField className = new AsteroidField();
        } 

    public class Asteroid extends PolyBlob {

       public static Random random = new Random();

        public Asteroid(int x, int y, double r) {
            super(x=-100, y=-100, r);
        }

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

            //PolyBlob start off location


            setDelta(idx, jdx);

            //Creating a variable that stores the number of sides
            int num_sides = random.nextInt(4)+5;

            //Creating a variable for pixels in diameter
            int poly_pixels = random.nextInt(20)+10;

            //Declare arrays
            int[] xd = new int [-100];
            int[] yd = new int [-100];
            double[] angle = new double [1];

           //divide num_side by 2pi and choose random angle for each
            for(int m=0; m<num_sides; m++){
                double reg_vert = (num_sides/(Math.PI * 2));
                angle[m] = (m*reg_vert)+(Math.random()*reg_vert);
            }

            //Creating random distance for vertice
            //Get relative x and y coordinates from BlobUtils
            for (int n=0; n<num_sides; n++){
                int vert_dis = random.nextInt(4)+5;
                Point rp = BlobUtils.rotatePoint(vert_dis, angle[n]);
                xd[n] = rp.x;
                yd[n] = rp.y;
            }
            //Instance method from PolyBlob class (set x any y vectices for polygon)
            setPolygon(xd, yd);
        }
    }  

这是第二类

package asteroidfield;
import blobzx.*;
import java.util.Random;


class AsteroidField implements BlobGUI{

 public static void main(String[] args) {
     new AsteroidField();
 }
    Random random;

    //Create sandbox
    SandBox sb = new SandBox();

    AsteroidField() {
        this.random = new Random();
    }

    //Set sandboxmode.flow(asteroid would appear on the other
    //side if it flows out of bound
    //Set the frame rate to 15
    public void init(){
       sb.setSandBoxMode(SandBoxMode.FLOW);
       sb.setFrameRate(15);
    }

    //Create generate() method
    //idx and idy determine the velocity of asteroids
    //declare and initialize rot for rotation
    //PolyBlob creates diamond shaped blob at idx, idy
    //Add asteroid to sandbox

 @Override
    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 b = new Asteroid(idx, idy, rot);
            sb.addBlob(b);
        }

        SandBox.simulate(sb);
    }
}

0 个答案:

没有答案