所以我有一些存儲在MySQL數據庫中的朋友系統的數據,以及一些API來檢索Java對象中的數據。 (MPlayer)包含諸如用戶名,在線狀態,朋友(由「:」分隔的ID)之類的東西。 MPlayer對象將玩家的唯一ID作爲構造函數。我該如何解決這個遞歸的StackOverFlowException錯誤?
創建時以及調用reload()方法時,數據保存在對象中。這不是每次我想獲得像用戶名這樣的東西時訪問數據庫。原因是我需要將數據放到一個循環中才能顯示在GUI上,而且我顯然不希望每幀都下載數據。相反,我只是每6秒左右使用重載方法。
其中一個函數是getFriends()並返回MPlayer的列表。該列表在MPlayer對象創建時存儲。問題是,當每個MPlayer朋友被創建時,它會爲他們的朋友創建一個列表,然後爲他們的朋友創建一個列表,最後由於遞歸而結束StackOverFlowException。
什麼是避免錯誤的好方法?
代碼中的問題:
MPlayer的構造函數/ load方法:
public MPlayer(String player){
this.uuid = player;
try {
st.setString(1, uuid);
} catch (SQLException e) {
e.printStackTrace();
}
this.reload();
}
public void reload(){
try {
ResultSet set = st.executeQuery();
if(set.next()){
if(set.getString("server").equals("none")){
isConnected = false;
}else{
isConnected = true;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
ResultSet set = st.executeQuery();
if(set.next()){
this.serverIP = set.getString("server");
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
ResultSet set = st.executeQuery();
if(set.next()){
this.username = set.getString("username");
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
ResultSet get = st.executeQuery();
if(get.next()){
this.online = get.getBoolean("status");
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
List<MPlayer> list = new ArrayList<MPlayer>();
ResultSet get = st.executeQuery();
if(get.next()){
for(String str : get.getString("friends").split(":")){
if(!str.equalsIgnoreCase("none")){
MPlayer player = new MPlayer(str);
if(player.isOnline()){
list.add(0,player);
}else{
list.add(player);
}
}
}
}
this.friends = list;
} catch (SQLException e) {
e.printStackTrace();
}
this.settings = new Settings(this);
PreparedStatement state = Main.getPreparedStatement("SELECT * FROM updates WHERE uuid=?");
try {
state.setString(1, this.getUUID());
ResultSet set2 = state.executeQuery();
List<StatusUpdate> updates = new ArrayList<StatusUpdate>();
while(set2.next()){
updates.add(new StatusUpdate(set2.getInt(1)));
}
Collections.sort(updates, new Comparator<StatusUpdate>() {
@Override
public int compare(StatusUpdate r1, StatusUpdate r2) {
return -1 * r1.getDate().compareTo(r2.getDate());
}
});
this.updates = updates;
} catch (SQLException e) {
e.printStackTrace();
}
List<StatusUpdate> updates = new ArrayList<StatusUpdate>();
for(MPlayer p : this.getFriends()){
updates.addAll(p.getStatusUpdates());
}
updates.addAll(this.getStatusUpdates());
Collections.sort(updates, new Comparator<StatusUpdate>() {
public int compare(StatusUpdate m1, StatusUpdate m2) {
return -1 * m1.getDate().compareTo(m2.getDate());
}
});
this.timeline = updates;
}
請發佈您的代碼和確切的錯誤。 [關於SO的問題](http://stackoverflow.com/help/how-to-ask)。 – Dresden
這裏的問題是,你正在從'reload'函數中調用'MPlayer player = new MPlayer(str)',但是在'MPlayer'的構造函數中,你也調用'this.reload();'這也會調用'MPlayer播放器=新的MPlayer(str);'所以循環是無限的 - 我認爲第一步應該是重構你的代碼 - 這不是一個好主意,你的'reload'函數做的太多了...... – ishmaelMakitla