-2

我已经为此苦苦挣扎了一段时间。我正在尝试使用所有者对象更新一个空的 JCombobox,只要通过单击按钮创建它,但我不断收到 nullpointerexception

        package gui;

    import java.awt.BorderLayout;
    import java.awt.EventQueue;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JMenuBar;
    import javax.swing.JToolBar;
    import javax.swing.JTabbedPane;
    import javax.swing.JDesktopPane;
    import javax.swing.border.TitledBorder;
    import javax.swing.border.LineBorder;
    import java.awt.Color;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import java.awt.Insets;
    import java.awt.Button;
    import java.awt.Component;
    import javax.swing.JLayeredPane;
    import javax.swing.JTable;
    import javax.swing.Box;
    import javax.swing.JSplitPane;
    import javax.swing.AbstractAction;
    import java.awt.event.ActionEvent;
    import javax.swing.Action;
    import javax.swing.JInternalFrame;
    import java.awt.event.*; 
    import controller.ForestOwnerCtr;


    import model.OwnerCon;
    import model.Owner;
    import model.Tree;

    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import javax.swing.JList;
    import javax.swing.JComboBox;
    import java.awt.Choice;
    import java.awt.Label;
    import javax.swing.DefaultComboBoxModel;

    public class ForestGui extends JFrame {

        private static ForestOwnerCtr FOCtr;
        private JPanel contentPane;
        private JTextField txtOwnerName;
        private JTextField txtOwnerAdress;
        private JTextField txtOwnerPhone;
        private JTextField txtName;
        private JTextField txtLocation;
        private JTextField txtArea;
        private JTextField textField;
        private JTextField textField_3;
        private JTextField textField_4;
        private JTextField txtAge;
        private JComboBox<Owner> owner_select;
        private ArrayList<Owner> test;

        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        ForestGui frame = new ForestGui();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            FOCtr = new ForestOwnerCtr();
        }

        /**
         * Create the frame.
         */
        public ForestGui() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 946, 509);

            JMenuBar menuBar = new JMenuBar();
            setJMenuBar(menuBar);

            JButton btnCreate = new JButton("Create");
            menuBar.add(btnCreate);

            JButton btnSearch_1 = new JButton("Search");
            menuBar.add(btnSearch_1);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);

            JPanel panel_search_forest = new JPanel();
            panel_search_forest.setBorder(new TitledBorder(new LineBorder(new Color(130, 135, 144)), "Seach forest", TitledBorder.LEADING, TitledBorder.TOP, null, null));
            panel_search_forest.setToolTipText("tst");
            panel_search_forest.setBounds(15, 11, 238, 238);
            contentPane.add(panel_search_forest);
            panel_search_forest.setLayout(null);

            JLabel lblName_1 = new JLabel("Name");
            lblName_1.setBounds(10, 28, 46, 14);
            panel_search_forest.add(lblName_1);

            txtName = new JTextField();
            txtName.setColumns(10);
            txtName.setBounds(92, 26, 119, 19);
            panel_search_forest.add(txtName);

            JLabel lblLocation = new JLabel("Location");
            lblLocation.setBounds(10, 56, 58, 14);
            panel_search_forest.add(lblLocation);

            JLabel lblArea = new JLabel("Area");
            lblArea.setBounds(10, 81, 46, 14);
            panel_search_forest.add(lblArea);

            txtLocation = new JTextField();
            txtLocation.setColumns(10);
            txtLocation.setBounds(92, 54, 119, 19);
            panel_search_forest.add(txtLocation);

            txtArea = new JTextField();
            txtArea.setColumns(10);
            txtArea.setBounds(92, 79, 119, 19);
            panel_search_forest.add(txtArea);

            JLabel lblAge = new JLabel("Age");
            lblAge.setBounds(10, 106, 46, 14);
            panel_search_forest.add(lblAge);

            txtAge = new JTextField();
            txtAge.setColumns(10);
            txtAge.setBounds(92, 107, 119, 19);
            panel_search_forest.add(txtAge);

            JButton CreateForest = new JButton("New forest");
            CreateForest.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    createForest();
                }
            });
            CreateForest.setBounds(109, 204, 119, 23);
            panel_search_forest.add(CreateForest);

            JLabel lblOwner = new JLabel("Owner");
            lblOwner.setBounds(10, 131, 46, 14);
            panel_search_forest.add(lblOwner);

            Choice choice = new Choice();
            choice.setBounds(92, 163, 119, 19);
            panel_search_forest.add(choice);

            JLabel lblTreeSorts = new JLabel("Tree sorts");
            lblTreeSorts.setBounds(10, 163, 76, 20);
            panel_search_forest.add(lblTreeSorts);


            JComboBox<Owner> owner_select = new JComboBox<Owner>();
            owner_select.setModel(new DefaultComboBoxModel(FOCtr.getOwners().toArray()));
            owner_select.setBounds(90, 137, 121, 20);

            panel_search_forest.add(owner_select);




            JPanel searc_owner = new JPanel();
            searc_owner.setToolTipText("tst");
            searc_owner.setBorder(new TitledBorder(new LineBorder(new Color(130, 135, 144)), "Seach owner", TitledBorder.LEADING, TitledBorder.TOP, null, null));
            searc_owner.setBounds(263, 11, 250, 196);
            contentPane.add(searc_owner);
            searc_owner.setLayout(null);

            JLabel lblName = new JLabel("Name");
            lblName.setBounds(10, 22, 46, 14);
            searc_owner.add(lblName);

            JLabel lblAdress = new JLabel("Adress");
            lblAdress.setBounds(10, 47, 46, 14);
            searc_owner.add(lblAdress);

            JLabel lblPhoneNumber = new JLabel("Phone number");
            lblPhoneNumber.setBounds(10, 77, 83, 14);
            searc_owner.add(lblPhoneNumber);

            txtOwnerName = new JTextField();
            txtOwnerName.setBounds(111, 20, 119, 17);
            searc_owner.add(txtOwnerName);
            txtOwnerName.setColumns(10);

            txtOwnerAdress = new JTextField();
            txtOwnerAdress.setColumns(10);
            txtOwnerAdress.setBounds(111, 45, 119, 17);
            searc_owner.add(txtOwnerAdress);

            txtOwnerPhone = new JTextField();
            txtOwnerPhone.setColumns(10);
            txtOwnerPhone.setBounds(111, 75, 119, 17);
            searc_owner.add(txtOwnerPhone);

            JButton btnSearch = new JButton("New Owner");
            btnSearch.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent E) {
                    createOwner();
                }
            });



            btnSearch.setBounds(141, 162, 89, 23);
            searc_owner.add(btnSearch);



            JPanel panel = new JPanel();
            panel.setLayout(null);
            panel.setToolTipText("tst");
            panel.setBorder(new TitledBorder(new LineBorder(new Color(130, 135, 144)), "Seach forest", TitledBorder.LEADING, TitledBorder.TOP, null, null));
            panel.setBounds(585, 171, 238, 196);
            contentPane.add(panel);

            JLabel label = new JLabel("Name");
            label.setBounds(10, 28, 46, 14);
            panel.add(label);

            textField = new JTextField();
            textField.setColumns(10);
            textField.setBounds(78, 25, 119, 17);
            panel.add(textField);

            JLabel label_1 = new JLabel("Location");
            label_1.setBounds(10, 56, 58, 14);
            panel.add(label_1);

            JLabel label_2 = new JLabel("Area");
            label_2.setBounds(10, 81, 46, 14);
            panel.add(label_2);

            textField_3 = new JTextField();
            textField_3.setColumns(10);
            textField_3.setBounds(78, 53, 119, 17);
            panel.add(textField_3);

            textField_4 = new JTextField();
            textField_4.setColumns(10);
            textField_4.setBounds(78, 78, 119, 17);
            panel.add(textField_4);

            JButton button_1 = new JButton("Search");
            button_1.setBounds(139, 162, 89, 23);
            panel.add(button_1);


            }






    private void createForest(){

        String name = txtName.getText();
        String location = txtLocation.getText();
        int totalArea = Integer.parseInt(txtArea.getText());
        int age = Integer.parseInt(txtAge.getText());
        //Owner owner = 
        FOCtr.createForest(name, location, totalArea, age);
        }

   private void createOwner(){


        String newName = txtOwnerName.getText();
        String newAdress = txtOwnerAdress.getText();
        String newPhone = txtOwnerPhone.getText();
        Owner o = new Owner(newName, newAdress, newPhone);
        test.add(o);
        owner_select.addItem(o);
        }

    }

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at gui.ForestGui.createOwner(ForestGui.java:285)
    at gui.ForestGui.access$1(ForestGui.java:278)
    at gui.ForestGui$3.actionPerformed(ForestGui.java:211)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
