gpt4 book ai didi

java - mouseEntered(MouseEvent e) 上线程 "AWT-EventQueue-0"java.lang.NullPointerException 中出现异常

转载 作者:行者123 更新时间:2023-12-01 13:18:15 25 4
gpt4 key购买 nike

这基本上创建了一个按钮的网格布局,设计为使用多个类的体育场。我在另一个类中使用此类,该类创建相同的面板以及 jtextfields 来显示价格、行、部分等。每当用户将鼠标悬停在按钮上时,它就会用有关该座位的信息填充 jtextfields。但是,一旦鼠标进入任何一个按钮,每次输入新按钮时我都会收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at StadiumPanel.mouseEntered(StadiumPanel.java:72)
at java.awt.AWTEventMulticaster.mouseEntered(AWTEventMulticaster.java:300)
at java.awt.Component.processMouseEvent(Component.java:6514)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.trackMouseEnterExit(Container.java:4620)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4474)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
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)

这是我的类(class):

import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.awt.event.*;
import javax.swing.border.LineBorder;

public class StadiumPanel extends BoardPanel implements MouseListener {

public Seat[][] seatButtons;
SeatInfo sI;
public int priceFinal;
public int secFinal;
public int rowFinal;
public int numFinal;

public StadiumPanel(Stadium s) {

Seat[][] seatButtons = s.getSeats();

setLayout(new GridLayout(27,35));
setBorder(new LineBorder(Color.BLACK, 1));
setBackground(Color.WHITE);

for (int r=0; r<27; r++) {
for (int c=0; c<35; c++) {
if (seatButtons[r][c] != null) {
if (seatButtons[r][c].getSection() == 1) {
JButton b = new JButton();
b.setBackground(Color.red);
b.addMouseListener(this);
add(b);
}
else if (seatButtons[r][c].getSection() == 2) {
JButton b = new JButton();
b.setBackground(Color.green);
b.addMouseListener(this);
add(b);
}
else if (seatButtons[r][c].getSection() == 3) {
JButton b = new JButton();
b.setBackground(Color.blue);
b.addMouseListener(this);
add(b);
}
else if (seatButtons[r][c].getSection() == 4) {
JButton b = new JButton();
b.setBackground(Color.yellow);
b.addMouseListener(this);
add(b);
}
}
else if (seatButtons[r][c] == null) {
JLabel b = new JLabel();
b.setBackground(Color.white);
add(b);
}
}
}
}
public void mousePressed(MouseEvent e) {

}

public void mouseReleased(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {
for (int r=0; r<27; r++) {
for (int c=0; c<35; c++) {
if ((e.getSource() == seatButtons[r][c]) && (seatButtons[r][c] != null)) {
priceFinal = seatButtons[r][c].getPrice();
secFinal = seatButtons[r][c].getSection();
rowFinal = seatButtons[r][c].getRow();
numFinal = seatButtons[r][c].getNumber();
sI.setPrice(String.valueOf(priceFinal));
sI.setRow(String.valueOf(rowFinal));
sI.setSec(String.valueOf(secFinal));
sI.setNum(String.valueOf(numFinal));
sI.updateText();
}
}
}
}

public void mouseExited(MouseEvent e) {

}

public void mouseClicked(MouseEvent e) {

}
public static void main(String args[]) {
JFrame f = new JFrame("Stadium Panel Test");
f.getContentPane().add(new StadiumPanel(new Stadium()));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(649, 500);
f.setVisible(true);
}
}

最佳答案

在构造函数中,您仅使用局部变量。您永远不会使用或填充类中的字段 Field Seat[][] SeatButtons 。您尝试在鼠标事件中使用它,但它是“null”。

关于java - mouseEntered(MouseEvent e) 上线程 "AWT-EventQueue-0"java.lang.NullPointerException 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22284846/

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