mouseEntered(MouseEvent e)中线程“AWT-EventQueue-0”java.lang.NullPointerException中的异常

时间:2014-03-09 16:34:50

标签: java swing nullpointerexception jbutton mouselistener

这基本上创建了一个按钮网格布局,设计为使用多个类的体育场。我在另一个类中使用这个类,它创建了与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)

这是我的班级:

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

1 个答案:

答案 0 :(得分:1)

在构造函数中,您只使用局部变量。您永远不会使用或填写课程中的Field Seat[][] seatButtons字段。比你尝试在鼠标事件中使用它但它是“null”。