I'm creating a Client/Server program that will send a serializable object back and forth. i have no idea how that nullpointer exception occurs it points to the line where the btnSave responds to the Click event, and the OutputStream object tries to write
only this feature is implemented now, in case anyone tests the code.
Serializable object:
import java.io.Serializable;
public class Contact implements Serializable {
private String Name;
private String Phone;
private Operation operation;
public Contact(String n, String p, Operation op){
this.operation = op;
this.Name=n;
this.Phone = p;
}
public String toString(){
return this.Name + this.Phone;
}
protected enum Operation{
save,
read
}
}
Client Code:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client extends JFrame {
private JTextField txtName = new JTextField();
private JTextField txtPhone = new JTextField();
private JLabel lblName = new JLabel(" Name ");
private JLabel lblPhone = new JLabel(" Phone ");
private JButton btnSave = new JButton(" Save ");
private JButton btnRead = new JButton(" Read ");
private JPanel pan = new JPanel();
private JTextArea jta = new JTextArea(5,5);
private ObjectOutputStream Send;
private ObjectInputStream Receive;
public static void main(String[] args) {
new Client();
}
public Client() {
btnSave.addActionListener(new Listener() );
pan.setLayout(new GridLayout(2,3));
pan.add(lblName);
pan.add(txtName);
pan.add(btnSave);
pan.add(lblPhone);
pan.add(txtPhone);
pan.add(btnRead);
setLayout(new BorderLayout());
add(pan, BorderLayout.NORTH);
add(new JScrollPane(jta), BorderLayout.SOUTH);
setTitle("Client");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try {
Socket socket = new Socket("localhost", 8000);
Receive = new ObjectInputStream(socket.getInputStream());
Send = new ObjectOutputStream(socket.getOutputStream());
}
catch (IOException ex) {
jta.append(ex.toString() + '\n');
}
}
private class Listener implements ActionListener {
public void actionPerformed(ActionEvent e){
Contact c;
if (e.getSource()==btnSave){
c = new Contact(txtName.getText(), txtPhone.getText(), Contact.Operation.save);
}
else
{
c = new Contact(txtName.getText(), txtPhone.getText(), Contact.Operation.read);
}
try{
Send.writeObject(c);
Send.flush();
/*
* try{
Contact x = (Contact)Receive.readObject();
JOptionPane.showMessageDialog(null, c.toString());
}
catch(ClassNotFoundException excee)
{}
*/
}
catch(IOException exc)
{
}
}
}
}
the server is :
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;
import java.awt.*;
import javax.swing.*;
public class PhoneServer extends JFrame {
// Text area for displaying contents
private JTextArea jta = new JTextArea();
public static void main(String[] args) {
new PhoneServer();
}
public PhoneServer() {
setLayout(new BorderLayout());
add(new JScrollPane(jta), BorderLayout.CENTER);
setTitle("MultiThreadServer");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try {
ServerSocket serverSocket = new ServerSocket(8000);
jta.append("Phone Server started at " + new Date() + '\n');
int clientNo = 1;
while (true) {
Socket socket = serverSocket.accept();
InetAddress inetAddress = socket.getInetAddress();
jta.append("New Client: #" + clientNo + " From: "+inetAddress.getHostAddress()+'\n');
HandleAClient task = new HandleAClient(socket);
new Thread(task).start();
clientNo++;
}
}
catch(IOException ex) {
System.err.println(ex);
}
}
class HandleAClient implements Runnable {
private Socket socket;
public HandleAClient(Socket socket) {
this.socket = socket;
}
public void run() {
Matcher matcher;
try {
ObjectInputStream inputFromClient = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream outputToClient = new ObjectOutputStream(socket.getOutputStream());
while (true) {
try{
Contact x = (Contact)inputFromClient.readObject();
JOptionPane.showMessageDialog(null, x.toString());
}
catch(ClassNotFoundException excr){}
//outputToClient.write(("Phone is " + phone + " and name is " + name).getBytes());//dataArray);
//outputToClient.write(data.getBytes());
}
}
catch(IOException e) {
System.err.println(e);
}
}
}
}
ERROR:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Client$Listener.actionPerformed(Client.java:76)
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.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(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.AccessControlContext$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)