gpt4 book ai didi

java - nullPointer 异常,我哪里出错了?

转载 作者:行者123 更新时间:2023-12-01 18:17:50 25 4
gpt4 key购买 nike

我正在学习《Head First Java》一书,现在正在尝试构建一个聊天室。我有一个 ChatServer 和一个 ChatClient,它们可以相互连接。但是当我尝试发送消息时出现以下错误:

java.lang.NullPointerException
at VerySimpleChatServer.tellEveryone(VerySimpleChatServer.java:61)
at VerySimpleChatServer$ClientHandler.run(VerySimpleChatServer.java:29)
at java.lang.Thread.run(Unknown Source)

我检查了我的tellEveryone,但我看不出我哪里出错了。我没有看到任何地方有任何指向任何内容的指针。

谁能帮我一点忙吗?

客户端服务器代码:

import java.io.*; 
import java.net.*;
import java.util.*;

public class VerySimpleChatServer {

ArrayList clientOutputStreams;

public class ClientHandler implements Runnable {
BufferedReader reader;
Socket sock;

public ClientHandler(Socket clientSocket) {
try {
sock = clientSocket;
InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(isReader);
} catch(Exception ex)
{
ex.printStackTrace();
}
}

public void run() {
String message;
try {
while (( message = reader.readLine()) != null) {
System.out.println(" read " + message);
tellEveryone(message);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}

public static void main (String[] args) {
new VerySimpleChatServer(). go();
}

public void go() {
List<PrintWriter>clientOutputStreams = new ArrayList<>();
try {
ServerSocket serverSock = new ServerSocket( 5000);
while( true) {
Socket clientSocket = serverSock.accept();
PrintWriter writer = new PrintWriter( clientSocket.getOutputStream());
clientOutputStreams.add( writer);
Thread t = new Thread( new ClientHandler( clientSocket));
t.start(); System.out.println(" got a connection");
}
} catch(Exception ex)
{
ex.printStackTrace();
}
}

public void tellEveryone(String message) {
Iterator it = clientOutputStreams.iterator();
while(it.hasNext()) {
try {
PrintWriter writer = (PrintWriter) it.next();
writer.println(message);
writer.flush();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}

}

ChatClient 的代码:

import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SimpleChatClient {
JTextArea incoming;
JTextField outgoing;
BufferedReader reader;
PrintWriter writer;
Socket sock;


public static void main(String[] args) {
SimpleChatClient client = new SimpleChatClient();
client.go();
}

public void go()
{
JFrame frame = new JFrame("Ludicrously Simple Chat Client");
JPanel mainPanel = new JPanel();
incoming = new JTextArea(15, 30);
incoming.setLineWrap(true);
incoming.setWrapStyleWord(true);
incoming.setEditable(false);

JScrollPane qScroller = new JScrollPane(incoming);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

outgoing = new JTextField(20);
JButton sendButton = new JButton("Send");
sendButton.addActionListener(new SendButtonListener());

mainPanel.add(qScroller);
mainPanel.add(outgoing);
mainPanel.add(sendButton);

setUpNetworking();

Thread readerThread = new Thread(new IncomingReader());
readerThread.start();

frame.add(mainPanel, BorderLayout.CENTER);
frame.setSize(400, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}


private void setUpNetworking()
{
try
{
sock = new Socket("127.0.0.1", 5000);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
System.out.println("networking established");
}
catch(IOException ex)
{
ex.printStackTrace();
}
}



public class SendButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
try
{
writer.println(outgoing.getText());
writer.flush();
}
catch(Exception ex)
{
ex.printStackTrace();
}

outgoing.setText("");
outgoing.requestFocus();
}
}


public class IncomingReader implements Runnable
{
public void run()
{
String message;
try
{
while ( (message = reader.readLine()) != null)
{
System.out.println("read " + message);
incoming.append(message + "\n");
}//close while
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}

}

编辑:

SimpleChatServerClient第60行到74行:

    public void tellEveryone(String message) {
Iterator it = clientOutputStreams.iterator();
while(it.hasNext()) {
try {
PrintWriter writer = (PrintWriter) it.next();
writer.println(message);
writer.flush();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}

}

最佳答案

您在 go 中使用的 clientOutputStreams

List<PrintWriter>clientOutputStreams = new ArrayList<>(); 

对于该方法来说是本地的

使用的clientOutputStreams

public void tellEveryone(String message) {
Iterator it = clientOutputStreams.iterator();

是从未初始化过的类变量

关于java - nullPointer 异常,我哪里出错了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28624102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com