开发者

Why is HashMap resetting after it exits method?

开发者 https://www.devze.com 2023-02-26 16:43 出处:网络
I am having some trouble with the HashMaps in my program.I have several declared at the top class like so:

I am having some trouble with the HashMaps in my program. I have several declared at the top class like so:

  public HashMap<String, Object> hmClass = new HashMap<String ,Object>();
        public HashMap<String, Object> hmIns = new HashMap<String, Ob开发者_JS百科ject>();
        public HashMap<String, Object> hmCon = new HashMap<String, Object>();
        public HashMap<String, Object> hmParam = new HashMap<String, Object>();
        public HashMap<String, Object> hmResult = new HashMap<String, Object>();

In a separate method, I am putting values into them like this:

public String newInstanceCreate(Class c3, String classInstance)//void ??
    {
        Class c = c3;
        String cI = classInstance;
        Object instance = null;
        Object con = null;
        String instanceName = null;

        try{
         instanceName = JOptionPane.showInputDialog(null, "Under what name would you like to store this Instance?", 
                                                "Create an Instance of a Class", JOptionPane.QUESTION_MESSAGE);
        }catch(NullPointerException e)
        {
            newInstanceCreate(c,cI);
        }

        hmClass.put(instanceName, c);  //add the name of the instance as key and class as value to the hashmap 
        System.out.println(hmClass);    

            con = JOptionPane.showInputDialog(null,"Choose Constructor for " + c, "Create an Instance of a Class", 3, null, getConstructors(c), JOptionPane.QUESTION_MESSAGE);


        System.out.println("worked + " + con);
        hmCon.put(instanceName, con);   //add the name of the instance as key and constructor as value to the hashmap
        System.out.println(hmCon);


        try {
            instance = c3.getDeclaredConstructor().newInstance(); //create the instance --KEY
            hmIns.put(instanceName, instance);
            System.out.println("23" + instance);
            hmParam.put(instanceName, getParams(c3, con));  //ask the user for the parameters (doubles) that they want in the instance constructor
                                                            // add the name of the instance as key and parameters(array) as value           

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e){
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        System.out.print("hmClass: \n" + hmClass + "hmIns: \n" + hmIns + "hmCon: \n" + hmCon + "hmParam: \n" + hmParam);
        return classInstance;
    }

But when I try access them from a another method, it seems that they have all been reset.

public void option4()
    {
        System.out.println("hmIns: " + hmIns);
        String stringInstance = JOptionPane.showInputDialog(null, "Which Instance would you like to stringify? " ,
                                            "Stringify an Instance of a Class", JOptionPane.QUESTION_MESSAGE);


        //get all the required info for making the instance
        /*
         * needed: name of stored instance
         *         name of correct and stored constructor 
         *         name of parameters that were created by user (doubles?)
         */
        System.out.println(stringInstance);
        if(hmIns.isEmpty())
            System.out.println("EMPTY");
        Object ins = hmIns.get(stringInstance);
        System.out.println("ins before: " + ins);
        Object cons = hmCon.get(stringInstance);
        System.out.println("cons before: " + cons);
        Object par = hmParam.get(stringInstance);
        System.out.println("par before: " + par);

        //construct an instance and run toString() here
        try {
            ins = ((Constructor)cons).newInstance(par);
            System.out.println(" ins: " + ins);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //result from toString
        /*
         * stored: name of result
         *         result itself (stored as string for now)
         */

        JOptionPane.showMessageDialog(null, "result here??--how to get result to double??", "Stringified Instance of: ", 0);

        mainWindow();
    }

In the last method listed, I tested to see if the HashMap had null values instead of actually being empty, but it returns as having no values at all. Why is the HashMap resetting after I exit the first method?

Thank you so much for any insight.

Fran


It doesn't reset. I suggest you look at your code in a debugger and check if they are looking at the same object/map. I suspect you have two objects which contain maps.

0

精彩评论

暂无评论...
验证码 换一张
取 消