Java编程:setActionCommand()错误

时间:2016-05-30 16:25:47

标签: java nullpointerexception awt jbutton actionlistener

我正在开发一款包含RMI的多人类俄罗斯方块游戏。我有一个界面(BorderLayout),其中有一个JButtons(板)网格,它可以处理罚款,侧面有几块面板,底部有一个面板,上面有3个JButton,代表3个俄罗斯方块。 当我运行客户端时出现问题:当我启动客户端时,我有一个获取3个池的方法(并获取每个的详细信息),因此我可以获取名称,颜色等来定义当我对这些JButton进行陈词滥调时,请采取适当的行动。

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();    
    }

提前致谢:)

1 个答案:

答案 0 :(得分:0)

在这个构造函数中:

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中,所有具有引用类型(非基元)的字段在初始化之前都为空。