Problems with juava juices
-
Trying to send a message gets a lot of mistakes. Here's Client:
import java.awt.Color; import java.awt.BorderLayout; import java.awt.event.*; import javax.swing.*;
import java.io.;
import java.net.;class SocketClient extends JFrame implements ActionListener
{JLabel text, clicked; JButton button; JPanel panel; JTextField textField; Socket socket = null; PrintWriter out = null; BufferedReader in = null; SocketClient() { //Начало конструктора text = new JLabel("Текст для отправки через сокет:"); textField = new JTextField(20); button = new JButton("Отправить"); button.addActionListener(this); panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.setBackground(Color.white); getContentPane().add(panel); panel.add("North", text); panel.add("Center", textField); panel.add("South", button); } //Конец конструктора public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if(source == button) { //Передача данных через сокет String text = textField.getText(); System.out.println(text); textField.setText(new String("")); //Получить текст с сервера try { String line = in.readLine(); System.out.println("Text received :" + line); } catch (IOException e) { System.out.println("Read failed"); System.exit(1); } } } public void listenSocket() { //Создать сокет try { socket = new Socket("localhost", 4444); out = new PrintWriter(socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); } catch (UnknownHostException e) { System.out.println("Unknown host: localhost"); System.exit(1); } catch (IOException e) { System.out.println("Nowdced I/O"); System.exit(1); } } public static void main(String[] args) { SocketClient frame = new SocketClient(); frame.setTitle("Client"); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; frame.addWindowListener(l); frame.pack(); frame.setVisible(true); frame.listenSocket(); }
}
Added.
Mistakes:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at SocketClient.actionPerformed(SocketClient.java:50) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(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$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.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$1.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)
-
On my running view of this.
button.addActionListener(this);
The object tries to use itself (not yet created). I'm not even saying that I don't understand what the object of this class is, Freim, the Processor of Events or the Protocol of Sokette.
And there's a code here that's collected. at least 3 classes: GUI, Main and Protocol.