简体   繁体   English

需要帮助调试,代码可以编译但无法运行

[英]Need help debugging, code compiles but won't run

Hey I could use help debugging this program. 嘿,我可以使用帮助调试该程序。 The code is not mine, it is from an answer to a question and I wanted to try it but I get a NullPointerException and can't figure out where the problem is. 该代码不是我的代码,它是从问题的答案而来的,我想尝试一下,但是得到了NullPointerException,无法弄清楚问题出在哪里。 I think the problem may be image paths but I am not sure so I could use help. 我认为问题可能出在图像路径上,但是我不确定是否可以使用帮助。

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

} }

Your problem is here: 您的问题在这里:

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

You don't have the required images in your project, so getClass().getResource() will return null and you will have a NullPointerException in the constructor of ImageIcon . 您的项目中没有所需的图像,因此getClass().getResource()将返回null并且ImageIcon的构造函数中将出现NullPointerException

What you have to do is put the following files in your project: 您需要做的是将以下文件放入您的项目中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM