第一次使用溢出很抱歉如果我做错了。但无论如何,我需要帮助我做的聊天室项目。简单地说,我一直存在的问题是当我的程序启动服务器然后客户端然后我要求用户的名字。但是在那之后我一直为我的客户收到这个错误。
Exception in thread "main" java.lang.NullPointerException
at project_pkg2_multiuser_chatroom.CRClient.run(CRClient.java:82)
at project_pkg2_multiuser_chatroom.CRClient.main(CRClient.java:98)
我不需要为我完成整个程序只需要一点帮助就可以解决这个问题。谢谢,我真的很感激。哦,如果你有任何使用溢出的提示,可能会很好,谢谢你。
package project_pkg2_multiuser_chatroom;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
//*This calls all the methods I will be using*
public class CRServer extends JFrame{
private static final int PORT1 = 10000;
private static JTextArea jta = new JTextArea("Server Box");
private static ArrayList<String> users = new ArrayList<String>();
private static ArrayList<String> passwords = new ArrayList<String>();
private static HashSet<PrintWriter> writers1 = new HashSet<PrintWriter>();
private static HashSet<PrintWriter> writers2 = new HashSet<PrintWriter>();
private static HashSet<PrintWriter> small = new HashSet<PrintWriter>();
private static ServerSocket listener1;
private static Socket socket;
private static FileOutputStream fos;
private static ObjectOutputStream output;
private static FileInputStream fis;
private static ObjectInputStream infrom;
private static String input;
private static String user;
private static String pass;
private static BufferedReader in;
private static PrintWriter out;
private static int numPass;
private static String ch = null;
private static boolean chose = false;
//*this sets up the UI and starts the handler when the client begins*
public CRServer() throws IOException {
setLayout(new BorderLayout());
add(new JScrollPane(jta), BorderLayout.CENTER);
jta.append("Server started at " + new Date() + "\n");
setTitle("Server");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
System.out.println("The chatroom is running....");
listener1 = new ServerSocket(PORT1);
//InetAddress inetAddress = socket.getInetAddress();
//System.out.println("Client's host name is " + inetAddress.getHostName());
//System.out.println("Client's IP address is " + inetAddress.getHostAddress());
try {
while (true) {
new Handler(listener1.accept()).start();
}
} catch (Exception e) {
System.err.println(e);
} finally {
listener1.close();
}
}
//*This handles the threading*
private static class Handler extends Thread {
//*this creates the socket for the handler*
public Handler(Socket s) {
socket = s;
}
//*this runs the thread*
public synchronized void run() {
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
fos = new FileOutputStream("users.dat");
output = new ObjectOutputStream(fos);
fis = new FileInputStream("users.dat");
infrom = new ObjectInputStream(fis);
//*when the client begins this will ask their name*
// while(true){
// out.println("ENTERCHANNEL");
// System.out.println("SUBMITNAME");
// ch = in.readLine();
// if(ch == null){
// return;
// } else if (ch == 2){
// chose = true;
// }
// }
while (true) {
out.println("SUBMITNAME");
System.out.println("SUBMITNAME");
user = in.readLine();
if (user == null) {
return;
}
else if (!users.contains(user)) {
users.add(user);
break;
} else {
out.println("NAMETAKEN");
System.out.println("NAMETAKEN");
}
}
output.writeObject(users);
output.close();
//toClient.writeObject(users);
out.println("NAMEACCEPTED");
System.out.println("NAMEACCEPTED");
users = (ArrayList) infrom.readObject();
infrom.close();
//*this will ask their name*
while (true) {
out.println("SUBMITPASS");
System.out.println("SUBMITPASS");
pass = in.readLine();
if (pass == null) {
return;
}
if(!passwords.contains(pass)) {
passwords.add(pass);
break;
}
for (int i = 0; i < passwords.size(); i++) {
if (passwords.get(i).equals(pass)) {
int numPass = i;
}
}
for (int i = 0; i < numPass; i++) {
String name = users.get(i);
}
// if(user == name){
// out.println("PASSACCEPTED");
// }
}
writers1.add(out);
//*this handler the line the user types in*
// while (true && chose = false) {
// input = in.readLine();
// if (input == null) {
// return;
// }
// for (PrintWriter whatever : writers1) {
// jta.append(user + ": " + input + '\n');
// whatever.println("MESSAGE " + user + ": " + input);
// System.out.println("MESSAGE");
// }
// }
// while (true && chose = true) {
// input = in.readLine();
// if (input == null) {
// return;
// }
// for (PrintWriter whatever : writers2) {
// jta.append(user + ": " + input + '\n');
// whatever.println("MESSAGE " + user + ": " + input);
// System.out.println("MESSAGE");
// }
/*String line = in.readLine();
if (users.contains(line)) {
for (PrintWriter whis : small) {
jta.append(user + ": " + input + '\n');
whis.println("MESSAGE " + user + ": " + input);
}
}*/
//}
} catch (Exception ex) {
System.err.println(ex);
}
//*this removes the user once they leave*
finally {
if (user != null) {
users.remove(user);
}
if (out != null) {
writers1.remove(out);
}
try {
socket.close();
} catch (IOException e) {
System.err.println("Problem closing socket!");
}
}
//*this updates the binary file which store the users names
try {
fos = new FileOutputStream("user.dat");
output = new ObjectOutputStream(fos);
output.writeObject(users);
output.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(CRServer.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(CRServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
//*this is the main method*
public static void main(String[] args) throws Exception {
new CRServer();
}
}
package project_pkg2_multiuser_chatroom;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
//*This calls all the methods I will be using*
public class CRClient extends JFrame{
private static int PORT = 10000;
private BufferedReader in;
private PrintWriter out;
private BufferedReader inList;
ObjectInputStream inform;
private static FileInputStream fis;
private static ObjectInputStream infrom;
private DataInputStream is = null;
private static ArrayList<String> users = new ArrayList<String>();
JFrame frame = new JFrame("Client Box");
//JFrame mesage = new JFrame("Message");
JTextField textField = new JTextField(40);
JTextArea messageArea = new JTextArea(8, 40 );
JComboBox userList;
//*this sets up the UI*
public CRClient() throws FileNotFoundException, IOException, ClassNotFoundException {
userList = new JComboBox(users.toArray());
textField.setEditable(false);
messageArea.setEditable(false);
frame.getContentPane().add(textField, BorderLayout.NORTH);
frame.getContentPane().add(userList, BorderLayout.SOUTH);
frame.getContentPane().add(messageArea, BorderLayout.CENTER);
frame.pack();
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
out.println(textField.getText());
textField.setText("");
}
});
}
/*
private String getServerAddress() {
return JOptionPane.showInputDialog(frame, "Server's IP (It's 127.0.0.1)",
"Welcome to the chatroom", JOptionPane.PLAIN_MESSAGE);
}*/
private String getChannel() {
return JOptionPane.showInputDialog(frame, "Select channel",
"Do you want 1 or 2?", JOptionPane.PLAIN_MESSAGE);
}
//*Allows the user to type in their name*
private String findName() {
return JOptionPane.showInputDialog(frame, "Your Screen Name?",
"Welcome to the chatroom", JOptionPane.PLAIN_MESSAGE);
}
//*Allows the user to type in their password*
// private String findPassword() {
// return JOptionPane.showInputDialog(frame, "Enter Password.",
// "Welcome to the chatroom", JOptionPane.PLAIN_MESSAGE);
// }
//*Sets up the thread which the client runs on*
private void run() throws IOException, ClassNotFoundException {
String serverAddress = "127.0.0.1";
Socket socket = new Socket(serverAddress, PORT);
//inform = new ObjectInputStream(socket.getInputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true) {
String line = in.readLine();
//users = (ArrayList) inform.readObject();
//*when SUBMITNAME comes in begin send the name the user typed
// if (line.startsWith("ENTERCHANNEL")){
// out.println(getChannel());
// } else
if (line.startsWith("SUBMITNAME")) {
out.println(findName()); //LINE 82
} else if (line.startsWith("NAMETAKEN")){
JOptionPane.showMessageDialog(frame, "Error: Name already exists", "Error", JOptionPane.ERROR_MESSAGE);
} else if (line.startsWith("NAMEACCEPTED")) {
textField.setEditable(true);
} else if (line.startsWith("MESSAGE")) {
messageArea.append(line.substring(8) + "\n");
}
}
}
//*this is the main method*
public static void main(String[] args) throws IOException, FileNotFoundException, ClassNotFoundException {
CRClient client = new CRClient();
client.frame.setDefaultCloseOperation(JFrame.EXI`enter code here`T_ON_CLOSE);
client.frame.setVisible(true);
client.run(); //LINE 98
}
}
答案 0 :(得分:0)
之所以发生这种情况,是因为您没有将out
设置为CRClient
对象中的任何内容。
答案 1 :(得分:0)
PrintWriter out对象永远不会在客户端初始化。