【问题标题】:Why does the ArrayList remain empty yet adding objects in another class to the list works?为什么 ArrayList 仍然为空,但将另一个类中的对象添加到列表中有效?
【发布时间】:2017-07-19 09:29:40
【问题描述】:

每当我尝试使用 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 中的用户名。每当收到连接正常时,ServerController 都会将名称添加到列表中,但我在 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)

【问题讨论】:

  • 看起来您的 ServerControllerMainApp 类中有 2 个不同的 User 实例
  • 在调用createPlayer之前,您的列表userNames必须填充一些数据
  • ServerControllerMainApp 中在哪里?
  • @Abubakkar 该列表填充在 ServerController 类中。
  • ServerController 在 MainApp 的任何地方都没有被调用。我的想法是从 ServerController 中添加,然后在 MainApp 中调用修改后的列表。

标签: java arraylist indexoutofboundsexception


【解决方案1】:

你创建了两个实例。

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 没有。

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

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

【讨论】:

  • 如何在 MainApp 中声明用户?公共用户用户; ?您还介意解释为什么将此方法移至 ServerController 还是错误?
  • 你是对的,ServerController 是一个错误。我还阐明了如何访问单例。请按照提供的链接了解单例的作用以及限制是什么。你为什么要创建一个类User 反正?奇怪的是它有几个用户名。是所有不同的名称还是同一用户的名称?如果它存储了所有名称,您可以将 public ArrayList&lt;String&gt; userNames = new ArrayList&lt;&gt;(); 移动到 MainApp 并删除 User
  • 感谢您的链接!不过有一些问题: 1. 当我尝试私有静态最终用户用户时;我收到我没有在默认构造函数中初始化它的错误消息,我如何在不创建另一个实例的情况下初始化它? 2.我不能从另一个类调用getInstance()方法-是因为1吗?
  • 它存储了各种用户的名字,但是我做了一个单独的类,因为我打算在不同的类中使用这个列表,而不仅仅是在 MainApp 中,这有意义吗?
  • private 更改为 public 并删除 final 即可。
【解决方案2】:

您在两个地方创建了一个用户对象。在 ServerController 中,您创建并成功地将用户名添加到数组中。然后在 MainApp 中创建一个 User 对象,它永远不会有任何用户名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-12
    • 2017-12-20
    • 2017-05-07
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多