×

Discussion Board

Results 1 to 3 of 3

Thread: need help

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    need help

    i want to update the code in bellow to be able to send image from computer to mobile using tcp socket
    i add a code to the "method save()" that should save the image in hard disk an then send it by socket to mobile
    it give me error Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException in BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));


    could you help me
    with best regard



    the method updated is

    public void save(){
    try{
    ImageIO.write(img, "jpg", new File("dynamics.jpg"));
    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    wr.write("image_sending");
    wr.flush();}
    catch(IOException ioe){
    System.err.println("write: " + ioe.getMessage());}
    }


    the code


    public static void main(String args[]){
    try {ServerSocket serverSocket = new ServerSocket(12344);
    toClient = serverSocket.accept();
    Thread desktopServerThread = new Thread(){
    public void run(){
    while(true){
    try {
    in= new BufferedReader(new InputStreamReader(toClient.getInputStream()));
    String str;

    if (!(str=in.readLine()).equals("") | !str.equals(" "))
    // output.write(str.getBytes());
    JFrame frame = new JFrame("Image Label");
    BufferedImage img = null;
    Main tt = new Main(img,"");
    tt.dbTest();
    String imageFileName = "map.png";
    img = ImageIO.read(new File(imageFileName));
    frame.add("Center", new Main(img, ""));

    frame.pack();
    frame.setVisible(true);

    }
    } catch(Exception e) {
    System.out.println("S: Error");
    e.printStackTrace();
    break;
    }
    }
    }
    };
    desktopServerThread.start();

    } catch (Exception e) {
    System.out.println("S: Connection Error");
    e.printStackTrace();
    }

    }

    BufferedImage img;
    String text;
    Font font;


    public Main(BufferedImage img, String text) {

    this.img = img;
    this.text = text;
    font = new Font("Serif", Font.PLAIN, 36);

    }

    public void paint(Graphics g) {

    g=img.getGraphics();
    int[] x= Main.x;
    int[][] A=Main.A;


    Dimension size = getSize();
    g.drawImage(img, 0, 0, 800, 600, this);

    g.setColor(Color.BLACK);
    g.setFont(font);
    FontRenderContext frc = ((Graphics2D) g).getFontRenderContext();
    Rectangle2D bounds = font.getStringBounds(text, frc);

    save();
    }

    public void save(){
    try{ImageIO.write(img, "jpg", new File("dynamics.jpg"));}
    catch(IOException ioe){System.err.println("write: " + ioe.getMessage());}
    }[/SIZE][/SIZE][/SIZE]

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: need help

    Your code is all desktop Java, not mobile... you might be better to ask on a regular Java forum.

    Quote Originally Posted by souhir View Post
    it give me error Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException in BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    This line of code will throw a NullPointerException if socket is null.

    Graham.

  3. #3
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Re: need help

    If you are planning to run the code you wrote on mobile devices the you are doing the wrong thing.Please write the code in Java ME.Please clear this thing.
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×