Socket.accept() throws null pointer exception

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 01:25:55

问题


I am writing a single p2p file sharing program that will accept connections and also serve as server itself.

Its in process but Line: 60

Socket sock1= tcpSocket.accept();

throws a Null pointer Exception and i don't know whats wrong. Tried everything.

import java.net.*;
import java.io.*;


public class echoer implements Runnable {
    int i,backlog;
    public Socket tcpClient= null;
    public ServerSocket tcpSocket= null;
    public echoer(int tcpPort, int udpPort, int backlog) {
        try {
            this.tcpSocket = new ServerSocket(tcpPort,backlog);
            System.out.println("Server connected to "+ InetAddress.getLocalHost() + "on TCP port " + tcpPort + "and UDP port " + udpPort );
            this.backlog= backlog;
            listening();
        }
        catch (SocketTimeoutException s) {
            System.out.println("timeout");
        }
        catch (IOException ioe) {
            System.out.println("could not listen on port 10009");
            System.exit(-1);
        }
}
public echoer () {

}
void listening(){
        try {
                //i++;
                tcpSocket.getInetAddress();
                System.out.println();

                //Thread t1= new Thread((Runnable) new AcceptInput());
                //t1.start();
                //tcpSocket.accept();
                //System.out.println("Connection accepted");
                //messaging();
                Thread t2 = new Thread((Runnable) new echoer());
                t2.start();
            }
            catch (Exception e) {
                System.out.println("Cannot accept connection");
            }
        }

public void Client(String addr, int port) throws IOException 
{
    System.out.println("address= "+ addr+ "port= "+ port);
    tcpClient = new Socket(addr,port);
}
/*void messaging () {
    System.out.println("Starting Thread");
    Thread t = new Thread((Runnable) new echoer());
    t.start();
}*/
public void run() {
    while (true) {
        try {
            //System.out.println("Listening on "+ InetAddress.getLocalHost().getHostAddress() + "on TCP port " + tcpSocket.getLocalSocketAddress());
            Socket sock1= tcpSocket.accept();
            //Client(InetAddress.getLocalHost().getHostAddress(),tcpSocket.getLocalPort());
            System.out.println("Connection accepted");
            ObjectOutputStream out= new ObjectOutputStream(sock1.getOutputStream());
            //Now start the messaging thread nad pass this sock1 to tcpClient
            /*String line;
            System.out.println("Write a message");
            DataInputStream din= new DataInputStream(tcpClient.getInputStream());
            line= din.readUTF();
            if (line == null) {
                din.close();
                tcpClient.close();
                }
            System.out.println("Recvd message:" + line);*/
            if (sock1 != null) {
            tcpSocket.close();
            }
        }
        catch (IOException o) {
            System.out.println("Read Failed");
            }
        }
    }

    /*catch (IOException i) {
        System.out.println("Last statement");
    }
}*/
public static void main(String[] args) {
    new echoer(Integer.parseInt(args[0]),Integer.parseInt(args[1]),5);

}
}

 class AcceptInput implements Runnable {
    String token;
    public void run () {
        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
        try {
            token= br.readLine();
        if (token== "connect" ) {
            System.out.print("Enter IP address: ");
            BufferedReader ip= new BufferedReader(new InputStreamReader(System.in));
            //accept ip and port
            // pass ip and port to tcpclient socket to initiate a connection
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}


回答1:


Here's the current problem, you call new Thread((Runnable) new echoer()) and this starts your thread.

However this calls the empty default constructor for echoer which currently has no actual code in it!

So even though you construct the sockets once, after you do that you just create a new instance of echoer with all new sockets and call run() on that

This means that all the sockets in run are null because they were never set and therefore through a NullPointerException when you try to use them.



来源:https://stackoverflow.com/questions/12831052/socket-accept-throws-null-pointer-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!