简体   繁体   English

在数组中创建菜单对象

[英]Creating objects in an array for a menu

I'm trying to create a registration menu but I'm having trouble creating the objects in an accessible array. 我正在尝试创建注册菜单,但是在可访问数组中创建对象时遇到了麻烦。 Super class constructor: 超类构造函数:

Login(String fn, String ln, String acc, String by, String pw){
    firstName = fn;
    lastName = ln;
    account = acc;
    birthYear = by;
    password = pw;
}

In the super class I created an array of type Login . 在超类中,我创建了一个类型为Login的数组。 In the child class Register , I'm trying to create objects in the array. 在子类Register ,我试图在数组中创建对象。 My registration menu has several fields to be filled in. I want an error message to pop up when the fields are empty and when the password fields do not match. 我的注册菜单中有几个字段需要填写。当这些字段为空并且密码字段不匹配时,我希望弹出一条错误消息。

JButton btnRegister = new JButton("Register");
btnRegister.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        boolean match = false;
        while (match == false){
        if (textField_4.getText().equals(textField_5.getText())){
        try{
            for (int i = count; i<= list.length;i++){
            list[i] = new Login(textField.getText(),textField_1.getText(),textField_2.getText(),textField_3.getText(),textField_4.getText());
            count++;
            match = true;
            }}
            catch (Exception e){
                JOptionPane.showMessageDialog(null, "Fields are empty", "Error", JOptionPane.INFORMATION_MESSAGE);
                System.out.println(e);
                break;
            }
        }
        else
            match = false;
            JOptionPane.showMessageDialog(null, "Password fields do not match!", "Error", JOptionPane.INFORMATION_MESSAGE);
            break;
        }
    }
});

I have gotten the password mismatch feature to work but I currently get a ArrayIndexOutOfBounds thrown when I try to enter all of the fields and create an object. 我已经使用了密码不匹配功能,但是当我尝试输入所有字段并创建对象时,当前抛出了ArrayIndexOutOfBounds How do I resolve this issue? 我该如何解决这个问题? Thank you in advance. 先感谢您。

The culprit is most likely these two lines 罪魁祸首很可能是这两条线

    for (int i = count; i<= list.length;i++){
    list[i] = ...

Trying to access " list[list.length] " will give you an ArrayIndexOutOfBounds , since indeces start at 0, so list 's last index is list.length-1 ; 尝试访问“ list [list.length] ”会给您一个ArrayIndexOutOfBounds ,因为indes的起始位置是0,所以list的最后一个索引是list.length-1 ;

Change from " i<= list.length " to " i < list.length ". 从“ i <= list.length ”更改为“ i <list.length ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM