gpt4 book ai didi

java - "Exception in thread "AWT-EventQueue-0 "java.lang.NullPointerException"按下按钮

转载 作者:行者123 更新时间:2023-12-01 10:02:45 43 4
gpt4 key购买 nike

我的教授给了我一些示例代码来制作一个基本的聊天程序,其中一个要求是允许用户通过按下按钮来连接和断开连接。我创建了按钮并为两者添加了操作监听器,但是当我单击“连接”按钮时,我得到以下信息:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Client.actionPerformed(Client.java:64)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

我将错误追溯到此 block :

try{
myOutputStream.reset(); //this is line 64 in the full Java file
myOutputStream.writeObject(myObject);
}catch(IOException ioe){
System.out.println(ioe.getMessage());
}

我会说我确实对示例代码进行了更改,因此上面的部分进行一些编辑之前就可以工作,即将启动连接的代码块移动到 if 语句中检查按钮是否被点击。之前它位于“客户端”构造函数(我的教授为该程序所做的)中,并且在启动 Java 程序时立即运行和连接。

完整的 Client 类在这里:

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

//THINGS TO DO:
//1: Add a means to add usernames
//2: Add something that adds a list of users
//3: Button to disconnect/connect
//4: Panel that allows you to draw things


public class Client extends Thread implements ActionListener{

ChatMessage myObject;
boolean sendingdone = false, receivingdone = false;
Scanner scan;
Socket socketToServer;
ObjectOutputStream myOutputStream;
ObjectInputStream myInputStream;
Button connect,disconnect;
Frame f;
TextField tf;
TextArea ta;

public Client(){

f = new Frame();
f.setSize(300,400);
f.setTitle("Chat Client");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
tf = new TextField();
tf.addActionListener(this);
f.add(tf, BorderLayout.NORTH);
ta = new TextArea();
f.add(ta, BorderLayout.CENTER);

/////////////////////////
/*
try{
socketToServer = new Socket("127.0.0.1", 4000);

myOutputStream = new ObjectOutputStream(socketToServer.getOutputStream());

myInputStream = new ObjectInputStream(socketToServer.getInputStream());
start();

}
catch(Exception e){
System.out.println(e.getMessage());
}
*/this is the original spot

Panel b = new Panel();

connect = new Button("Connect");
disconnect = new Button("Disconnect");
connect.addActionListener(this);
disconnect.addActionListener(this);


b.add(connect);
b.add(disconnect);
f.add(b, BorderLayout.SOUTH);

f.setVisible(true);
}
public void actionPerformed(ActionEvent ae){
myObject = new ChatMessage();
myObject.setMessage(tf.getText());
tf.setText("");
try{
myOutputStream.reset();
myOutputStream.writeObject(myObject);
}catch(IOException ioe){
System.out.println(ioe.getMessage());
}

if (ae.getSource() == connect){
try{
socketToServer = new Socket("127.0.0.1", 4000);

myOutputStream =
new ObjectOutputStream(socketToServer.getOutputStream());

myInputStream =
new ObjectInputStream(socketToServer.getInputStream());
start();


}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}



public void run(){
System.out.println("Listening for messages from server . . . ");
try{
while(!receivingdone){
myObject = (ChatMessage)myInputStream.readObject();
ta.append(myObject.getMessage() + "\n");
}
}catch(IOException ioe){
System.out.println("IOE: " + ioe.getMessage());
}catch(ClassNotFoundException cnf){
System.out.println(cnf.getMessage());
}
}

public static void main(String[] arg){

Client c = new Client();

}
}

我认为您还需要这两个 Java 类,ChatServer(它必须正在运行并且客户端必须连接到)和 ChatMessage(客户端用来创建消息的类):

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

public class ChatServer{
public static void main(String[] args )
{
ArrayList<ChatHandler> AllHandlers = new ArrayList<ChatHandler>();

try
{ ServerSocket s = new ServerSocket(4000);

for (;;)
{ Socket incoming = s.accept( );
new ChatHandler(incoming, AllHandlers).start();
}
}
catch (Exception e)
{ System.out.println(e);
}
}
}

class ChatHandler extends Thread
{ public ChatHandler(Socket i, ArrayList<ChatHandler> h)
{
incoming = i;
handlers = h;
handlers.add(this);
try{
in = new ObjectInputStream(incoming.getInputStream());
out = new ObjectOutputStream(incoming.getOutputStream());
}catch(IOException ioe){
System.out.println("Could not create streams.");
}
}
public synchronized void broadcast(){

ChatHandler left = null;
for(ChatHandler handler : handlers){
ChatMessage cm = new ChatMessage();
cm.setMessage(myObject.getMessage());
try{
handler.out.writeObject(cm);
System.out.println("Writing to handler outputstream: " + cm.getMessage());
}catch(IOException ioe){
//one of the other handlers hung up
left = handler; // remove that handler from the arraylist
}
}
handlers.remove(left);

if(myObject.getMessage().equals("bye")){ // my client wants to leave
done = true;
handlers.remove(this);
System.out.println("Removed handler. Number of handlers: " + handlers.size());
}
System.out.println("Number of handlers: " + handlers.size());
}

public void run()
{
try{
while(!done){
myObject = (ChatMessage)in.readObject();
System.out.println("Message read: " + myObject.getMessage());
broadcast();
}
} catch (IOException e){
if(e.getMessage().equals("Connection reset")){
System.out.println("A client terminated its connection.");
}else{
System.out.println("Problem receiving: " + e.getMessage());
}
}catch(ClassNotFoundException cnfe){
System.out.println(cnfe.getMessage());
}finally{
handlers.remove(this);
}
}

ChatMessage myObject = null;
private Socket incoming;

boolean done = false;
ArrayList<ChatHandler> handlers;

ObjectOutputStream out;
ObjectInputStream in;
}

(我没有对其中任何一个进行任何更改)

import java.io.*;

public class ChatMessage implements Serializable{
public String name;
public String message;

public ChatMessage(){}
public ChatMessage(String name, String message){
setName(name);
setMessage(message);
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setMessage(String message){
this.message = message;
}
public String getMessage(){
return message;
}
}

最佳答案

myOutputStream 从未初始化。您评论了应该发生的部分。

关于java - "Exception in thread "AWT-EventQueue-0 "java.lang.NullPointerException"按下按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36682094/

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