2

私の友人と私は、大学で Java でゲームをプログラミングする必要があります。作成したツールバーに小さな時計を実装しようとしています。コードを実行すると、次のエラーが表示されます。

スレッド「Thread-0」での例外 java.lang.NullPointerException at GameTimer.run(GameTimer.java:29)

KodeKsToolBar.java

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;

import javax.swing.*;

public class KodeKsToolBar extends JToolBar{
    private static final long serialVersionUID = 1L;
    public static JLabel timeLabel;

    public KodeKsToolBar(GUI listener, Dimension size){
        //unimported Stuff hidden
        //...   
        JLabel timeLabel = new JLabel(GameTimer.currentTime);
        GameTimer t = new GameTimer();
        t.start();
        //clock = new Clock(this);
        //clock.start();

        setFloatable(false);
        add(ToolBarItem_NGame);
        add(ToolBarItem_Load);
        add(ToolBarItem_Save);
        add(ToolBarItem_Resign);
        add(toolBarItem_PauseResume);
        add(timeLabel);
    }
}

GameTimer.java:

import java.awt.*;
import java.awt.event.*;
import java.text.DateFormat;
import java.util.Date;



public class GameTimer extends Thread {
    static boolean running = true;
    int milliSeconds = 0;
    int seconds = 0;
    int minutes = 0;
    static String currentTime = "00:00";

    public void run() {
        while(running) {
            milliSeconds++;
            if(milliSeconds > 999){
                milliSeconds = 0;
                seconds++;
            }
            if(seconds > 59){
                seconds = 0;
                minutes++;
            }
            KodeKsToolBar.timeLabel.setText(getTime()); // <-- This is the line mentioned above, which causes the error
            try {
                Thread.sleep(10);
            } catch (InterruptedException e2) {
                e2.printStackTrace();
            }
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e2) {
            e2.printStackTrace();
        }
    }

    public String getTime() {
        currentTime = minutes + ":" + seconds;
        return currentTime;
    }
}
4

3 に答える 3

3

静的変数にアクセスしています

KodeKsToolBar.timeLabel

KodeKsToolBarただし、コンストラクターで同じ名前のローカル変数を初期化する

JLabel timeLabel = new JLabel(GameTimer.currentTime);

削除JLabelしてそのままにしておきます.-

timeLabel = new JLabel(GameTimer.currentTime);

PS: @Overcraft Full of Eels が述べたように、定数 ( として宣言されているものfinal) を除いて、インスタンス変数は静的ではない必要があることに注意してください。

于 2013-10-12T20:04:56.497 に答える
2

コンストラクターで同じ名前の変数を再宣言することにより、 timeLabel 変数をシャドーイングしています。これは、コンストラクターに宣言された変数のみを初期化し、クラスで配置および宣言された変数は初期化しないことを意味します。コンストラクターで timeLabel を再宣言しないでください。クラスで一度だけ宣言してください。

また、この変数は、ロングショットではなく、静的であってはならないことに注意してください。実際、定数を除くすべての静的変数は、静的であってはならず、インスタンス変数である必要があります。

于 2013-10-12T20:06:10.077 に答える
1

クラス変数を割り当てるstaticため、次のものが必要です。

timeLabel = new JLabel(GameTimer.currentTime);

JLabel離れると別のオブジェクトが作成され、クラスメンバーが初期化されないため、これが必要です。コンパイラは、このような状況について警告する必要があります。

ところで、静的変数は、特定のクラスのグローバル定数のようなものです。実際にそれが必要かどうかを確認して判断する必要があります。

于 2013-10-12T20:07:57.147 に答える