gpt4 book ai didi

Java Messenger(多线程和Swing)

转载 作者:行者123 更新时间:2023-12-01 16:34:41 25 4
gpt4 key购买 nike

嘿,我在使用 java Messenger 应用程序时遇到了问题。我在 Netbeans 中制作了一个简单的 GUI 以及服务器和客户端类。当我在 GUI 中创建服务器的新实例时,我启动一个新线程并开始在其中监听。当服务器获取某些内容并转到 handleConnection() 时,就会出现问题,我想在另一个类和另一个线程(GUI)的 JTextPane 上使用 setText() 。这是代码:

gui 类中连接按钮的代码:

private void doConnect(java.awt.event.ActionEvent evt) {                           

server = new mServer(1234);
ms = new mServer(this);
Thread t = new Thread(server);
t.start();
statusLine.setText("Connection established");
}

服务器类:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.nejc;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;

/**
*
* @author Nejc
*/
public class mServer implements Runnable{

private int port;

JMessenger jMsg;

public mServer(int port){

this.port=port;
}

public mServer(JMessenger in){

jMsg = in;
}

public void listen(){

try{

ServerSocket listener = new ServerSocket(port);
Socket server;

while(true){

server = listener.accept();
handleConnection(server);
}

}
catch (IOException ioe){

System.out.println("IOException: " + ioe);
//ioe.printStackTrace();
System.exit(1);
}
}

protected void handleConnection(Socket server) throws IOException{

//DataOutputStream out = new DataOutputStream(server.getOutputStream());
final DataInputStream in = new DataInputStream(server.getInputStream());

Runnable r = new Runnable(){

@Override
public void run(){

try{

jMsg.field.setText("Mitja: " + in.readUTF());
}
catch (IOException ex){

Logger.getLogger(mServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
SwingUtilities.invokeLater(r);


//out.writeUTF(output);

/*JTextPane field = jMsg.field;
String mssg = in.readUTF();
field.setText(mssg);
*
*/

server.setKeepAlive(true);
}

@Override
public void run(){

listen();
}
}

抱歉我忘了这一点:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.nejc.mServer$1.run(mServer.java:70)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

最佳答案

您正在创建两个 mServer 实例 servermsserverjMsgnull所以我希望您会得到 NullPointerExceptionrun方法。

更新

记下 Marko 关于不从 GUI 线程上的输入流读取的答案。

关于Java Messenger(多线程和Swing),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10570749/

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