简体   繁体   中英

HashMap.put causing an infinite loop in Java

So I have a class named MainControl that is ran from another class (The main one) that I am certain only runs once. Inside of MainControl I have a few things that have to be loaded, one of which being a function that populates the HashMap with the key set to the keybind (int) and the values set to a class that holds the information of the specific keybinds function (KeyDetails).

So to populate the hashmap it goes through 2 loops, the first being to loop through the list of functions, the second to check if the key should be bound to the function. If the second loop finds that it should be bound it will run Keybinds.put(KeyCode, new Details(Function, KeyCode, KeyName, false); (Just ignore the false).

For some reason it ends up forcing MainControl(); to run again once it reached Keybinds.put... for no reason at all. There are no functions that should cause MainControl to run and it works when I remove the Keybinds.put line. Just by removing THAT single line it works.

public MainControl()
{   
    System.out.println("Starting System");
    LoadSession("Default");
    System.out.println("Ended System - Never Reached");
}

public static void LoadSession(String s)
{
    Keybinds = new HashMap();

    for (int i = 0; i < FunctionStringList.length; i++)
    {
        String Key = "";
        int KeyVal = 0;

        try
        {                           
            for (int a = 0; a < KeyBindingList.length; a++)
            {
                if (KeyBindingList[a].KeyName.equalsIgnoreCase(FunctionStringList[i]))
                {
                    Key = KeyBindingList[a].KeyName
                    KeyVal = KeyBindingList[a].KeyCode
                }
            }            


            Keybinds.put(KeyVal, new Details(FunctionStringList[i], KeyVal, Key, false));

            System.out.println("Key: " + Key + " Val: " + KeyVal + " Hack: " + FunctionStringList[i]);      
        }
        catch (Exception E) { E.printStackTrace(); }        
    }
}

public static String FunctionStringList[] =
{
    "Forward", "Backwards", "StrafeLeft", "StrafeRight", "Jump", "Sneak"
};

Details Class:

public class Details extends MainControl
{
public Details(String Name, int KeyCode, String KeyName2, boolean Bool)
{       
    FunctionName = Name;
    Code = KeyCode;
    KeyName = KeyName2 != null ? KeyName2 : "None";
    State = Bool;
}

public boolean Toggle()
{
    State = !State;
    return State;
}

public void SendChat(String s)
{
    Console.AddChat(s);
}

public String FunctionName;
public String KeyName;
public int Code;
public boolean State;
}

Your Details class is-a MainControl ; it's a subclass.

When you extend a class, the child class' constructor is calling the parent object's no-arg constructor which is causing an infinite recursion.

Edit to add from the comment below: Your "offending line" is:

Keybinds.put(KeyVal, new Details(FunctionStringList[i], KeyVal, Key, false));

When the Details constructor executes, it then calls MainControl() ... which then calls LoadSession() ... which then creates a new Details ... which then calls MainControl() .. etc, etc. Infinite recursion until you get a Stack Overflow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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