有人可以帮我调试吗,
编辑1:错误不在下面的代码中 * 以下代码中出现错误: *
if (e.getSource().equals(btnRefresh))
{
PeerList.removeAllElements();
FileList.removeAllElements();
try {
AllPeers=(List <String>)Services.getPeers();
} catch (RemoteException e1) {
// TODO Auto-generated catch block
}
for (String CurrentPeer:AllPeers)
{
System.out.println(CurrentPeer);
PeerList.addElement(CurrentPeer);
}
}
编辑1:错误在这里
lstPeerList.addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent e)
{
try
{
SelectedPeer=(String)lstPeerList.getSelectedValue();
List <String> PeerFiles=Services.getPeerFiles(SelectedPeer);
FileList.removeAllElements();
for (String CurrentFile:PeerFiles)
{
FileList.addElement(CurrentFile);
}
}
catch (RemoteException e1)
{
// TODO Auto-generated catch block
}
}});
在下面的代码中,如果我将变量检索为以下代码,则会发生错误:
SelectedPeer=(String)lstPeerList.getSelectedValue();
List <String> PeerFiles=Services.getPeerFiles(SelectedPeer);
但如果我这样做,即不使用检索到的变量,则不会发生错误:
SelectedPeer=(String)lstPeerList.getSelectedValue();
List <String> PeerFiles=Services.getPeerFiles("Noor");
没有错误发生,
例外:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Server.getPeerFiles(Server.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:637)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:178)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:132)
at $Proxy0.getPeerFiles(Unknown Source)
at MainForm$1.valueChanged(MainForm.java:55)
at javax.swing.JList.fireSelectionValueChanged(JList.java:1765)
at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1779)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:167)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:147)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:194)
at javax.swing.DefaultListSelectionModel.removeIndexInterval(DefaultListSelectionModel.java:660)
at javax.swing.plaf.basic.BasicListUI$Handler.intervalRemoved(BasicListUI.java:2589)
at javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:161)
at javax.swing.DefaultListModel.removeAllElements(DefaultListModel.java:385)
at MainForm.actionPerformed(MainForm.java:223)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6267)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
答案 0 :(得分:2)
每次更改选择时都会调用ListSelectionListener。这可能意味着现在没有选择,lstPeerList.getSelectedValue()
会返回null
。然后你调用getPeerFiles(null)
,这个方法似乎无法处理null
个参数。
您可以更改getPeerFiles
方法,也可以仅在所选值不是null
时调用它。
答案 1 :(得分:0)
无论第54行是什么
at Server.getPeerFiles(Server.java:54)
为空,所以发布第54行
编辑:
可能是空的东西:
e, btnRefresh, PeerList, FileList, Services
第54行中的任何一个都是你的原因。
(抱歉不知道将它添加到我原来的帖子中)