正如youtube上的视频所示,该场景
http://youtu.be/5OwXqnZ64rE
虽然服务器聊天窗口确实发送了消息,而客户端获取消息并显示消息,但它并没有相反的工作方式。
以下是三个类:
Server:
package backend;
import frontend.Gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Natalochka
*/
public class Server implements ActionListener, Runnable {
private ServerSocket ss;
private Socket s;
private ObjectOutputStream oos;
private ObjectInputStream ois;
private Gui servertalkstogui;
public Server(Gui in ) {
servertalkstogui = in;
}
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == servertalkstogui.getCreate()){
servertalkstogui.getAreachat().append("This is the server: " + "\n");
Thread t = new Thread(this);
t.start();
}
if(ae.getSource() == servertalkstogui.getButton()){
String linea = servertalkstogui.getTextField().getText();
servertalkstogui.getTextField().setText("");
this.writeLine(linea);
}
}
@Override
public void run() {
try {
ss = new ServerSocket(9999);
s = ss.accept();
oos = new ObjectOutputStream(s.getOutputStream());
ois = new ObjectInputStream(s.getInputStream());
this.readLine();
} catch (IOException e) {
try {
this.closeServer();
} catch (IOException ex) {
System.out.println("se jodio");
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
e.getLocalizedMessage();
} catch (InterruptedException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void writeLine(String linea){
try {
oos.writeObject(linea);
servertalkstogui.getAreachat().append("\n I say: " + linea);
} catch (IOException e) {
e.getLocalizedMessage();
}
}
public void readLine() throws InterruptedException{
try {
while(true){
Object aux = ois.readObject();
if(aux != null && aux instanceof String){
servertalkstogui.getAreachat().append("Client says: " + (String)aux + "\n");
}
}
} catch (IOException | ClassNotFoundException e) {
}
}
public void closeServer() throws IOException{
try {
oos.close();
s.close();
ss.close();
} catch (Exception e) {
e.addSuppressed(e);
}
}
}
CLIENT
package backend;
import frontend.Gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Natalochka
*/
public class Client implements ActionListener, Runnable {
private Socket s;
private ObjectOutputStream oos;
private ObjectInputStream ois;
private Gui clienttalkstogui;
public Client(Gui in){
clienttalkstogui = in;
}
@Override
public void actionPerformed(ActionEvent ae) {
// to connect to the socket that the Server opened
if(ae.getSource() == clienttalkstogui.getConnect()){
Thread t = new Thread(this);
t.start();
}
// to obtain and send whatever was typed in the text field
if(ae.getSource() == clienttalkstogui.getButton()){
String linea = clienttalkstogui.getTextField().getText();
this.writeLine(linea);
}
}
@Override
public void run() {
try {
s = new Socket("localhost", 9999);
oos = new ObjectOutputStream(s.getOutputStream());
ois = new ObjectInputStream(s.getInputStream());
clienttalkstogui.getAreachat().append("Conected to port" + "\n");
this.readLine();
} catch (IOException | InterruptedException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void writeLine(String linea){
try {
// to send it to the server
oos.writeObject(linea);
// to have it displayed on the client window too
clienttalkstogui.getAreachat().append(linea);
} catch (IOException e) {
//this.closeClient();
e.getLocalizedMessage();
}
}
public void readLine() throws InterruptedException{
try {
while(true){
Object aux = ois.readObject();
if(aux != null && aux instanceof String){
clienttalkstogui.getAreachat().append("Server says: " + (String)aux + "\n");
}
}
} catch (IOException | ClassNotFoundException e) {
}
}
/*
public void closeClient()
{
try {
ois.close();
oos.close();
s.close();
} catch (Exception e) {
e.getLocalizedMessage();
}
}
*/
}
GUI
package frontend;
import backend.Client;
import backend.Server;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
*
* @author Natalochka
*/
public class Gui extends JFrame{
private JMenuBar bar;
private JMenu menu;
private JMenuItem connect, create, exit;
private JTextArea areachat;
private JTextField campochat;
private JButton botonchat;
private JScrollPane scroll;
/*WE CREATE INSTANTIATED OBJECTS OF CLASSES INTERACTING WITH THE GUI*/
Server servidor = new Server(this);
Client cliente = new Client(this);
/*CREATING THE CONSTRUCTOR*/
public Gui(){
super("CHAT WINDOW");
this.setSize(400, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*INSTANTIATE THE OBJECTS*/
bar = new JMenuBar();
menu = new JMenu("Menu");
connect = new JMenuItem("Conectar");
create = new JMenuItem("Crear");
exit = new JMenuItem("Salir");
areachat = new JTextArea();
campochat = new JTextField(20);
botonchat = new JButton("Send");
scroll = new JScrollPane(areachat);
/*THE BAR IS PLACED IN THE JFRAME WINDOW*/
this.setJMenuBar(bar);
/*THE MENU IS ADDED TO THE BAR*/
bar.add(menu);
/*THE ITEMS ARE ADDED TO THE MENU*/
menu.add(connect);
menu.add(create);
menu.add(exit);
/*MAKE ITEMS LISTEN TO THE EVENT FROM THE CODE CLASSES*/
create.addActionListener(servidor);
connect.addActionListener(cliente);
exit.addActionListener(servidor);
exit.addActionListener(cliente);
botonchat.addActionListener(cliente);
botonchat.addActionListener(servidor);
/*CREATING THE LAYOUTS*/
/*AREACHAT*/
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
this.add(scroll, gbc);
/*TEXTFIELD*/
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.weighty = 0;
this.add(campochat,gbc);
/*BOTON*/
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0;
gbc.weighty = 0;
this.add(botonchat, gbc);
this.setVisible(true);
}
/*CREATING THE GETTERS AND SETTERS*/
/*GETTERS*/
public JTextArea getAreachat(){
return areachat;
}
public JMenuItem getCreate(){
return create;
}
public JMenuItem getConnect(){
return connect;
}
public JTextField getTextField(){
return campochat;
}
public JButton getButton(){
return botonchat;
}
/*SETTERS*/
public static void main(String[] args) throws InterruptedException {
Gui objeto = new Gui();
}
}
这是错误日志
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at backend.Server.writeLine(Server.java:80)
at backend.Server.actionPerformed(Server.java:48)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
BUILD STOPPED (total time: 58 seconds)
答案 0 :(得分:1)
我重写了我的全部答案:
您的代码有几个实现缺陷,导致难以阅读,调试和生成错误。
您获得的NullPointerException
是因为您在ActionListener
按钮上设置了两个send
(Client
的监听器和Server
的监听器):
botonchat.addActionListener(cliente);
botonchat.addActionListener(servidor);
因此,无论您是仅为Server
还是Client
设置变量,都会调用它们,一个会有一些null
个变量 - > NullPointerException
。
快速(和脏)修复是在使用之前检查变量oos
是否已设置,并且仅在读取数据时清除JTextField
:
//Server
public void writeLine(String linea)
{
try
{
if (this.oos != null)
{
oos.writeObject(linea);
servertalkstogui.getTextField().setText("");//clearing JTextField
servertalkstogui.getAreachat().append("\n I say: " + linea);
}
}
catch (IOException e)
{
e.getLocalizedMessage();
}
}
//Client
public void writeLine(String linea)
{
try
{
if (this.oos != null)
{
// to send it to the server
oos.writeObject(linea);
clienttalkstogui.getTextField().setText("");//clear the text when it have been read
// to have it displayed on the client window too
clienttalkstogui.getAreachat().append(linea);
}
}
catch (IOException e)
{
// this.closeClient();
e.getLocalizedMessage();
}
}
删除JTextField
中actionPerformed
的清除:
//Server
@Override
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == servertalkstogui.getCreate()/* .getButton() */)
{
servertalkstogui.getAreachat().append("This is the server: " + "\n");
Thread t = new Thread(this);
t.start();
}
if (ae.getSource() == servertalkstogui.getButton())
{
String linea = servertalkstogui.getTextField().getText();
//servertalkstogui.getTextField().setText("");// do not clear the text here ! You don't know if it will be read yet.
this.writeLine(linea);
}
}
这项工作没有任何Exception
。
一个好的做法是将Client
和Server
的代码分开:没有理由在Gui
中实例化。