views:

36

answers:

4

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

public class Draw extends JFrame {

/*
 * Socket stuff
 */
static String host;
static int port;
static int localport;
DatagramSocket ds;
Socket socket;

Draw d;
Paper p = new Paper(ds);


public Draw(int localport, String host, int port) {
    d = this;

    this.localport = localport;
    this.host = host;
    this.port = port;

    try {
        ds = new DatagramSocket(localport);
        InetAddress ia = InetAddress.getByName(host);

        System.out.println("Attempting to connect DatagramSocket. Local port "
                + localport + " , foreign host " + host + ", foreign port " + port + "...");

        ds.connect(ia, port);

        System.out.println("Success, ds.localport: " + ds.getLocalPort()
                    + ", ds.port: " + ds.getPort() + ", address: " + ds.getInetAddress());

        Reciever r = new Reciever(ds);

        r.start();
    } catch (Exception e) {
        e.printStackTrace();
    }

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    getContentPane().add(p, BorderLayout.CENTER);
    setSize(640, 480);
    setVisible(true);
}

public static void main(String[] args) {

    int x = 0;
    for (String s : args){
        if (x==0){
            localport = Integer.parseInt(s);
            x++;
        }
        else if (x==1){
            host = s;
            x++;
        }
        else if (x==2){
            port = Integer.parseInt(s);
        }
    }
    Draw d = new Draw(localport, host, port);
}

}

class Paper extends JPanel {

DatagramSocket ds;

private HashSet hs = new HashSet();

public Paper(DatagramSocket ds) {
    this.ds=ds;
    setBackground(Color.white);
    addMouseListener(new L1(ds));
    addMouseMotionListener(new L2());
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.black);
    Iterator i = hs.iterator();
    while(i.hasNext()) {
        Point p = (Point)i.next();
        g.fillOval(p.x, p.y, 2, 2);
    }
}

private void addPoint(Point p) {
    hs.add(p);
    repaint();
}

class L1 extends MouseAdapter {

    DatagramSocket ds;

    public L1(DatagramSocket ds){
        this.ds=ds;
    }

    public void mousePressed(MouseEvent me) {







        addPoint(me.getPoint());
        Point p = me.getPoint();
        String message = Integer.toString(p.x) + " " + Integer.toString(p.y);
        System.out.println(message);



        try{

            byte[] data = message.getBytes("UTF-8");

            //InetAddress ia = InetAddress.getByName(ds.host);

            String convertedMessage = new String(data, "UTF-8");

            System.out.println("The converted string is " + convertedMessage);

            DatagramPacket dp = new DatagramPacket(data, data.length);

            System.out.println(ds.getPort());
            //System.out.println(message);
            //System.out.println(ds.toString());
            //ds.send(dp);

            /*System.out.println("2Sending a packet containing data: " +data +" to "
                        + ia + ":" + d.port + "...");*/
        } catch (Exception e){
            e.printStackTrace();

        }
    }
}

class L2 extends MouseMotionAdapter {
    public void mouseDragged(MouseEvent me) {
        addPoint(me.getPoint());
        Point p = me.getPoint();
        String message = Integer.toString(p.x) + " " + Integer.toString(p.y);
        //System.out.println(message);
    }
}

}

class Reciever extends Thread{

DatagramSocket ds;
byte[] buffer;

Reciever(DatagramSocket ds){
    this.ds = ds;
    buffer = new byte[65507];   
}

public void run(){
    try {
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
        while(true){
            try {
                ds.receive(packet);
                String s = new String(packet.getData());
                System.out.println(s);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

+1  A: 

Well, you've presented quite a mess of code there, but you've got this:

DatagramSocket ds;
Socket socket;

Draw d;
Paper p = new Paper(ds);

The default value for reference type fields is null, so you're effectively calling

new Paper(null)

That will then end up calling

new L1(null)

which is why calling ds.getPort() in L1 is throwing an exception.

Note that the value of the variable is being passed in each case - it's not like the ds within L1 is associated with the ds field in the Draw class.

Without looking in a lot more detail, it's hard to suggest an easy fix - but it's likely to involve waiting until you've created the DatagramSocket before you create the Paper.

Jon Skeet
+2  A: 

You do this:

DatagramSocket ds;
Socket socket;

Draw d;
Paper p = new Paper(ds);

Here, ds is uninitialized, thus null. Paper passes it to L1, thus the ds in L1 is also null.

Sjoerd
A: 

When you declare Paper p = new Paper(ds); it initializes the Paper with a null DatagramSocket.

I think what you want to do is to change that line to Paper p; then right after ds = new DatagramSocket(localport); add p = new Paper(ds);

Just a heads up in case you're wondering, a common misconception in Java is that: if you assign new Paper(ds); and you later change ds (not any of its instance variables, you actually change the whole of ds as in: ds = new something), reference manipulation in Java does not mean the instance of ds initially used in paper is changed.

msakr
A: 

I know the code is a big mess, im just playing around trying to learn UDP.

The problem was passing null to the paper constructor. Thank you for your time and help.

Fred
This is a comment, not an answer. This is why answer comments exist. You should also pick a correct answer.
msakr