为什么我收到此错误?我删除了一段冗余代码,现在我有了这个

时间:2014-05-16 19:54:29

标签: java pointers null breakout

我正在为我的第一年学位写一个突破性游戏,我刚刚重新实现了项目,这意味着我之前尝试过的方法现在已经多余了。所以我试图删除它,我得到了这个:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at breakout.ViewBreakout.display(ViewBreakout.java:106)
at breakout.ViewBreakout.drawActualPicture(ViewBreakout.java:69)
at breakout.ViewBreakout.drawPicture(ViewBreakout.java:161)
at breakout.ViewBreakout.paint(ViewBreakout.java:147)
at javax.swing.RepaintManager$3.run(RepaintManager.java:819)
at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

这是我的代码,项目位置位是多余的

package breakout;

import java.util.Observable;
import static breakout.Global.*;
/**
* Model of the game of breakout
*  The active object ActiveModel does the work of moving the ball
*/

public class ModelBreakout extends Observable
{
  private GameObject ball;      // The ball
  private GameObject bricks[];  // The bricks
  private GameObject bat;       // The bat
  private GameObject item[];       // The item
  private int itemLocations[];  //Item location

  private ModelActivePart am  = new ModelActivePart( this );
  private Thread activeModel  = new Thread( am );

  private int score = 0;



public void createGameObjects()
{
ball   = new GameObject(ModelActivePart.randomRange(10, W -10), H/2, BALL_SIZE, BALL_SIZE, Colour.RED );
bat    = new GameObject(W/2, H - BRICK_HEIGHT*4, BRICK_WIDTH*3, 
                        BRICK_HEIGHT, Colour.GRAY);
item   = new GameObject[100];                      
bricks = new GameObject[BRICKS];

int[] itemLocations = new int[100];

for(int i = 1; i < BRICKS + 1; i++)
{
    if(i <= 10)
    {
        bricks[i - 1] = new GameObject((i*BRICK_WIDTH - 40) + i * 7, 100, BRICK_WIDTH, 
                        BRICK_HEIGHT, Colour.BLUE);

        if(itemLocations[0] == i - 1)
        {
            item[0] = new GameObject(((i*BRICK_WIDTH - 40) + i * 7) + BRICK_WIDTH/2, 100, BRICK_WIDTH/3, 
            BRICK_HEIGHT/2, Colour.GRAY);
        }
        if(itemLocations[1] == i - 1)
        {
            item[1] = new GameObject((i*BRICK_WIDTH - 40) + i * 7 + BRICK_WIDTH/2, 100, BRICK_WIDTH/3, 
            BRICK_HEIGHT/2, Colour.GRAY);
        }
        if(itemLocations[2] == i - 1) 
        {
            item[2] = new GameObject((i*BRICK_WIDTH - 40) + i * 7 + BRICK_WIDTH/2, 100, BRICK_WIDTH/3, 
            BRICK_HEIGHT/2, Colour.GRAY);
        }
        if(itemLocations[3] == i - 1)
        {
            item[3] = new GameObject((i*BRICK_WIDTH - 40) + i * 7 + BRICK_WIDTH/2, 100, BRICK_WIDTH/3, 
            BRICK_HEIGHT/2, Colour.GRAY);
        }
    }
    else if (i > 10 && i <= 20)
    {
        bricks[i - 1] = new GameObject(((i - 10)*BRICK_WIDTH - 40) + (i - 10) * 7, 150, BRICK_WIDTH, 
                        BRICK_HEIGHT, Colour.RED);

         if(itemLocations[0] == i - 1)
        {
            item[0] = new GameObject((i*BRICK_WIDTH - 40) + i * 7 + BRICK_WIDTH/2, 100, BRICK_WIDTH/3, 
            BRICK_HEIGHT/2, Colour.GRAY);
        }
        if(itemLocations[1] == i - 1)
        {
            item[1] = new GameObject((i*BRICK_WIDTH - 40) + i * 7 + BRICK_WIDTH/2, 100, BRICK_WIDTH/3, 
            BRICK_HEIGHT/2, Colour.GRAY);
        }
        if(itemLocations[2] == i - 1) 
        {
            item[2] = new GameObject((i*BRICK_WIDTH - 40) + i * 7 + BRICK_WIDTH/2, 100, BRICK_WIDTH/3, 
            BRICK_HEIGHT/2, Colour.GRAY);
        }
        if(itemLocations[3] == i - 1)
        {
            item[3] = new GameObject((i*BRICK_WIDTH - 40) + i * 7 + BRICK_WIDTH/2, 100, BRICK_WIDTH/3, 
            BRICK_HEIGHT/2, Colour.GRAY);
        }
    }
    else
    {
        bricks[i - 1] = new GameObject(((i - 20)*BRICK_WIDTH - 40) + (i - 20) * 7, 200, BRICK_WIDTH, 
                        BRICK_HEIGHT, Colour.GRAY);

         if(itemLocations[0] == i - 1)
        {
            item[0] = new GameObject((i*BRICK_WIDTH - 40) + i * 7 + BRICK_WIDTH/2, 100, BRICK_WIDTH/3, 
            BRICK_HEIGHT/2, Colour.GRAY);
        }
        if(itemLocations[1] == i - 1)
        {
            item[1] = new GameObject((i*BRICK_WIDTH - 40) + i * 7 + BRICK_WIDTH/2, 100, BRICK_WIDTH/3, 
            BRICK_HEIGHT/2, Colour.GRAY);
        }
        if(itemLocations[2] == i - 1) 
        {
            item[2] = new GameObject((i*BRICK_WIDTH - 40) + i * 7 + BRICK_WIDTH/2, 100, BRICK_WIDTH/3, 
            BRICK_HEIGHT/2, Colour.GRAY);
        }
        if(itemLocations[3] == i - 1)
        {enter code here
            item[3] = new GameObject((i*BRICK_WIDTH - 40) + i * 7 + BRICK_WIDTH/2, 100, BRICK_WIDTH/3, 
            BRICK_HEIGHT/2, Colour.GRAY);
        }
    }

}
// *[1]**********************************************************
// * Fill in code to place the bricks on the board         DONE *
// **************************************************************
}

public void startGame()             { activeModel.start(); }

public GameObject getBall()         { return ball; }

public GameObject[] getBricks()     { return bricks; }

public GameObject[] getItem()       { return item;  }

public int getItemLoc( int index )  { return itemLocations[index]; }

public GameObject getBat()          { return bat; }

public void addToScore( int n )     { score += n; }

public int getScore()               { return score; }

public void stopGame()              { }


/**
 * Move the bat dist pixels. (-dist) => left or (+dist) => right
 * @param dist - Distance to move
 */
 public void moveBat( float dist )
 {
   // *[2]**********************************************************************
   // * Fill in code to prevent the bat being moved off the screen        Done *
   // **************************************************************************
   float BatX = bat.getX();

if ( BatX  == 5 && dist == -5)
{
    bat.moveX(0);
}
else if (BatX + (BRICK_WIDTH*3) == W - 5 && dist == 5)
{
    bat.moveX(0);
}    
else
{
    Debug.trace( "Model: Move bat = %6.2f", dist );
    bat.moveX(dist);
    //modelChanged();
}
}

/**
 * Model has changed so notify observers so that they
 *  can redraw the current state of the game
 */
public void modelChanged()
{
  setChanged(); notifyObservers();
}

}

对于可怕格式化的代码感到抱歉..

这是我的观点突破代码:

            package breakout;



            import java.awt.Color;
            import java.awt.Dimension;
            import java.awt.Font;
            import java.awt.FontMetrics;
            import java.awt.Graphics;
            import java.awt.Graphics2D;
            import java.awt.event.KeyEvent;
            import java.awt.event.KeyListener;
            import java.awt.geom.Rectangle2D;
            import java.awt.image.BufferedImage;
            import java.util.Observable;
            import java.util.Observer;

            import javax.swing.JFrame;

            import static breakout.Global.*;

            /**
             * Displays a graphical view of the game of breakout
             *  Uses Garphics2D would need to be re-implemented for Android
             */
            public class ViewBreakout extends JFrame implements Observer
            { 
              private ControllerBreakout controller;
              private GameObject   ball;             // The ball
              private GameObject[] bricks;           // The bricks
              private GameObject   bat;              // The bat
              private GameObject[]   item;           // The item

              private int          score =  0;       // The score
              private int          frames = 0;

              public ViewBreakout()
              {
                setSize( W, H );                        // Size of window
                addKeyListener( new Transaction() );    // Called when key press
                setDefaultCloseOperation(EXIT_ON_CLOSE);
              }

              /**
               *  Code called to draw the current state of the game
               *   Uses draw:       Draw a shape
               *        fill:       Fill the shape
               *        setPaint:   Colour used
               *        drawString: Write string on display
               *  @param g Graphics context to use
               */
              public void drawActualPicture( Graphics2D g )
              {
                frames++;
                // White background

                g.setPaint( Color.white );
                g.fill( new Rectangle2D.Float( 0, 0, W, H ) );

                Font font = new Font("Monospaced",Font.BOLD,24); 
                g.setFont( font ); 

                // Blue playing border

                g.setPaint( Color.blue );              // Paint Colour
                g.draw( new Rectangle2D.Float( B, M, W-B*2, H-M-B ) );

                // Display the ball
                display( g, ball );

                // Display the bricks that make up the game
                // *[3]**********************************************************
                // * Fill in code to display bricks (A brick may not exist)     *
                // **************************************************************

                for(int i = 0; i <= BRICKS -1; i++)
                {
                    if (bricks[i].getX() != 0 || bricks[i].getY() != 0)
                    {
                        display( g, bricks[i]);
                    }
                }


                for(int i = 0; i < ModelActivePart.getItemCount() ; i++)
                {
                    display( g, item[i]);
                }

                // Display the bat
                display( g, bat );

                // Display state of game
                g.setPaint( Color.black );
                FontMetrics fm = getFontMetrics( font );
                String fmt = "BreakOut: Score = [%6d] fps=%5.1f";
                String text = String.format(fmt, score,
                                frames/(Timer.readTimeTaken()/1000.0 )
                               );
                if ( frames > 100 ) { frames = 0; Timer.startTimer(); }
                g.drawString( text, W/2-fm.stringWidth(text)/2, (int)M*2 );
              }

              private void display( Graphics2D g, GameObject go )
              {
                switch( go.getColour() )
                {
                  case GRAY: g.setColor( Color.gray );
                             break;
                  case BLUE: g.setColor( Color.blue );
                             break;
                  case RED:  g.setColor( Color.red );
                             break;
                  case WHITE: g.setColor( Color.white );
                }
                g.fill( new Rectangle2D.Float( go.getX(),     go.getY(), 
                                               go.getWidth(), go.getHeight() ) );
              }

              /**
               * Called from the model when its state has changed
               */

              @Override
              public void update( Observable aModel, Object arg )
              {
                ModelBreakout model = (ModelBreakout) aModel;
                // Get from the model the ball, bat, bricks & score
                ball    = model.getBall();              // Ball
                bricks  = model.getBricks();            // Bricks
                bat     = model.getBat();               // Bat
                score   = model.getScore();             // Score
                item    = model.getItem();              // Item
                Debug.trace("Update");
                repaint();                              // Re draw game
              }

              @Override
              public void update( Graphics g )          // Called by repaint
              {
                drawPicture( (Graphics2D) g );          // Draw Picture
              }

              @Override
              public void paint( Graphics g )           // When 'Window' is first
              {                                         //  shown or damaged
                drawPicture( (Graphics2D) g );          // Draw Picture
              }

              private BufferedImage theAI;              // Alternate Image
              private Graphics2D    theAG;              // Alternate Graphics

              public void drawPicture( Graphics2D g )   // Double buffer
              {                                         //  to avoid flicker
                if (  theAG == null )
                {
                  Dimension d = getSize();              // Size of curr. image
                  theAI = (BufferedImage) createImage( d.width, d.height );
                  theAG = theAI.createGraphics();
                }
                drawActualPicture( theAG );             // Draw Actual Picture
                g.drawImage( theAI, 0, 0, this );       //  Display on screen
              }

              /**
               * Need to be told where the controller is
               */
              public void setController(ControllerBreakout aPongController)
              {
                controller = aPongController;
              }

              /**
               * Methods Called on a key press 
               *  calls the controller to process
               */
              class Transaction implements KeyListener  // When character typed
              {
                @Override
                public void keyPressed(KeyEvent e)      // Obey this method
                {
                  // Make -ve so not confused with normal characters
                  controller.userKeyInteraction( -e.getKeyCode() );
                }

                @Override
                public void keyReleased(KeyEvent e)
                {
                  // Called on key release including specials
                }

                @Override
                public void keyTyped(KeyEvent e)
                {
                  // Send internal code for key
                  controller.userKeyInteraction( e.getKeyChar() );
                }
              }
            }

0 个答案:

没有答案