0

I am attempting to send an image over Socket in Java however I get a NullPointerException. Here is the code for server:

package sendImage;


import java.net.*;
import javax.imageio.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class Server {
    ServerSocket server;
    Socket client;
//------------------------------------------------------------------------------
    public static void main(String[] args) {
        new Server();
    }
//------------------------------------------------------------------------------
    public Server(){
        connect();
    }
//------------------------------------------------------------------------------
    public void connect(){
        try{
            server = new ServerSocket(9999);
            client = server.accept();

            sendImage();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
//------------------------------------------------------------------------------
    public void sendImage(){
        String image = "C:/Users/JavaChild/Downloads/jo-d-101027-mentalist.jpg";
        try{
            InputStream in = Server.class.getResourceAsStream(image);
            BufferedImage img = ImageIO.read(in);
            JLabel l = new JLabel(new ImageIcon(img));
            JFrame frame = new JFrame("Server");
            frame.add(l);
            frame.setVisible(true);
            ImageIO.write(img, "JPG", client.getOutputStream());
        }catch(Exception e){
            e.printStackTrace();
        }
    }
//------------------------------------------------------------------------------
}  

and Client:

package sendImage;

import java.net.*;
import javax.imageio.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import java.awt.image.BufferedImage;
import java.io.*;


public class Client {
    Socket server;

//------------------------------------------------------------------------------
    public static void main(String[] args) {
        new Client();
    }
//------------------------------------------------------------------------------
    public Client(){
        connect();
    }
//------------------------------------------------------------------------------
    public void connect(){
        try{
            server = new Socket("JavaChild-PC",9999);
            readImage();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
//------------------------------------------------------------------------------
    public void readImage(){
        try{
            BufferedImage img = ImageIO.read(server.getInputStream());
            JLabel label = new JLabel(new ImageIcon(img));
            JFrame frame = new JFrame("Client");
            frame.add(label);
            frame.setVisible(true);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}  

Please tell me how to send an image properly over the socket
and what is causing the exception?

Stack trace

java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at sendImage.Client.readImage(Client.java:37)
    at sendImage.Client.connect(Client.java:28)
    at sendImage.Client.<init>(Client.java:22)
    at sendImage.Client.main(Client.java:18)
An SO User
  • 24,612
  • 35
  • 133
  • 221
  • 1
    Can you post the stack trace? – Dan W Mar 21 '13 at 19:31
  • @DanW done! The stack trace has been posted! :) – An SO User Mar 21 '13 at 19:33
  • What line is line 37 exactly? – christopher Mar 21 '13 at 19:33
  • @ChrisCooney `JLabel label = new JLabel(new ImageIcon(img));` I guess the image is not being sent properly so I am getting a null there – An SO User Mar 21 '13 at 19:34
  • 1
    I would guess that `img` is null in that case. Have you tried debugging to see if you're properly reading in the image? – Dan W Mar 21 '13 at 19:36
  • Exception in Server at `InputStream in = Server.class.getClassLoader().getResourceAsStream(image); BufferedImage img = ImageIO.read(in);` it says **input==null** – An SO User Mar 21 '13 at 19:40
  • 1
    Have you tried `InputStream in = new FileInputStream(image);`? – Pshemo Mar 21 '13 at 19:41
  • @Pshemo OK, worked !!! – An SO User Mar 21 '13 at 19:42
  • @Pshemo You should add that as an answer so poster can accept and people know this is answered. – Dan W Mar 23 '13 at 18:03
  • @DanW In my opinion beside giving solution answer should also explain problem. I'm not sure why OP code didn't work so decided to post other solution as comment. If you can explain why code from my comment worked but `Server.class.getResourceAsStream` not feel free to post this answer and you will definitely get +1 from me :) – Pshemo Mar 23 '13 at 18:24

0 Answers0