我有一个简单的ChatClient,可以将数据发送到服务器。为了让Chat Client发送数据,我创建一个Socket,为socket.getOutputStream()创建一个PrintWriter,然后执行writer.println(“”)。出于某种原因,我得到了一个很大的例外。我查看了类似的例外情况,很多人都说要更新java。但我的Mac上有最新版本。是因为我有Leopard,还是我的伪劣节目?请看一下。
private void setUpNetworking() {
try {
**sock = new Socket("127.0.0.1", 65534);**
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
**writer = new PrintWriter(sock.getOutputStream());**
System.out.println("made connection... everything here works");
}
catch (IOException ex) {
ex.printStackTrace();
System.out.println("setup fail, but i didn't get this");
}
}
public class SendButtonListener implements ActionListener{
public void actionPerformed (ActionEvent ev){
try {
**writer.println(entertext.getText());**
**writer.flush();**
System.out.println("PROBLEM IS RIGHT HERE!!!");
}
catch (Exception ex) {
ex.printStackTrace();
System.out.println("i get a exception. HELP");
}
这是我的例外:
java.lang.NullPointerException
at SimpleChatClient$SendButtonListener.actionPerformed(SimpleChatClient.java:52)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1882)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2202)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:246)
at java.awt.Component.processMouseEvent(Component.java:5617)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3129)
at java.awt.Component.processEvent(Component.java:5382)
at java.awt.Container.processEvent(Container.java:2010)
at java.awt.Component.dispatchEventImpl(Component.java:4083)
at java.awt.Container.dispatchEventImpl(Container.java:2068)
at java.awt.Component.dispatchEvent(Component.java:3918)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4256)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3936)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3866)
at java.awt.Container.dispatchEventImpl(Container.java:2054)
at java.awt.Window.dispatchEventImpl(Window.java:1801)
at java.awt.Component.dispatchEvent(Component.java:3918)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:501)
at java.awt.EventQueue.access$000(EventQueue.java:80)
at java.awt.EventQueue$1.run(EventQueue.java:462)
at java.awt.EventQueue$1.run(EventQueue.java:461)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:84)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:95)
at java.awt.EventQueue$2.run(EventQueue.java:476)
at java.awt.EventQueue$2.run(EventQueue.java:475)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:84)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:473)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
**at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)**
我不知道这意味着什么。请帮忙。
答案 0 :(得分:1)
从你的堆栈跟踪和注释中,听起来就像你打电话
writer.println(entertext.getText());
在您actionPerformed
的{{1}}方法中,SendButtonListener
writer
为空。
哪个是null,以及它是如何通过这种方式从代码中看不清楚,但你应该能够通过调试器或日志记录来解决它。
答案 1 :(得分:0)
以下是调试方法 - 或者:
println
以打印每个可能为null的内容,就在它出现异常之前答案 2 :(得分:0)
检查.Simple Chat Client的第52行,其中包含or.null