gpt4 book ai didi

java - 空指针异常和 Action 监听器

转载 作者:行者123 更新时间:2023-11-30 07:18:53 25 4
gpt4 key购买 nike

我正在研究我的简单游戏应用程序,将所有内容从 700 行精简到 177 行,以消除重复。当我尝试运行我的应用程序时,我收到一个 Exception in thread "AWT-EventQueue-0"java.lang.NullPointerException 抛给我。我知道它的意思(通过阅读 this )并且我知道它与我如何执行 actionListener 有关,但我不确切地知道如何 .

我遵循了我遇到的所有处理数组和 JButtons 的教程,但肯定有一些小问题。 private JButton[][] blocks = new JButton[rows][cols] 被正确声明,所以不是那样,我没有改变我的做法 blocks[i][j] .addActionListener(this) 来 self 以前版本的代码,除了我用 block[i][j] 更改了 block1、block2... 等。

这是扔给我的东西:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at blox.Blox.game(Blox.java:105)
at blox.Blox.access$000(Blox.java:25)
at blox.Blox$1.run(Blox.java:173)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
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.awt.EventQueue.dispatchEvent(EventQueue.java:697)
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)

这是我的代码:

public class Blox implements ActionListener {

private final String gameVersion = "Blox - v1.0.2";
private final int rows = 7;
private final int cols = 7;

// Board Inits
private ImageIcon purpleBlock = new ImageIcon();
private ImageIcon redBlock = new ImageIcon();
private ImageIcon blueBlock = new ImageIcon();
private ImageIcon greenBlock = new ImageIcon();
private ImageIcon closeImage = new ImageIcon();
private JButton[][] block = new JButton[rows][cols];
private JPanel boardBg = new JPanel();

// Error console init
private JLabel errorMessage = new JLabel();
private JPanel errorBg = new JPanel();
private JButton closeError = new JButton();

// Score/UI Inits
private JButton resetBoard = new JButton("Reset Board");

/**
* 700 x 480 Frame Size
* Sets Up and displays initial scene.
*
*/
private void game() {

// Initialize mainFrame
JFrame mainFrame = new JFrame(gameVersion);
Dimension minSize = new Dimension(700,480);
mainFrame.setMinimumSize(minSize);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLayout(null);
mainFrame.setResizable(false);
try {
mainFrame.setContentPane(new JLabel(new ImageIcon(
ImageIO.read(new File("bggrad.jpg")))));
} catch (IOException e) {
e.printStackTrace();
}

// Setup block Images
try {
purpleBlock.setImage(ImageIO.read(new File("purpleBlock.png")));
redBlock.setImage(ImageIO.read(new File("redBlock.png")));
blueBlock.setImage(ImageIO.read(new File("blueBlock.png")));
greenBlock.setImage(ImageIO.read(new File("greenBlock.png")));
closeImage.setImage(ImageIO.read(new File("close.jpg")));
} catch (IOException e) {
e.printStackTrace();
}

// Sets up error message panel
// Only displayed if error occurs on ActionListener
errorMessage.setText("");
errorMessage.setForeground(Color.black);
Font newLabelFont = new Font(errorMessage.getFont().getName(),
Font.BOLD,errorMessage.getFont().getSize());
errorMessage.setFont(newLabelFont);
closeError.setIcon(closeImage);
closeError.setBounds(0,0,24,24);
errorBg.setBounds(0,430,700,30);
errorBg.setBackground(Color.lightGray);
errorBg.add(closeError);
errorBg.add(errorMessage);
errorBg.setVisible(false);

// Sets up Score/UI Portions of Screen
resetBoard.setBounds(60,363,175,25);
resetBoard.setVisible(true);
resetBoard.addActionListener(this);

// Set Up Board
boardBg.setLayout(new GridLayout(rows,cols));
boardBg.setBounds(300,50,350,350);
for(int i=0; i<rows; i++) {
for(int j=0; i<cols; j++) {
block[i][j].setIcon(randomizer());
block[i][j].addActionListener(this);
boardBg.add(block[i][j]);
}
}

mainFrame.getContentPane().add(boardBg);
mainFrame.getContentPane().add(errorBg);
mainFrame.getContentPane().add(resetBoard);

//mainFrame.pack();
mainFrame.setVisible(true);
}


/**
* Checks onClick event for which JButton was clicked
* and changes JButton.setIcon() accordingly.
*
* @param none
* @return none
* @see java.awt.event.*
* @see JButton
* @see setIcon()
* @see getIcon()
*
*/
public void actionPerformed(ActionEvent e) {

if(e.getSource()==resetBoard) {
System.out.println("Resetting");
gameReset();
}

if(e.getSource()==errorBg || e.getSource()==errorMessage) {
errorBg.setVisible(false);
}
}

public void gameReset() {

for(int i=0; i<rows; i++) {
for(int j=0; i<cols; j++) {
block[i][j].setIcon(randomizer());
}
}
}

public ImageIcon randomizer() {

Random r = new Random();
int rNum = r.nextInt(4);

if(rNum==0) {
return purpleBlock;
} else if(rNum==1) {
return redBlock;
} else if(rNum==2) {
return greenBlock;
} else {
return blueBlock;
}
}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Blox().game();
}
});
}
}

最佳答案

问题可能出在你的循环上:

    for(int i=0; i<rows; i++) {
for(int j=0; i<cols; j++) {
block[i][j].setIcon(randomizer());
block[i][j].addActionListener(this);
boardBg.add(block[i][j]);
}
}

block[i][j] 未初始化。首先像这样初始化它们:

    for(int i=0; i<rows; i++) {
for(int j=0; i<cols; j++) {.
block[i][j] = new JButton();
block[i][j].setIcon(randomizer());
block[i][j].addActionListener(this);
boardBg.add(block[i][j]);
}
}

关于java - 空指针异常和 Action 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15029339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com