3

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;
}
9
  • 7
    Perhaps you could post some of your code, rather than just describing it in your own words? Commented Apr 28, 2012 at 23:27
  • 1
    Without showing your actual code, no one here can help you. Commented Apr 28, 2012 at 23:27
  • 5
    @Brian: Except for Jon Skeet. Commented Apr 28, 2012 at 23:30
  • 1
    Where is the definition of the class Details? Removing the line that has HashMap.put() also removes the only line that uses the Details class. This hints that it may contain the problem. Commented Apr 28, 2012 at 23:52
  • 3
    To expand on @Grilse comment - if something is happening on that line, it's because the constructor of your Details class is causing it. My guess is ... you're instantiating another MindControl object and causing an infinite recursion. Commented Apr 28, 2012 at 23:54

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.