簡體   English   中英

如何修復“ main”線程中的“ java.lang.NullPointerException”異常

[英]How to fix “Exception in thread ”main“ java.lang.NullPointerException”

我正在編寫一個Java程序,將其弄亂,試圖制作一個新面板,並以某種方式將其弄亂了,並且不允許我運行該程序。 我回滾到它可以正常工作的最后一點,但仍然不好。 我不知道哪里出了問題。 從錯誤中,我只能假定我在main方法和makeGUI方法之間的某個地方弄糟了您,但看不到任何錯誤。

我得到的錯誤是;

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

是因為您分配

mousePanel.add(mouseLabel,BorderLayout.SOUTH);

在初始化mouseLabel之前。

mouseLabel = new JLabel();

當mouseLabel尚未初始化時,您正在將mouseLabel添加到mousePanel。 這就是為什么您有一個nullPointerException的原因。

相反,您應該這樣做

public void buildGUI() {



mousePanel = new JPanel();
mousePanel.setLayout(new BorderLayout());
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");

mousePanel.add(mouseLabel,BorderLayout.SOUTH);/*Add The mousePanel Object after it has being initialized*/

actionsPanel = new JPanel();
actionsPanel.setLayout(new GridLayout(3,3));
}

我希望這對兄弟有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM