Quisiera solicitar su ayuda para resolver un error en java. He estado mirando en el foro algunos problemas similares pero no he encontrado soluciones satisfactorias a mi problema.
El programa es un ejemplo básico de sockets con cliente y servidor.
Sin embargo cada vez que se ejecuta el cliente, al intentar enviar datos genera el siguiente error:
run:
Conectado OK
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at proxy.SocketConnect.writeLine(SocketConnect.java:51)
at main.EnviodeDatos.actionPerformed(EnviodeDatos.java:50)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
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:6539)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6304)
at java.awt.Container.processEvent(Container.java:2239)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.awt.Container.dispatchEventImpl(Container.java:2283)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue$4.run(EventQueue.java:733)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
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)
El programa es el siguiente:
EnviodeDatos (main):
package main;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import proxy.SocketConnect;
import proxy.SocketInter;
import proxy.SocketInter;
import proxy.SocketConnect;
public class EnviodeDatos extends JFrame implements ActionListener {
JTextField textmensaje;
JButton btsend;
/**
*
*/
public EnviodeDatos() {
textmensaje = new JTextField();
textmensaje.setBounds(10, 10, 200, 120);
add(textmensaje);
btsend = new JButton();
btsend.setText("SEND");
btsend.setBounds(10, 135, 120, 20);
btsend.addActionListener((ActionListener) this);
add(btsend);
setLayout(null);
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new EnviodeDatos();
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getSource() == btsend) {
SocketInter socket = (SocketInter) new SocketConnect("127.0.0.1", 8080);
socket.writeLine(textmensaje.getText());
socket.dispose();
}
}
}
interface socket
package proxy;
public interface SocketInter {
String readLine();
void writeLine(String str);
void dispose();
}
socket concreto
package proxy;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketConnect implements SocketInter {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
/**
* @param socket
* @param in
* @param out
*/
public SocketConnect(String host, int port) {
try {
Socket socket = new Socket(host, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
System.out.println("Conectado OK ");
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
@Override
public String readLine() {
String str = null;
try {
str = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
@Override
public void writeLine(String str) {
// TODO Auto-generated method stub
// 4. The wrapper delegates to the target
try {
DataOutputStream data = new DataOutputStream(socket.getOutputStream());
data.writeUTF(str);
System.out.println("Dato enviado");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Dato no enviado: " + e.getMessage());
}
}
@Override
public void dispose() {
// TODO Auto-generated method stub
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
He revisado pero no encuentro problema, igual debo decir que mis conocimientos en java son muy básicos. Les agradecería cualquier ayuda que puedan brindarme.