为什么我在L1类的ds.getPort行获得nullpointerexception?

时间:2010-06-17 11:07:19

标签: java nullpointerexception

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

3 个答案:

答案 0 :(得分:2)

你这样做:

DatagramSocket ds;
Socket socket;

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

这里,ds未初始化,因此为空。 Paper将其传递给L1,因此L1中的ds也为空。

答案 1 :(得分:1)

嗯,你在那里提出了相当多的代码,但你已经得到了这个:

DatagramSocket ds;
Socket socket;

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

引用类型字段的默认值为null,因此您正在有效地调用

new Paper(null)

然后最终会调用

new L1(null)

这就是为什么在L1中调用ds.getPort()会抛出异常。

请注意,在每种情况下都会传递变量的 - 它不像L1中的ds与{{1}中的ds字段相关联}。class。

如果没有详细了解,很难提出一个简单的解决方案 - 但是在您创建Draw之前,可能需要等到创建DatagramSocket

答案 2 :(得分:0)

当您声明Paper p = new Paper(ds);时,它会使用null DatagramSocket初始化Paper。

我认为您要做的是将该行更改为Paper p;,然后在ds = new DatagramSocket(localport);添加p = new Paper(ds);

后立即

如果你想知道,只是一个抬头,Java中常见的误解是:如果你分配new Paper(ds);,你以后更改 ds(不是它的任何实例变量,你实际上改变了整个ds,如:ds = new something),Java中的引用操作并不意味着最初在纸张中使用的ds的实例被改变了。