0

I created "client" and "server" in two separate projects. I need to send two arraylists and a buffered image from client to server. For this, I created a serialized class "sending_data" which contains two arraylists and buffered image.

As "sending_data" class is defined only in "client" and not in "server", I included the jar of "client" in "server". Data is successfully send as a "sending_data" object by the client but I am getting problem in extracting two arraylists and buffered image from that received "sending_data" object at the server side. I don't know where i am wrong.

Given below is the code for class "sending_data".

public class sending_data implements Serializable
{
 ArrayList<ArrayList<String>> stringer=new ArrayList<>();
 ArrayList<ArrayList<Integer>> integ=new ArrayList<>();
  transient byte[] client_byte;

 public ArrayList<ArrayList<String>> get_string_arraylist()
 {
     return stringer;
 }
 public ArrayList<ArrayList<Integer>> get_integer_arraylist()
 {
     return integ;
 }
 public byte[] get_buff_array()
 {
     return client_byte;
 }
 public void set_string_arrayList(ArrayList<ArrayList<String>> arr_str)
 {
     stringer=arr_str;
 }
 public void set_integer_arrayList(ArrayList<ArrayList<Integer>> arr_integ)
 {
     integ=arr_integ;
 }
 public void set_byte_array(byte[] cli_byte)
 {
     client_byte=cli_byte;
 }
}

Given below is the code section of sending data from "client" to "server" side:

 String server_name="localhost";
            int port=25000;
            try
            {
                System.out.println("Connecting to " + server_name + " on port " + port);
                Socket client = new Socket(server_name, port);
                System.out.println("Just connected to " + client.getRemoteSocketAddress());
                ObjectOutputStream outToServer = new ObjectOutputStream(client.getOutputStream());
               sending_data sd_=new sending_data();  
                sd_.set_string_arrayList(global);
                sd_.set_integer_arrayList(global_inte);
                sd_.set_byte_array(imageInByte);
                outToServer.writeObject(sd_);
                System.out.println("Data send to server");
                ObjectInputStream inFromServer = new ObjectInputStream(client.getInputStream());
                sen_data=(sending_data)inFromServer.readObject();
                outToServer.close();
                inFromServer.close();
                client.close();
                System.out.println("message send");
            }
            catch(IOException ex)
            {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                 Logger.getLogger(Sender.class.getName()).log(Level.SEVERE, null, ex);
             }

Here is the code for "server" side. I am only showing the code section of declarations and receiving data from "client".

public class Server extends Application {
 String s="";
 ImageView myImageView;
 BufferedImage bbbf;
 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
 Date date = new Date();
 ArrayList<ArrayList<String>> stringer_=new ArrayList<>();
 ArrayList<ArrayList<Integer>> Integg=new ArrayList<>();
 byte[] byter;
Sender.sending_data  sd=null;
@Override
public void start(Stage primaryStage) {
    Text t = new Text(20,20," ");
    t.setFont(Font.font ("Segoe Print", 20));
     myImageView=new ImageView();
    myImageView.setFitHeight(300);
    myImageView.setFitWidth(200);
    Button btn = new Button();
    btn.setText("Start Server");
    btn.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            try
            {
   ServerSocket serverSocket=new ServerSocket(25000);
   System.out.println("Waiting for connection on "+25000);
   Socket socket=serverSocket.accept();
   System.out.println("connected to localhost on port no. "+25000);
   ObjectInputStream serverInputStream = new ObjectInputStream(socket.getInputStream());
   ObjectOutputStream serverOutputStream = new ObjectOutputStream(socket.getOutputStream());
   Object anObj = serverInputStream.readObject();    
   sd= (Sender.sending_data)anObj; 
   byter=new byte[4665600];
   if(sd!=null)
   {
       t.setText("Data recieved at  "+dateFormat.format(date));
   }
   stringer_=sd.get_string_arraylist();
   Integg=sd.get_integer_arraylist();
   byter=sd.get_buff_array();
   for(int i=0;i<20;i++)
   {
       System.out.print(byter[i]+"  ");
   }
   System.out.println("data recieved successfully");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    });


public static void main(String[] args)  {
   launch(args);
}   

}

error i got is:

java.lang.NullPointerException
at server.Server$1.handle(Server.java:80)
at server.Server$1.handle(Server.java:57)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)

I thought the problem is in "server" side at line :

 Sender.sending_data sd=null;

So I tried to solve it by changing the above line to :

Sender.sending_data sd= new Sender.sending_data 

but it shows an error and hint given in netbeans is : an enclosing instance that contains Sender.sending_data is required I am totally confused. Please help me.

  • which is line 80 in Server.java when you got the above stack trace? – Jos Oct 31 '16 at 17:56
  • "Sender.sending_data sd= new Sender.sending_data" should be syntax error without parentheses and semicolon on the first place – hammerfest Oct 31 '16 at 18:01
  • @redflar3 Line 80 : System.out.print(byter[i]+" "); – Sachin Vashistha Oct 31 '16 at 19:39
  • @hammerfest I forgot to put paranthesis and semicolon but after putting them error is not gone. – Sachin Vashistha Oct 31 '16 at 19:39
  • 1
    I'm not sure if this is the first NullPonterException, but `client_byte` is marked as `transient` so it will not be serialized. This will cause a NullPointerException in the server for loop since `byter` will always be null. Also, the nonstandard naming conventions makes this code difficult to read! – Andrew S Oct 31 '16 at 21:04
  • @AndrewS Thanks man, it worked. After removing "transient" I am getting the desired results. But I didn't understand why putting "transient" before "client_byte" led to NullPointerException in the server. – Sachin Vashistha Nov 01 '16 at 04:02

0 Answers0