GUI中的私有实例变量“未初始化”(java)

时间:2013-11-29 00:00:45

标签: java swing

我正在编写一个扩展JFrame并实现ActionListener的类。 我有一个私有int“currentSize”。它在构造函数中初始化。有些方法由actionPerformed调用,使用“currentSize”。当我尝试编译时,它说“变量类型可能尚未初始化”,突出显示currentSize的用法。我理解为什么会出现这种错误,但我该如何解决呢?

class GUIinterface extends JFrame implements ActionListener
{
    ...
    private int currentSize;
    ...

    public GUIinterface()
    {
        super("Contact database");

        contact = new Contact[1000];
        currentSize = 0;
        currentState = STATE_NOTHING;

        setSize(600, 400);
        createMenu();

        setVisible(true);
    }

    private void createParseEmail()
    {
        Component[] component = formCreate.getComponents();
        String value;

        long customerID;
        int type;
        String email;
        String user;
        String domain;

        // ID
        value = ((JTextField)(component[1])).getText();
        try
        {
            customerID = Long.parseLong(value);
        }
        catch (NumberFormatException e)
        {
            formCreate.remove(17);
            JLabel label = new JLabel(" Customer ID is a long integer");
            formCreate.add(label, 17);
            return;
        }

        // type
        value = ((JTextField)(component[3])).getText();
        try
        {
            customerID = Integer.parseInt(value);
        }
        catch (NumberFormatException e)
        {
            formCreate.remove(17);
            JLabel label = new JLabel(" Contact type is an integer");
            formCreate.add(label, 17);
            return;
        }

        // email
        email = ((JTextField)(component[5])).getText();
        if (email.indexOf("@") == -1 || email.indexOf(".") == -1)
        {
            formCreate.remove(17);
            JLabel label = new JLabel(" Valid email required");
            formCreate.add(label, 17);
            return;
        }
        user = email.split("@")[0];
        domain = email.split("@")[1];
        if (user.length() < 1 || domain.length() < 1)
        {
            formCreate.remove(17);
            JLabel label = new JLabel(" Valid email required");
            formCreate.add(label, 17);
            return;
        }

        contact[currentSize] = new EmailContact(customerID, user, domain, type);
        currentSize++; // causes error; am not allowed to use dynamic arrays
    }

    public void actionPerformed(ActionEvent e)
    {
        String command = e.getActionCommand();
        if (e.getSource() instanceof JMenuItem)
        {
            if (command.equals("CREATE_CONTACT"))
            {
                currentState = STATE_CREATE;
                setupCreate();
            }
            if (command.equals("ERASE_CONTACT"))
            {
                currentState = STATE_ERASE;
            }
            if (command.equals("DISPLAY_CONTACTS"))
            {
                currentState = STATE_DISPLAY;
            }
            if (command.equals("FIND_CONTACT"))
            {
                currentState = STATE_FIND;
            }
        } // end of MenuItem

        if (e.getSource() instanceof JButton)
        {
            if (currentState == STATE_CREATE)
            {
                if (command.equals("CREATE_TELEPHONE"))
                {
                    setupCreateTelephone();
                }
                else if (command.equals("CREATE_EMAIL"))
                {
                    setupCreateEmail();
                }
                else if (command.equals("CREATE_POSTAL"))
                {
                    setupCreatePostal();
                }
                else if (command.equals("CREATE_TELEPHONE_REGISTER"))
                {
                    createParseTelephone();
                }
                else if (command.equals("CREATE_EMAIL_REGISTER"))
                {
                    createParseEmail();
                }
                else if (command.equals("CREATE_POSTAL_REGISTER"))
                {
                    createParsePostal();
                }
            }
        }
        revalidate();
    }
}

BTW我正在使用DrJava和Java ver。 7_45 **如果我之前的代码过于尖锐,请道歉

1 个答案:

答案 0 :(得分:2)

此行contact[currentSize] = new EmailContact(customerID, user, domain, type);使用变量type,但在它为局部变量type赋值之前没有任何行。