error

+1 Pavithra Mathivanan · February 4, 2015
I get the following error at the end of this program "The serializable class Server does not declare a static final serialVersionUID field of type long" in both client and server class,how do i correct it.

Post a Reply

Replies

Oldest  Newest  Rating
0 Pavithra Mathivanan · February 9, 2015
this is my code..couldnt find wats wrong..:(


SERVER PROGRAM:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Server extends JFrame {

/**

*/
private static final long serialVersionUID = 1L;
private JTextField typeBox;
private JTextArea chatBox;
private ServerSocket server;
private Socket connection;
private ObjectOutputStream output;
private ObjectInputStream input;

public Server(){

super("Pavithra's Instant Messenger");
typeBox=new JTextField();
typeBox.setEditable(false);
typeBox.addActionListener(  
new ActionListener(){
public void actionPerformed(ActionEvent event){
sendMessage(event.getActionCommand());
typeBox.setText(" ");
}
}   
);
add(typeBox,BorderLayout.NORTH);
add(new JScrollPane(chatBox),BorderLayout.SOUTH);
setVisible(true);
setSize(300,400);

}

public void startRunning() {
try{
server= new ServerSocket(6749,100);
while(true){
try{
waitForConnection();
setUpStreams();
whileChatting();}
catch(EOFException eof){
showMessage("\n SERVER- ended the connection");
      }
}
}catch(IOException ioe){
ioe.printStackTrace();}

finally{
closeall();
}

}

public void waitForConnection() throws IOException{
showMessage("Waiting for someone to connect");
connection= server.accept();
showMessage("\n Connected to "+ connection.getInetAddress().getHostName());
}

public void setUpStreams() throws IOException{
output=new ObjectOutputStream(connection.getOutputStream());
output.flush();
input=new ObjectInputStream(connection.getInputStream());
showMessage("\n Streams are set up!");

}

public void whileChatting() throws IOException{
String message="You are now connected!";
sendMessage("\n"+message);
ableToType(true);
do{
try{
message=(String) input.readObject();
showMessage(message);
} catch(ClassNotFoundException cnf){
cnf.printStackTrace();
}

}while(!message.equals("CLIENT-END"));
}

private void closeall(){
showMessage("\n Connections are ending");
ableToType(false);
try{
output.close();
input.close();
connection.close();}
catch(IOException e){
e.printStackTrace();
}

}

private void sendMessage(String message){
try{
output.writeObject("\n SERVER -"+message);
output.flush();
showMessage("\n SERVER -"+message);
}
catch(IOException eo){
chatBox.append("\n something went wrong cant send that msg nw");
}
}

private void showMessage(final String m){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
chatBox.append(m);
}
}
);
}
private void ableToType(final boolean tof){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
chatBox.setEditable(tof);
}
}
);
}

}

SERVER MAIN PROGRAM:

import javax.swing.JFrame;

public class ServerTest {
public static void main(String[] args){
Server s=new Server();
s.startRunning();
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}
CLIENT PROGRAM:

import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.net.*;

public class Clients extends JFrame {


/**

*/
private static final long serialVersionUID = 1L;
private JTextField typeBox;
private JTextArea chatBox;
private Socket connection;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message="";
private String serverIP;

public Clients(String host){

super("Shwasthika's Instant Messenger");
serverIP=host;
typeBox=new JTextField();
typeBox.setEditable(false);
typeBox.addActionListener(  
new ActionListener(){
public void actionPerformed(ActionEvent event){
sendMessage(event.getActionCommand());
typeBox.setText(" ");
}
}   
);
add(typeBox,BorderLayout.NORTH);
add(new JScrollPane(chatBox),BorderLayout.SOUTH);
setVisible(true);
setSize(300,400);

}

public void startRunning() {
       try{
ConnectToServer();
setUpStreams();
whileChatting();}
catch(EOFException eof){
showMessage("\n SERVER- ended the connection");
      }
   catch(IOException ioe){
       ioe.printStackTrace();}
finally{
closeall();
}
}



public void ConnectToServer() throws IOException{
showMessage("Attempting to connect");
connection= new Socket(InetAddress.getByName(serverIP),6749);
showMessage("\n Connected to "+connection.getInetAddress().getHostName() );
}

public void setUpStreams() throws IOException{
output=new ObjectOutputStream(connection.getOutputStream());
output.flush();
input=new ObjectInputStream(connection.getInputStream());
showMessage("\n Streams are set up!");

}

public void whileChatting() throws IOException{

ableToType(true);
do{
try{
message=(String) input.readObject();
showMessage("\n" + message);
} catch(ClassNotFoundException cnf){
cnf.printStackTrace();
}
}while(!message.equals("SERVER-END"));
}

private void closeall(){
showMessage("\n Connections are ending");
ableToType(false);
try{
output.close();
input.close();
connection.close();}
catch(IOException e){
e.printStackTrace();
}

}

private void sendMessage(String message){
try{
output.writeObject("\n CLIENT -"+message);
output.flush();
showMessage("\n CLIENT -"+message);
}
catch(IOException eo){
chatBox.append("\n something went wrong cant send that msg nw");
}
}

private void showMessage(final String m){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
chatBox.append(m);
}
}
);
}
private void ableToType(final boolean tof){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
chatBox.setEditable(tof);
}
}
);
}

}


CLIENT MAIN PROGRAM:

import javax.swing.JFrame;

public class ClientTest {
public static void main(String[] args){
Clients c;
c=new Clients("127.0.0.1");
c.startRunning();
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}
0 Pavithra Mathivanan · February 5, 2015
thank u!..tried it and that worked..but I am getting all these following errors,

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Server$2.run(Server.java:118)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(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)

can you please help me..i followed the same code as in the tutorials..
  • 1

Java / Android Development

107,018 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator