【问题标题】:Need help debugging, code compiles but won't run需要帮助调试,代码编译但不会运行
【发布时间】:2014-03-08 07:16:34
【问题描述】:

嘿,我可以帮助调试这个程序。该代码不是我的,它来自一个问题的答案,我想尝试一下,但我得到一个 NullPointerException 并且无法弄清楚问题出在哪里。我认为问题可能是图像路径,但我不确定,所以我可以使用帮助。

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;

public class CircleImages {

private int score = 0;
private JTextField scoreField = new JTextField(10);

public CircleImages() {
    scoreField.setEditable(false);

    final ImageIcon[] icons = createImageIcons();
    final JPanel iconPanel = createPanel(icons, 8);

    JPanel bottomLeftPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
    bottomLeftPanel.add(new JLabel("Score: "));
    bottomLeftPanel.add(scoreField);

    JPanel bottomRightPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
    JButton newGame = new JButton("New Game");
    bottomRightPanel.add(newGame);
    JButton quit = new JButton("Quit");
    bottomRightPanel.add(quit);

    JPanel bottomPanel = new JPanel(new GridLayout(1, 2));
    bottomPanel.add(bottomLeftPanel);
    bottomPanel.add(bottomRightPanel);

    newGame.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            reset(iconPanel, icons);
            score = 0;
            scoreField.setText(String.valueOf(score));
        }
    });

    JFrame frame = new JFrame();
    frame.add(iconPanel);
    frame.add(bottomPanel, BorderLayout.PAGE_END);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

private void reset(JPanel panel, ImageIcon[] icons) {
    Component[] comps = panel.getComponents();
    Random random = new Random();
    for(Component c : comps) {
        if (c instanceof JLabel) {
            JLabel button = (JLabel)c;
            int index = random.nextInt(icons.length);
            button.setIcon(icons[index]);
        }
    }
}

private JPanel createPanel(ImageIcon[] icons, int gridSize) {
    Random random = new Random();
    JPanel panel = new JPanel(new GridLayout(gridSize, gridSize));
    for (int i = 0; i < gridSize * gridSize; i++) {
        int index = random.nextInt(icons.length);
        JLabel label = new JLabel(icons[index]);
        label.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e) {
                score += 1;
                scoreField.setText(String.valueOf(score));
            }
        });
        label.setBorder(new LineBorder(Color.GRAY, 2));
        panel.add(label);
    }
    return panel;
}

private ImageIcon[] createImageIcons() {
    String[] files = {"DarkGrayButton.png",
        "BlueButton.png",
        "GreenButton.png",
        "LightGrayButton.png",
        "OrangeButton.png",
        "RedButton.png",
        "YellowButton.png"
    };
    ImageIcon[] icons = new ImageIcon[files.length];
    for (int i = 0; i < files.length; i++) {
        icons[i] = new ImageIcon(getClass().getResource("/circleimages/" + files[i]));
    }
    return icons;
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new CircleImages();
        }
    });
}

}

【问题讨论】:

  • 能否请您发布整个异常堆栈跟踪?
  • 您应该学习的一项重要技能是如何使用调试器。以后会对你有很大的帮助。
  • 这是我可以发布的尽可能多的内容:CircleImages 的 javax.swing.ImageIcon 的线程“AWT-EventQueue-0”java.lang.NullPointerException 中的异常。(未知来源) .createImageIcons(CircleImages.java:90) 在 CircleImages.(CircleImages.java:15) 在 CircleImages$3.run(CircleImages.java:98) 在 java.awt.event.InvocationEvent.dispatch(Unknown Source) 在 java .awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source)

标签: java debugging user-interface nullpointerexception


【解决方案1】:

你的问题在这里:

icons[i] = new ImageIcon(getClass().getResource("/circleimages/" + files[i]));

您的项目中没有所需的图像,因此getClass().getResource() 将返回null,并且您将在ImageIcon 的构造函数中有一个NullPointerException

你要做的就是将以下文件放入你的项目中:

  • /circleimages/DarkGrayButton.png
  • /circleimages/BlueButton.png
  • /circleimages/GreenButton.png
  • /circleimages/LightGrayButton.png
  • /circleimages/OrangeButton.png
  • /circleimages/RedButton.png
  • /circleimages/YellowButton.png

【讨论】:

  • 把它们放在我的项目中是什么意思?喜欢将它们与班级放在同一个文件夹中吗?现在,我将图像与班级放在同一文件夹中。
  • @user3390522 你必须将它们放在你的类路径中,在一个名为 circleimages 的文件夹中(通常在文件夹 srcsrc/java 中)。如果你的类在一个包中,它不会和类在同一个文件夹中。
  • @user3390522 不客气。您能否将答案标记为已接受(绿色检查)?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-12
  • 2013-01-15
  • 1970-01-01
  • 2013-03-24
  • 2010-10-21
相关资源
最近更新 更多