当我调用此方法时,我的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)
如何解决?
答案 0 :(得分: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()
构造函数方法结束后不再存在。
答案 1 :(得分:0)
at org.elsys.salvation.client.FunctionalityManager.getPerson(FunctionalityManager.java:101)
哪一行101?
我beleave TextItem box
为空。