簡體   English   中英

線程“main”中的異常java.lang.IndexOutOfBoundsException:索引:5,大小:0

[英]Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 0

我只是有這個錯誤。 我有這個

private List<String> neighbourNames;

其中包含網絡中對等點的名稱。 當一個對等點嘗試離開網絡時,嘗試將自己從其他對等點的列表中刪除。 這發生了,但最后我得到了一個錯誤。 這是代碼

for(String s : neighbourNames){
            try {
                stub = (PeerInterface) registry.lookup(s);
            } catch (NotBoundException e) {
                System.out.println("Cannot reach " + s + ", probably is offline");
            }
            stub.removeMeFromNetwork(nome,key);

        }

錯誤是標題中顯示的錯誤,並且...

at com.sun.proxy.$Proxy0.removeMeFromNetwork(Unknown Source)
    at com.server.PeerNode.leaveNetwork(PeerNode.java:181)
    at com.server.PeerNode.printMenu(PeerNode.java:475)
    at com.server.PeerNode.run(PeerNode.java:118)
    at com.server.Main.main(Main.java:85)

拋出該錯誤的行是:stub.removeMeFromNetwork(nome,key);

這是 removeMeFromNetwork 的實現:

public void removeMeFromNetwork(String nome, int ke) throws RemoteException {
        System.out.println(nome + " is leaving the network");

        neighbourNames.remove(nome);
        listKey.remove(ke);
        System.out.println("QUALCUNO E' USCITO VICINI RIMANENTI\n"+ neighbourNames);

    }

提前致謝

接口List有兩種remove方法,一種接受對象( List.remove(Object) )和一種接受索引號( List.remove(int) )。 當你有一個List<Integer>你必須小心調用正確的。

即,在您的代碼中,您有一個參數int ke並調用listKey.remove(ke); 這將調用List.remove(int)並將ke解釋為索引而不是元素。 您可以通過插入類型listKey.remove((Integer)ke);來刪除Integer對象listKey.remove((Integer)ke);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM