2012-05-14 97 views
0

我在我的GWT項目空指針異常,當我把這個方法:UmbrellaException引起NullPointerException異常

public void getPerson(ArrayList<Date> set, TextItem box, ListBox listBox) { 
    if (listBox.getSelectedIndex() == 0) { 
     Person person = new Person(box.getValueAsString(), set); 
     Leaders.add(person); 
    } else if (listBox.getSelectedIndex() == 1) { 
     Person person = new Person(box.getValueAsString(), set); 
     Reviewers.add(person); 
    } else if (listBox.getSelectedIndex() == 2) { 
     Person person = new Person(box.getValueAsString(), set); 
     Leaders.add(person); 
     Reviewers.add(person); 
    } 
} 

它與下面的定義類的一部分:

public class FunctionalityManager { 

private Date startDate; 
private Date endDate; 
private HashSet<Person> Leaders; 
private HashSet<Person> Reviewers; 
private ArrayList<SoftwareWork> SoftwareWorks; 
private ArrayList<DiplomaWork> HardwareWorks; 
private ArrayList<DiplomaWork> NetWorks; 

public FunctionalityManager() { 
    super(); 
    startDate = new Date(); 
    endDate = new Date(); 
    HashSet<Person> Leaders = new HashSet<Person>(); 
    HashSet<Person> Reviewers = new HashSet<Person>(); 
    ArrayList<SoftwareWork> SoftwareWorks = new ArrayList<SoftwareWork>(); 
    ArrayList<DiplomaWork> HardwareWorks = new ArrayList<DiplomaWork>(); 
    ArrayList<DiplomaWork> NetWorks = new ArrayList<DiplomaWork>(); 
} 

...和一些獲得者和二傳手。在主類哪裏是我這裏onModuleLoad()方法是如何創建一個FunctionalityManager:

public class Salvation implements EntryPoint { 

private FunctionalityManager FM = new FunctionalityManager(); 

和這裏就是我所說的getPerson方法:

Button oneMoreButton = new Button("One More"); 
    oneMoreButton.addClickHandler(new ClickHandler() { 
     public void onClick(ClickEvent event) { 
      FM.getPerson(dates, textBox, listBox); 
      RootPanel.get("mainDiv").clear(); 
      addPerson(); 
     } 
    }); 

    Button next = new Button("Next"); 
    next.addClickHandler(new ClickHandler() { 
     public void onClick(ClickEvent event) { 
      FM.getPerson(dates, textBox, listBox); 
      RootPanel.get("mainDiv").clear(); 
      addDiploma(); 

     } 
    }); 

這裏是和異常,如果它會幫助:

16:53:11.406 [ERROR] [Salvation] Uncaught exception escaped 
com.google.gwt.event.shared.UmbrellaException: One or more exceptions caught, see full set in UmbrellaException#getCauses 
    at com.google.gwt.event.shared.HandlerManager.fireEvent(HandlerManager.java:129) 
    at com.smartgwt.client.widgets.BaseWidget.fireEvent(BaseWidget.java:67) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103) 
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71) 
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172) 
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:337) 
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:218) 
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136) 
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561) 
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269) 
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91) 
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java) 
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:213) 
    at sun.reflect.GeneratedMethodAccessor38.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103) 
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71) 
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172) 
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:292) 
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:546) 
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.NullPointerException: null 
    at org.elsys.salvation.client.FunctionalityManager.getPerson(FunctionalityManager.java:101) 
    at org.elsys.salvation.client.Salvation$5.onClick(Salvation.java:149) 
    at com.smartgwt.client.widgets.events.ClickEvent.dispatch(ClickEvent.java:96) 
    at com.smartgwt.client.widgets.events.ClickEvent.dispatch(ClickEvent.java:1) 
    at com.google.gwt.event.shared.GwtEvent.dispatch(GwtEvent.java:1) 
    at com.google.web.bindery.event.shared.EventBus.dispatchEvent(EventBus.java:40) 
    at com.google.web.bindery.event.shared.SimpleEventBus.doFire(SimpleEventBus.java:193) 
    at com.google.web.bindery.event.shared.SimpleEventBus.fireEvent(SimpleEventBus.java:88) 
    at com.google.gwt.event.shared.HandlerManager.fireEvent(HandlerManager.java:127) 
    at com.smartgwt.client.widgets.BaseWidget.fireEvent(BaseWidget.java:67) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103) 
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71) 
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172) 
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:337) 
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:218) 
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136) 
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561) 
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269) 
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91) 
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java) 
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:213) 
    at sun.reflect.GeneratedMethodAccessor38.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103) 
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71) 
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172) 
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:292) 
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:546) 
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363) 
    at java.lang.Thread.run(Unknown Source) 

如何修復?

回答

1

您並未正確創建全局變量。更改以下行...

public FunctionalityManager() { 
    super(); 
    startDate = new Date(); 
    endDate = new Date(); 
    Leaders = new HashSet<Person>(); // Changed 
    Reviewers = new HashSet<Person>(); // Changed 
    SoftwareWorks = new ArrayList<SoftwareWork>(); // Changed 
    HardwareWorks = new ArrayList<DiplomaWork>(); // Changed 
    NetWorks = new ArrayList<DiplomaWork>(); // Changed 
} 

你正在創造局部變量稱爲LeadersReviewers等,而不是初始化的全局變量。

基本上你這與其說是..

HashSet<Person> Leaders = new HashSet<Person>(); 

的只是調用這個...

Leaders = new HashSet<Person>(); 

所以,全局變量實際上沒有被設置 - 你只是創建新的本地具有相同名稱的變量,在FunctionalityManager()構造方法結束後不再存在。

+0

'Leaders.add(person);'是#101行,是的TextItem和列表框是在此之前創建的(但是在調用方法ibeing後被添加到RootPanel) – malkoto1

+0

發現問題 - 請參閱我編輯的答案 – wattostudios

+0

是的,我看到它,它的工作,謝謝! – malkoto1

0
at org.elsys.salvation.client.FunctionalityManager.getPerson(FunctionalityManager.java:101) 

哪一行是101行?

我beleave TextItem box爲空。