你好,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)
在此先感谢您的帮助!
=====编辑======
更相关的代码:
//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);
}
}
最佳答案
public void addElementsToPane() {
GetTabByName get = new GetTabByName();
那是该方法的局部特性。
get
与passMap
(调用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