java - 为什么HashMap在退出方法后会重置?

标签 java hashmap nullpointerexception

我的程序中的 HashMap 遇到一些问题。我在顶级类中声明了几个,如下所示:

  public HashMap<String, Object> hmClass = new HashMap<String ,Object>();
        public HashMap<String, Object> hmIns = new HashMap<String, Object>();
        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>();

在一个单独的方法中,我将值放入其中,如下所示:

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;
    }

但是当我尝试从另一种方法访问它们时,它们似乎都已被重置。

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();
    }

在列出的最后一个方法中,我测试了 HashMap 是否具有空值而不是实际为空,但它返回为根本没有值。为什么退出第一个方法后HashMap会重置?

非常感谢您的见解。

弗兰

最佳答案

它不会重置。我建议您在调试器中查看代码并检查它们是否正在查看相同的对象/映射。我怀疑你有两个包含 map 的对象。

关于java - 为什么HashMap在退出方法后会重置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5679313/

相关文章:

java - 如何从 xml 解析器获取值到其他类

Java HashMap 与 bukkit

java - 使用 Spring MVC 的简单文件上传形式的 NullPointerException

java - 我需要帮助弄清楚如何阻止我的程序抛出 NullPointerException

java - 自 OpenQuant 消亡以来,是否有免费的实时财务数据馈送?

java - 将 C++ 构造函数翻译成 Java

java - Java 中的 Rest API 下载大小 > 3 GB 的文件 (.tar.gz)

clojure - 创建 HashMap Clojure

Java内存使用简单数据结构

android - 为什么从 URL 解析 XML 时会出现 java.lang.NullPointerException 异常? (以及如何解决它?)