java - 为什么 ArrayList 保持为空,但将另一个类中的对象添加到列表中却可以工作?

标签 java arraylist indexoutofboundsexception

每当我尝试使用 MainApp 类中的 userNames 列表时,都会收到 IndexOutOfBoundsException。但是,我确信这些名称已从 ServerController 类添加到列表中,因此我不明白为什么在另一个类中调用时列表为空。

我想这可能是在检查SO上的类似问题时出现的实例化问题,我已经尝试解决这个问题很长一段时间了,但我根本无法这样做。我非常感谢任何建议,TIA。

这是我的用户类,其唯一目的是存储用户数据,即名称和 ID。

public class User {

public User() {
 }

public ArrayList<String> userNames = new ArrayList<>();

public ArrayList<String> getUserNames() {
    return this.userNames;
  }
}

这是我的服务器类:将名称添加到 userNames 列表中工作正常。

public class ServerController {

  private final Server server;
  private final User user;
  private final GameConfig gameConfig;

  public ServerController(GameConfig gameConfig) {
    this.gameConfig = gameConfig;
    this.server = new Server();
    this.user = new User();
  }

  public User getUser() {
      return user;
  }
  private class ServerListener extends Listener {
   //this is where I add the names to the userNames list
    @Override
    public void received(Connection connection, Object obj) {
        server.sendToAllExceptTCP(connection.getID(), obj);
        if (obj instanceof String) {
            final String jp = (String) obj;
            user.userNames.add(jp);
        }
}

这是我的 MainApp:我尝试访问存储在 userNames 中的用户名称。每当收到连接时,服务器 Controller 就会将名称添加到列表中,这工作正常,但我在 createPlayer 方法中遇到异常。

public class MainApp {
 public User user = new User();

 public Game createPlayer(){

 if (("Mitspieler".equals(client1)) && ("Mitspieler".equals(client2))   && ("Mitspieler".equals(bot1))) {
        for (int i = 0; i < clients.length; i++) {
   //this is the source of the exception
            players[i + 1] = createHumanPlayer(user.getUserNames().get(i), i, restColors[i]);
        }
 }
  //....
}

堆栈跟踪:

Exception in thread "JavaFX Application Thread"  
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at client.MainApp.createPlayer(MainApp.java:265)

最佳答案

您创建两个实例。

public class MainApp {
    public User user = new User(); // Instance 1
}

public class ServerController {
    private User user;

    public ServerController(GameConfig gameConfig) {
        this.user = new User(); // Instance 2
        ...
    }
}

实例 1 和实例 2 不共享任何数据。实例 1 保存您添加的名称,实例 2 不保存。

如果您不打算拥有多个 Controller 实例,您可以执行类似的操作

public class User {
    private static User USER;

    public static User getInstance() {
        if(null == USER) {
            USER = new User();
        }
        return USER;
    }
}

public class MainApp {
    public Game createPlayer() {
        User user = User.getInstance();
        List<String> userNames = user.getUserNames();
    }
}

User 将是 Singleton 。但请注意:如果您有多个 ServerController 实例,所有实例都将使用相同的用户列表。如果这不是您想要的,您需要使用相同的 User 实例初始化 ServerControllerMainApp

关于java - 为什么 ArrayList 保持为空,但将另一个类中的对象添加到列表中却可以工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42508269/

相关文章:

java - 尽管使用 indexInBounds 仍获取 ArrayIndexOutOfBounds

java - 刷新jsp页面时再次提交

java - 如何访问 App Engine 应用程序存储的文件

java - 参数化 JUnit+JUnitParams 测试不适用于具有

angular - 如何从 typescript 中的对象中删除除某些属性之外的所有属性

java - 更改自定义 JFileChooser 的外观会引发异常

java - gdx 将布局文件解析为目标文件

java - 在将相同的元素添加到新的 ArrayList 后,有什么方法可以从给定的 ArrayList 中删除某些元素吗?

mysql - 使用 vb.net 查找数组中与给定时间最接近的时间

Java StringTokenizer 超出 .CSV 文件范围