我正在寫一個Java程序,試圖使一個新的面板搞砸了,並以某種方式搞砸了,不會讓我運行程序。我回到了工作的最後一點,但仍然不好。我不知道我錯在哪裏。從錯誤我只能假設我迷失在主要方法和makeGUI方法之間的某處,但我看不到任何錯誤。如何解決「在線程中的異常」主要「java.lang.NullPointerException」
我得到的錯誤是;
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl<Container.java:1090>
at java.awt.Container.add<Container.java:966>
at assignment2g2.buildGUI<assignment2g2.java:47>
at assignment2g2.<init><assignment2g2.java:31>
at assignment2g2.main<assignment2g2.java:124>
和我的代碼...
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.event.MouseInputListener;
import java.awt.*;
import java.awt.event.*;
public class assignment2g2 extends JFrame implements MouseInputListener {
JPanel mousePanel, actionsPanel;
JLabel mouseLabel;
JLabel topLeft,topMid,topRightMid,topRight,midLeft,midMid,midRightMid,midRight,btmLeft,btmMid,btmRightMid,btmRight;
ImageIcon circle = new ImageIcon("circle.jpg");
ImageIcon square = new ImageIcon("square.jpg");
ImageIcon triangle = new ImageIcon("triangle.jpg");
//JButton button1=new JButton("?");
//JButton button2=new JButton("?");
public assignment2g2() {
buildGUI();
buildActionsPanel();
mousePanel.add(actionsPanel);
getContentPane().add(mousePanel);
setSize(800,400);
setVisible(true);
}
public void buildGUI() {
mousePanel = new JPanel();
mousePanel.setLayout(new BorderLayout());
mousePanel.add(mouseLabel,BorderLayout.SOUTH);
mousePanel.addMouseListener(this);
mousePanel.addMouseMotionListener(this);
mouseLabel = new JLabel();
mouseLabel.setBorder(new LineBorder(Color.BLACK));
mouseLabel.setForeground(Color.RED);
mouseLabel.setText("Please select an answer");
actionsPanel = new JPanel();
actionsPanel.setLayout(new GridLayout(3,3));
}
public void buildActionsPanel() {
topLeft = new JLabel();
topMid = new JLabel();
//topMid.setIcon(circle);
topMid.setText("circle");
topRightMid = new JLabel();
topRight = new JLabel();
midLeft = new JLabel();
midLeft.setBorder(new LineBorder(Color.BLACK));
midLeft.addMouseListener(this);
midLeft.setText("Square");
midMid = new JLabel();
midMid.setBorder(new LineBorder(Color.BLACK));
midMid.addMouseListener(this);
midMid.setText("Triangle");
/*midRightMid = new JLabel();
midRightMid.setBorder(new LineBorder(Color.BLACK));
midRightMid.addMouseListener(this);
midRightMid.setText("Circle");*/
midRight = new JLabel();
midRight.setBorder(new LineBorder(Color.BLACK));
midRight.addMouseListener(this);
midRight.setText("Circle");
btmLeft = new JLabel();
btmMid = new JLabel();
btmRightMid = new JLabel();
btmRight= new JLabel();
actionsPanel.add(topLeft);
actionsPanel.add(topMid);
//actionsPanel.add(topRightMid);
actionsPanel.add(topRight);
actionsPanel.add(midLeft);
actionsPanel.add(midMid);
//actionsPanel.add(midRightMid);
actionsPanel.add(midRight);
actionsPanel.add(btmLeft);
actionsPanel.add(btmMid);
//actionsPanel.add(btmRightMid);
actionsPanel.add(btmRight);
}
public static void main(String[] args) {
new assignment2g2();
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
mouseLabel.setText("Mouse Pressed Event");
//if(e.getSource() instanceof JLabel) {
// JOptionPane.showMessageDialog(null, "Incorrect", "Incorrect", JOptionPane.INFORMATION_MESSAGE);
//}
if(e.getSource()==midRight) {
if((topMid.getText()).matches("circle")){
JOptionPane.showMessageDialog(null, "Correct", "Correct", JOptionPane.INFORMATION_MESSAGE);
//topMid.setIcon(square);
topMid.setText("square");
}
else{
JOptionPane.showMessageDialog(null, "Incorrect", "Incorrect", JOptionPane.INFORMATION_MESSAGE);
}
}
if(e.getSource()==midMid) {
if((topMid.getText()).matches("triangle")){
JOptionPane.showMessageDialog(null, "Correct", "Correct", JOptionPane.INFORMATION_MESSAGE);
//topMid.setIcon(square);
topMid.setText("circle");
}
else{
JOptionPane.showMessageDialog(null, "Incorrect", "Incorrect", JOptionPane.INFORMATION_MESSAGE);
}
}
if(e.getSource()==midLeft) {
if((topMid.getText()).matches("square")){
JOptionPane.showMessageDialog(null, "Correct", "Correct", JOptionPane.INFORMATION_MESSAGE);
//topMid.setIcon(square);
topMid.setText("triangle");
}
else{
JOptionPane.showMessageDialog(null, "Incorrect", "Incorrect", JOptionPane.INFORMATION_MESSAGE);
}
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
}
如果有人可以幫助我,這將是巨大的。 對不起事先,我知道代碼是有點亂的:L
不,他們是不同的東西。我之前有過它,它運行正常。 – 2014-12-07 00:17:08
您在'buildGUI()'方法中向您的容器('mousePanel')添加'null'組件('mouseLabel') – jhamon 2014-12-07 00:17:43
仔細查看堆棧跟蹤。它告訴你在'assign2g2.java'的第47行代碼中發生了異常。轉到那行代碼,找出那裏不應該是'null'的'null'。 – Jesper 2014-12-07 00:17:48