问题描述
你好。
这两天我试图弄清楚我在下面的代码中做错了什么:
Hello there.
For two days i''m trying to figure out what am i doing wrong in the code bellow:
(receiving NullPointerException on actionPerformed method of sendButton, on line output.println(messageToServer);
public class ChatApplication implements ActionListener{
//here i've declared my variables JTextAreas, clientSocket, buttons, JFrame, String userName
public ChatApplication(){}
public ChatApplication(String userName,String serverName) throws UnknownHostException, IOException{
//super(userName);
// try{
this.userName=userName;
clientSocket=new Socket(serverName,11111);
input=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
output=new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()),true);
output.println(userName);
new ServerMessageThread().start();
// }catch(IOException e){
// System.err.println(e.getMessage());
// }
}
//creating my interface etc.
sendButton.addActionListener(this);
connectButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String userName=JOptionPane.showInputDialog(window,"Name: "," ",JOptionPane.PLAIN_MESSAGE);
try{
String serverName="localhost";
new ChatApplication(userName, serverName);
window.setTitle(userName);
}catch(IOException ex){
JOptionPane.showMessageDialog(window,"Clientul nu se poate conecta."+ex.getMessage().toString());
}
}
});
startServerButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Thread thread=new Thread(){
public void run(){
ChatServer server=new ChatServer();
try {
server.process();
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
};
thread.start();
}
});
}
public static void main(String[] args) {
ChatApplication program=new ChatApplication();
program.createInterface();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==sendButton){
sendFlag=true;
try{
String messageToServer=new String("");
messageToServer=messageField.getText();
//PipedOutputStream pipedOut=new PipedOutputStream();
output.println(messageToServer);//<- this is where i get NullPointerException
}catch(NullPointerException ex){
System.err.println(ex.getMessage());
ex.printStackTrace();
}
}
}
/*
* clasa se ocupa cu crearea unui thread pentru a putea citi de la server
*/
class ServerMessageThread extends Thread{
public void run(){
String serverResponse=new String();
try {
while(true){
serverResponse=input.readLine();
System.out.println(serverResponse);
output.println(serverResponse); //and here sending a message to the server works
ChatApplication.messagesTextArea.append(serverResponse+"\n");
}
} catch (IOException e) {
ChatApplication.messagesTextArea.setForeground(Color.RED);
ChatApplication.messagesTextArea.setText("Eroare citire de la server: "+e.getMessage());
}
}
}
/*
* clasa care se ocupa cu crearea firelor de executie a clientilor
*/
}
当我点击sendButton时,我得到NullPointerException。而且我有点卡在那里。 outputStream在ServerMessageThread中工作,但在单击我的按钮时则不行。我知道它可以修复,但目前我无法理解。
ChatServer可以正常工作,因为我已经测试过它而不使用客户端界面,在控制台中测试它。对于每个客户我都在创建一个帖子。
我只需要一个想法。谢谢。最好的问候。
PS:我知道在问题中加入很多代码并不是一个好习惯,但我想告诉你我是如何实现客户端的app。
When i click the sendButton i get NullPointerException. And I''m kinda stuck there. The outputStream works in the ServerMessageThread, but not when clicking my button. I know that it can be fixed, but at the moment i can''t figure it out.
The ChatServer works, because i''ve tested it without using a client interface, tested it in the console. For every client i''m creating a thread.
I just need an idea.Thank you. Best regards.
P.S: i know that it isn''t a good practice to put much code in the question, but i wanted to show you how i implemented the client app.
推荐答案
public static void main(String[] args) {
ChatApplication program=new ChatApplication(); //calling noarg constructor
program.createInterface();
}
我明白了,您在参数化的ctor(构造函数)中进行所有流初始化,但是您正在调用默认的ctor 。所有东西都保持为null,NullPointerException。
现在这里
I see, You do all stream initialisation in the parameterized ctor (constructor) but you are calling the default ctor. Every thing is kept null so, NullPointerException.
Now here
sendButton.addActionListener(this);
connectButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String userName=JOptionPane.showInputDialog(window,"Name: "," ",JOptionPane.PLAIN_MESSAGE);
try{
String serverName="localhost";
new ChatApplication(userName, serverName); //call to appropriate constructor
window.setTitle(userName);
}catch(IOException ex){
JOptionPane.showMessageDialog(window,"Clientul nu se poate conecta."+ex.getMessage().toString());
}
}
});
在这里你打电话给所需的ctor并开始一个线程,ctor,初始化流,因此通话很好。
PS:你的用户界面很乱,做一些功课。
Here you call the required ctor and start a thread, the ctor, initializes the streams and hence the calls are fine.
PS: Your UI is messy, do some homework.
这篇关于写入服务器时的NullPointerExpetion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!