HashMap因未知原因返回空指针

时间:2014-04-13 05:54:16

标签: java nullpointerexception hashmap

你好SO的人,

我很难理解为什么我的代码返回空指针。任何帮助将不胜感激!

public class GetTabByName {

    HashMap<String,JPanel> allTabs;

    public void getMap(HashMap<String,JPanel> allTabs){

        this.allTabs = allTabs;

             //this print works fine
        System.out.println(allTabs.get("addPatDocTab").getName());
    }

    public JPanel getTabByName(String tabName){

        JPanel tab = null;

            //this is where the problem occurs. even I try to print the line above
            //I get the same null pointer
        if(allTabs.containsKey(tabName)){
            tab = allTabs.get(tabName);
        }
        else{
            System.out.println(tabName + " is not a tab");
        }
        return tab;
    } 
}

错误信息如下:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at edu.miami.cis324.swing.jfarache.GetTabByName.getTabByName(GetTabByName.java:23)
    at edu.miami.cis324.swing.jfarache.AddEditElements.addElementsToPane(AddEditElements.java:14)
    at edu.miami.cis324.swing.jfarache.MainFrame.<init>(MainFrame.java:177)
    at edu.miami.cis324.swing.jfarache.SwingHome.createAndShowGUI(SwingHome.java:27)
    at edu.miami.cis324.swing.jfarache.SwingHome.access$0(SwingHome.java:21)
    at edu.miami.cis324.swing.jfarache.SwingHome$1.run(SwingHome.java:16)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

提前感谢您的帮助!

===== EDIT ==

更相关的代码:

//create a HashMap that connects the name of each tab to its corresponding JPanel
      //this map will be used for the method getTabByName
      HashMap<String,JPanel> tabsMap = new HashMap<String,JPanel>();
      for(Component tab:allTabs){

          JPanel tempTab = (JPanel)tab;
          tabsMap.put(tab.getName(), tempTab);
      }

      //pass the map to the GetTabsByName class so that other classes can access GetTabByName's getTabByName method
      GetTabByName passMap = new GetTabByName();
      passMap.getMap(tabsMap);

      AddEditElements subTab1 = new AddEditElements();
      subTab1.addElementsToPane();

最后:

public class AddEditElements {

    //the method accepts a list of the tabs in Add/Edit
    public void addElementsToPane(){

        GetTabByName get = new GetTabByName();

        JPanel addPatDocTab = get.getTabByName("addPatDocTab");

        JRadioButton rdbtnPatient = new JRadioButton("Patient");
        rdbtnPatient.setBounds(0, 0, 109, 23);  

        addPatDocTab.add(rdbtnPatient); 
    }
}

3 个答案:

答案 0 :(得分:2)

问题在于:

GetTabByName get = new GetTabByName();
JPanel addPatDocTab = get.getTabByName("addPatDocTab");

如果您按照这样做,它会创建一个新的GetTabByName,默认情况下它具有null allTabs。然后它调用get.getTabByName(&#34; addPatDocTab&#34;);它试图访问该特定GetTabsByName的所有标记。

在调用getTabByName之前,您应该为allTabs分配一些内容。

在类GetTabByName中,你可以像这样立即实例化allTab:

HashMap<String,JPanel> allTabs = new HashMap<String, JPanel>()

答案 1 :(得分:1)

public void addElementsToPane() {
    GetTabByName get = new GetTabByName();

这是该方法的本地化。 getpassMap(您调用的getMap(tabsMap))没有任何关系。

您可能希望将passMap传递给addElementsToPane()方法。

public class AddEditElements {

    public void addElementsToPane(GetTabByName passMap) { // add parameter       
        //GetTabByName get = new GetTabByName(); //don't create a new object
        JPanel addPatDocTab = passMap.getTabByName("addPatDocTab"); // <-------

这改变了方法的签名。

然后,

AddEditElements subTab1 = new AddEditElements();
subTab1.addElementsToPane(passMap); // pass the object with the populated map

答案 2 :(得分:0)

getMap(HashMap<String,JPanel> allTabs) method

您正尝试将值分配给全局定义的

HashMap<String,JPanel> allTabs; 

如果我的理解是正确的。

请尝试以下方法

HashMap<String,JPanel> allTabs global variable name differntly

HashMap<String,JPanel> globalAllTabs;

并尝试相同的

在此之前确保通过在

中打印全局定义的hashmap
getTabByName(String tabName) method