为什么当我尝试在 PrintWriter 上写入时会抛出异常?
我有一个简单的 ChatClient,它将数据发送到服务器。为了让聊天客户端发送数据,我创建一个 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)**
我不知道这意味着什么。请帮忙。
I have a simple ChatClient that sends data to a Server. In order for Chat Client to send data, I make a Socket, make a PrintWriter to the socket.getOutputStream(), then do writer.println(""). For some reason, I get the a big fat exception. I looked at similar exceptions to mine, and a lot of people said to update java. But I have the latest version on my Mac. Is it because I have Leopard, or is it my shoddy programming? Please take a look.
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");
}
Here's my exception:
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)**
I don't know what this means. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从堆栈跟踪和注释来看,听起来像是当您调用
SendButtonListener
的actionPerformed
方法时,或者writer
enterText 为空。
哪个是 null,以及它是如何以这种方式获得的,从代码中尚不清楚,但您应该能够通过调试器或日志记录来找出它。
From your stack trace and notes, it sounds like when you call
in the
actionPerformed
method of yourSendButtonListener
, eitherwriter
enterText
is null.Which is null, and how it got that way isn't clear from the code, but you should be able to figure it out with a debugger or logging.
以下是如何调试它 - 要么:
Here's how to debug this - either:
println
to print each thing that might be null, just before it has the exceptionChekc.SimpleChatClient的第52行,有ir.null的东西
Chekc the line 52 of.SimpleChatClient, something there ir.null