简体   繁体   English

该行提供了一个空指针,我不知道为什么

[英]This line is giving a nullpointer and I have no idea why

I'm trying to add buttons to all these panels, so I can check if they're clicked. 我正在尝试向所有这些面板添加按钮,以便可以检查是否单击了它们。 I'm still new to Java and this is how we were taught how to do it. 我还是Java的新手,这就是我们被教导如何做的方法。

Right now I'm making a big panel and adding 48 new panels onto it and then adding buttons on each of those panels, so I can make an action event. 现在,我正在制作一个大面板,并在其上添加48个新面板,然后在每个面板上添加按钮,以便进行动作事件。 If there is a way for me to check if I clicked the panel then I could do that, but I have no idea how. 如果我可以检查一下是否单击面板,则可以这样做,但是我不知道如何。

I'm getting the NullPointerException on the line "panel[x].add(click[x]);" 我在“ panel [x] .add(click [x]);”行上收到NullPointerException。

package CatchTheMouse;

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

public class CatchTheMouse extends JFrame implements ActionListener, MouseListener{
    final int ROWS = 8;
    final int COLS = 6;
    final int GAP = 2;
    final int MAX_PANELS = ROWS * COLS;
    int clicks;
    int hits;
    int percentage = 0;
    int width;
    int height;
    int panelX;
    int panelY;
    int whichPanel = (int)(Math.random() * 47 + 1);

    JButton[] click = new JButton[MAX_PANELS];
    JLabel grats = new JLabel("");
    JLabel spot = new JLabel("X");
    JPanel[] panel = new JPanel[MAX_PANELS];
    JPanel pane = new JPanel(new GridLayout(ROWS, COLS, GAP, GAP));
    Font xFont = new Font("Ariel", Font.BOLD, 20);
    Font font = new Font("Ariel", Font.PLAIN, 12);

    public CatchTheMouse() {
        super("Catch the Mouse");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300,300);
        add(spot);
        spot.setFont(xFont);
        add(grats);
        grats.setFont(font);
        add(pane);
        for(int x = 0; x < MAX_PANELS; ++x) {
            panel[x] = new JPanel();
            pane.add(panel[x]);
            panel[x].setBackground(Color.RED);
            panel[x].add(click[x]);
            click[x].addActionListener(this);
            click[x].setVisible(false);
        }
        pane.setBackground(Color.BLACK);
        panel[whichPanel].add(spot);
    }

    public void mouseClicked(MouseEvent e) {
        clicks = e.getClickCount();
    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void actionPerformed(ActionEvent e) {
        Object src = e.getSource();
        if(src == click[whichPanel]) {
            hits++;
            grats.setText("You have made " + Integer.toString(hits) + " hits");
        }
    }

    public static void main(String[] args) {
        CatchTheMouse frame = new CatchTheMouse();
        frame.setVisible(true);
    }
}

A guess, this line: 猜想这行:

panel[x].add(click[x]);

You're trying to add JButton's that have not yet been constructed. 您正在尝试添加尚未构建的JButton。 Construct them first before adding! 在添加之前,先构建它们!

click[x] = new JButton("something");
panel[x].add(click[x]);

In the future though, when asking for help here, please include all relevant information, including and especially the line that throws any exceptions that you're stuck on. 不过,将来在这里寻求帮助时,请包括所有相关信息,尤其是包括引发您所遇到的任何异常的行。

You are missing click[x] = new JButton() prior to using click[x] . 在使用click[x]之前,您缺少click[x] = new JButton() You got it right with the initialization of panel[x] . 通过panel[x]的初始化,您做对了。

    for(int x = 0; x < MAX_PANELS; ++x) {
        panel[x] = new JPanel();
        pane.add(panel[x]);
        panel[x].setBackground(Color.RED);           
        click[x] = new JPanel(); // add this
        panel[x].add(click[x]);
        click[x].addActionListener(this);
        click[x].setVisible(false);
    }

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

相关问题 Java spring / hibernate 问题:查询给了我一个错误,但我不知道为什么 - Java spring / hibernate question: Query is giving me an error but I have no idea why java-为什么此for循环会给出nullpointer异常? - java - Why is this for-loop giving a nullpointer exception? 由于某种原因,这崩溃了,我不知道为什么 - For some reason this is crashing and I have no idea why JavaFX应用程序冻结,我不知道为什么 - JavaFX Application Freezes, and I have no idea why 为什么 Intellij idea 在 if 条件下警告空指针? - Why does Intellij idea warn about nullpointer in that if condition? 行-&gt; JSONArray jArray = new JSONArray(result); 给出nullpointer异常 - Line -> JSONArray jArray=new JSONArray(result); giving nullpointer exception 我不断收到ArrayIndexOutOfBoundsException,我不知道为什么 - I keep getting ArrayIndexOutOfBoundsException and I have no idea why 程序不会显示图片,我也不知道为什么 - Program won't display pictures and I have no idea why 我的PrintList函数的ArrayIndexOutOfBoundsException我不知道为什么 - ArrayIndexOutOfBoundsException for my PrintList function and I have no idea why 为什么当我使用Java反射时IDEA会出错,但是Eclipse没有 - why IDEA have a error when I use java reflection, but eclipse not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM