调用此方法时,我的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)


如何解决?

最佳答案

您没有正确创建全局变量。更改以下行...

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()构造函数方法结束后,该局部变量将不再存在。

09-11 19:36