4

2 回答 2

2

您定义一个为空的类变量:

private JComboBox<Owner> owner_select;

然后在构造函数中创建一个不为 null 的局部变量:

JComboBox<Owner> owner_select = new JComboBox<Owner>();

createOwner() 方法中的代码只能访问类变量。您不想创建局部变量,因此构造函数中的代码应为:

owner_select = new JComboBox<Owner>();
于 2013-02-24T21:42:26.147 回答
2
I have been struggling with this for a time

解决此问题不应超过五分钟。

NPE 是最容易解决的问题。打印堆栈跟踪,记下它发生的类和行号,并扫描该行以查找未通过将它们设置为非空引用来初始化的引用。如果不明显,请使用带有调试器的 IDE。

只是不要犯重复相同行为并期待不同结果的错误。除非它是导致您的问题的竞争条件,否则这是精神错乱的一个迹象。

FWIW,你写的代码太多了。这就是我讨厌 Swing 的原因之一——代码太多!稍加思考和分解将大有帮助。

例如,地球上没有理由让您的课程扩展JFrame. JPanel会是更好的选择。您可以将您的课程嵌入到 a 中JFrame,然后离开。

textField_4? 那是什么?谁能说出那是为了什么?神秘的人甚至可能是三周后的你。

于 2013-02-24T20:59:24.937 回答