0

我一直在做我的项目,很难知道我可以解决什么问题,所以我可以让它工作,我是一个初学者,只是想让你知道..

这是错误消息:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException: String is null
    at sun.java2d.SunGraphics2D.drawString(SunGraphics2D.java:2817)
    at project2.project2.paint(project2.java:101)
    at sun.awt.RepaintArea.paintComponent(RepaintArea.java:264)
    at sun.awt.RepaintArea.paint(RepaintArea.java:240)
    at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:347)
    at java.awt.Component.dispatchEventImpl(Component.java:4937)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:702)
    at java.awt.EventQueue$4.run(EventQueue.java:700)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
    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 project2;

import java.awt.*;
import java.applet.*;

public class project2 extends Applet
{
    Image tortoise, hare;
    int tortX = 250, hareX = 250;
    final int tortY = 100, hareY = 300, WIDTH = 15, HEIGHT = 50;
    int turn; String turnNum;
    int move; String tMove, hMove;
    public void init()
    {
        tortoise = getImage( getDocumentBase(), "images/tortoise.gif" );
        hare = getImage( getDocumentBase(), "images/hare.gif" );
        move = 0; turn = 0;
    }
    public void control()
    {
        while (( tortX < 985 ) || ( hareX < 985 ))
        {
            move = (int)(10 * Math.random());
            switch (move)
            {
            case 1:
            case 2:
                tortX += (3 * WIDTH);
                hareX += (9 * WIDTH);
                tMove = "Fast Plod"; hMove = "Big Hop";
                break;
            case 3:
            case 4:
            case 5:
                tortX += (3 * WIDTH);
                hareX += WIDTH;
                tMove = "Fast Plod"; hMove = "Small Hop";
                break;
            case 6:
                tortX += WIDTH;
                if (hareX == 250) {} // Agit Nihil 
                else if (hareX <= (250 + (11 * WIDTH)))
                    hareX = 250;
                else
                    hareX -= (12 * WIDTH);
                tMove = "Slow Plod"; hMove = "Big Slip";
                break;
            case 7:
            case 8:
                tortX += (1 * WIDTH);
                if (hareX == 250) {} // Agit Nihil
                else if (hareX <= (250 + (WIDTH)))
                    hareX = 250;
                else
                    hareX -= (2 * WIDTH);
                tMove = "Slow Plod"; hMove = "Small Slip";
                break;
            case 9:
            case 10:
                if (tortX == 250) {} // Agit nihil
                else if (tortX <= (250 + (5 * WIDTH)))
                    tortX = 250;
                else
                    tortX -= (6 * WIDTH);
                tMove = "Slip"; hMove = "Fall Asleep.";
                break;
                // Cuniculus dormit, agit nihil .
            }
            turn++; turnNum = (turn + "");
            repaint();
            for (int i = 1; i <= 10; i++)
            {
                delay();
            }
        }
        tortX = 985; hareX = 985;
        repaint();
    }
    public void paint( Graphics screen )
    {
        drawRace(screen);
        if (tortX >= 985)
        {
            screen.setFont(new Font("Times New Roman", Font.ITALIC, 48));
            screen.drawString("Tortoise Wins", 650, 240);
            clearCurrent(screen);
            fillNext(screen);
        }
        else if (hareX >= 985)
        {
            screen.setFont(new Font("Times New Roman", Font.ITALIC, 48));
            screen.drawString("Tortoise Wins", 650, 240);
            clearCurrent(screen);
            fillNext(screen);
        }
        else
        {      
            screen.drawString(("Turn " + turnNum), 621, 55);
            screen.setFont(new Font("Times New Roman", Font.ITALIC, 12));
            screen.drawString(tMove, 59, 65); screen.drawString(hMove, 66, 255);
            clearCurrent(screen);
            fillNext(screen);
        }
        stop();
    }
    public void clearCurrent( Graphics s )
    {
        s.clearRect(tortX+1, tortY+1, WIDTH-1, HEIGHT-1);
        s.clearRect(hareX+1, hareY+1, WIDTH-1, HEIGHT-1);
    }
    public void fillNext( Graphics s )
    {
        s.fillRect(tortX+1, tortY+1, WIDTH-1, HEIGHT-1);
        s.fillRect(hareX+1, hareY+1, WIDTH-1, HEIGHT-1);
    }
    public void drawRace( Graphics s )
    {
        // Initium
        s.drawRect(250, 100, 750, 50);
        s.drawRect(250, 300, 750, 50);
        int lineX = 265, lineYi = 100, lineYf = 150;
        for (int i = 1; i <= 98; i++)
        {
            if (lineX == 1000)
            {
                lineX = 265; lineYi = 300; lineYf = 350;
            }
            s.drawLine(lineX, lineYi, lineX, lineYf);
            lineX += 15;
        }
        s.fillRect(tortX+1, tortY+1, WIDTH-1, HEIGHT-1);
        s.fillRect(hareX+1, hareY+1, WIDTH-1, HEIGHT-1);
        s.drawImage(tortoise, 59, 80, this);
        s.drawImage(hare, 66, 271, this);
        s.setFont(new Font("Times New Roman", Font.BOLD, 24));
        s.drawString("Race", 250, 55);
    }
    public void delay()
    {
        for (int i = 0; i < 90000000; i++)
        {
        }
    }
    public void stop()
    {
    }
}
4

1 回答 1

2

它看起来像tMoveor hMove(或两者兼有) is null。在某些情况下,看起来像中的switch语句可以使这些未分配。control()要么确保在绘制屏幕之前明确分配tMovehMove,要么(也许更好,因为不能保证control()在绘制小程序之前调用它)替换以下代码:

screen.drawString(tMove, 59, 65);
screen.drawString(hMove, 66, 255);

有了这个:

if (tMove != null) {
    screen.drawString(tMove, 59, 65);
}
if (hMove != null) {
    screen.drawString(hMove, 66, 255);
}
于 2013-10-13T07:06:20.013 回答