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;
}
André
  • 343
  • 2
  • 7
  • 15
  • 7
    Perhaps you could post some of your code, rather than just describing it in your own words? – Mark Byers Apr 28 '12 at 23:27
  • 1
    Without showing your *actual* code, no one here can help you. – Brian Roach Apr 28 '12 at 23:27
  • 5
    @Brian: Except for Jon Skeet. – Niklas B. Apr 28 '12 at 23:30
  • Your question aside, you should pay attention to your Java Naming Conventions. We're pretty stubborn on that in the Java world. `CONSTANTS`, `ClassNames`, `methodNames` and `fieldAndVariableNames` should all use the right case. – adarshr Apr 28 '12 at 23:40
  • @adarshr Will start to use that now, I am self tought so I have never learnt to do this. – André Apr 28 '12 at 23:44
  • 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. – Grilse Apr 28 '12 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. – Brian Roach Apr 28 '12 at 23:54
  • @BrianRoach Update with the Details class – André Apr 29 '12 at 00:00
  • My bet will be that the bug is elsewhere. Not in the code that you posted in here, but in the ones that uses them. "For some reason it ends up forcing MainControl(); to run again once it reached Keybinds.put..." is a clue for me. Is the application multithreaded, and some other process(es) monitoring Keybinds? – bungrudi Apr 29 '12 at 00:09

1 Answers1

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.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161