0

当我调用此方法时,我的 GWT 项目中有空指针异常:

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)

如何修复?

4

2 回答 2

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
}

您正在创建名为Leaders,Reviewers等的局部变量,而不是初始化全局变量。

基本上你叫这个..

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

而不是仅仅调用这个......

Leaders = new HashSet<Person>();

所以全局变量实际上并没有被设置——你只是创建了同名的新局部变量,在FunctionalityManager()构造函数方法结束后不再存在。

于 2012-05-14T14:12:37.340 回答
0
 at org.elsys.salvation.client.FunctionalityManager.getPerson(FunctionalityManager.java:101)

101号线是哪个?

我相信TextItem box是空的。

于 2012-05-14T14:09:58.607 回答