Why do I get a nullpointerexception at line ds.getPort in class L1?

Posted by Fred on Stack Overflow See other posts from Stack Overflow or by Fred
Published on 2010-06-17T11:07:19Z Indexed on 2010/06/17 11:13 UTC
Read the original article Hit count: 299

Filed under:
|

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();
    }
}

}

© Stack Overflow or respective owner

Related posts about java

Related posts about nullpointerexception