【问题标题】:Java programming : setActionCommand() errorJava 编程:setActionCommand() 错误
【发布时间】:2016-09-28 13:09:07
【问题描述】:

我正在开发一款类似俄罗斯方块的多人游戏,其中包含 RMI。我有一个界面(BorderLayout),带有一个工作正常的 JButton(板)网格,侧面有几个面板,底部有一个面板,上面有 3 个 JButton,代表 3 个俄罗斯方块。 当我运行客户端时出现问题:当我启动客户端时,我有一个方法可以获取 3 件(并获取每个件的详细信息),因此我可以获取名称、颜色等来定义当我点击这些 JButtons 时的正确操作。

public void setBoutons(Piece[] bloc) 
{   
    this.piece1 = bloc[0];
    this.piece2 = bloc[1];
    this.piece3 = bloc[2];      

    this.nompiece1 = piece1.getNom();
    this.nompiece2 = piece2.getNom();
    this.nompiece3 = piece3.getNom();

    //System.out.println(nompiece1);

    this.bouton1.setActionCommand(this.nompiece1);
    this.bouton2.setActionCommand(this.nompiece2);
    this.bouton3.setActionCommand(this.nompiece3);

    this.couleur1 = piece1.getCouleur();
    this.couleur2 = piece2.getCouleur();
    this.couleur3 = piece3.getCouleur();

    this.disposition1 = piece1.getDisposition();
    this.disposition2 = piece2.getDisposition();
    this.disposition3 = piece3.getDisposition();        

}

发生的情况是我无法设置 ActionCommand。我尝试了很多方法,即使只是使用像“hello”这样的简单字符串,我仍然遇到这个错误:

Client exception: java.lang.NullPointerException
java.lang.NullPointerException
at Fenetre.setBoutons(Fenetre.java:174)
at Fenetre.<init>(Fenetre.java:60)
at Client.main(Client.java:22)

我把整个代码放在下面,除了 actionPerformed 部分(占用大量空间,如果需要,请向我索取):

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Fenetre extends JFrame implements ActionListener{
    private JButton[][] cases;
    private JButton bouton1;
    private JButton bouton2;
    private JButton bouton3;
    private String nompiece1;
    private String nompiece2;
    private String nompiece3;
    private Color couleur1;
    private Color couleur2;
    private Color couleur3;
    private byte[][] disposition1;
    private byte[][] disposition2;
    private byte[][] disposition3;
    private Piece piece1;
    private Piece piece2;
    private Piece piece3;
    private byte p1;
    private byte p2;
    private byte p3;
    private Interface ninja;
    private JPanel grille;
    private JPanel zonepieces;
    private JPanel zonedroite;
    private JPanel zonehaut;
    private JLabel pseudo;
    private JLabel score;


    public Fenetre(Interface uneinterface) 
    {
        this.ninja = uneinterface;
        try 
        {
            Piece[] unpool = ninja.PoolPieces();
            this.setBoutons(unpool);
        } 
        catch (RemoteException e) 
        {
            e.printStackTrace();
        }

        this.p1 = 0;
        this.p2 = 0;
        this.p3 = 0;

        this.cases = new JButton[10][10];
        this.grille = new JPanel();
        this.zonepieces = new JPanel();
        this.zonedroite = new JPanel();
        this.zonehaut = new JPanel();          
        this.grille.setLayout(new GridLayout(10, 10, 2, 2)); 

        for(int j = 0; j<10; j++)
        {   
            for(int i = 0; i<10; i++) 
            {
                this.cases[j][i] = new JButton(" ");
                this.cases[j][i].addActionListener(this);
                this.cases[j][i].setActionCommand(""+j+i);
                this.cases[j][i].setBackground(Color.gray);
                this.grille.add(cases[j][i]);
            }
        }

        this.bouton1 = new JButton();
        this.bouton1.addActionListener(this);
        this.bouton1.setBackground(Color.WHITE);

        this.bouton2 = new JButton();
        this.bouton2.addActionListener(this);
        this.bouton2.setBackground(Color.WHITE);

        this.bouton3 = new JButton();
        this.bouton3.addActionListener(this);
        this.bouton3.setBackground(Color.WHITE);

        this.zonepieces.setLayout(new FlowLayout());    
        this.zonepieces.add(bouton1);    
        this.zonepieces.add(bouton2);    
        this.zonepieces.add(bouton3);
        this.pseudo = new JLabel("Pseudo");
        this.zonehaut.add(pseudo);
        this.score = new JLabel("Score");
        this.zonedroite.add(score);

        add(grille, BorderLayout.CENTER);
        add(zonepieces, BorderLayout.SOUTH);
        add(zonedroite, BorderLayout.EAST);
        add(zonehaut, BorderLayout.NORTH);

        this.setTitle("1010");
        this.setSize(700, 700);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);

    }

    public void setBoutons(Piece[] bloc) 
    {
        this.piece1 = bloc[0];
        this.piece2 = bloc[1];
        this.piece3 = bloc[2];      

        this.nompiece1 = piece1.getNom();
        this.nompiece2 = piece2.getNom();
        this.nompiece3 = piece3.getNom();

        //System.out.println(nompiece1);

        this.bouton1.setActionCommand(this.nompiece1);
        this.bouton2.setActionCommand(this.nompiece2);
        this.bouton3.setActionCommand(this.nompiece3);

        this.couleur1 = piece1.getCouleur();
        this.couleur2 = piece2.getCouleur();
        this.couleur3 = piece3.getCouleur();

        this.disposition1 = piece1.getDisposition();
        this.disposition2 = piece2.getDisposition();
        this.disposition3 = piece3.getDisposition();    
    }

提前致谢:)

【问题讨论】:

  • 跟踪 NullPointerException 的最佳方法是使用您最喜欢的 IDE 调试您的应用并为 NullPointerException 添加一个异常断点。
  • 源问题是 setActionCommand :当我借用它时,整个事情运行得很好,但是由于我的整个游戏都依赖于那个 ActionCommand,我无法用它做任何事情哈哈
  • 好的,我们需要的最少信息是:第 174 行是哪一行?
  • this.bouton1.setActionCommand(this.nompiece1);
  • 它在帖子的第一块代码中。您还可以在整个代码的末尾找到它(帖子的第 3 个部分)。

标签: java nullpointerexception awt jbutton actionlistener


【解决方案1】:

在这个构造函数中:

public Fenetre(Interface uneinterface) 
{
    this.ninja = uneinterface;
    try 
    {
        Piece[] unpool = ninja.PoolPieces();
        this.setBoutons(unpool);
    } 
    catch (RemoteException e) 
    {
        e.printStackTrace();
    }

您正在调用setBoutons 方法,该方法又访问bouton1 字段。但是该字段只是稍后在构造函数中初始化:

this.bouton1 = new JButton();

这意味着它在您调用setBoutons 时为空。更改您的构造函数以首先初始化 boutons,您应该没问题。

在 Java 中,所有具有引用类型(非基元)的字段在初始化之前都是 null。

【讨论】:

  • 非常感谢,它成功了! (我现在觉得很愚蠢)
  • @AlexandrevanWesel 没有。每个人一开始都会犯这样的错误。你专注于你的愿景,忘记了细节。学习调试您的应用程序并阅读堆栈跟踪。对你有很大帮助!
  • 感谢您的建议 :)
猜你喜欢
  • 2012-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多