I am trying to copy large files over a network using sockets and channels. Here is a code snippet

//sender side
FileInputStream in = new FileInputStream(inputFileName);
FileChannel fromFile = in.getChannel();
SocketChannel toSocket = sendSocket.getChannel();
fromFile.transferTo(0, 999999, toSocket );

However, in the line where I call transferTo I get NullPointerException. Any idea why? (I also enclosed it in a while loop as I've been advised, but that's just for robustness and doesn't treat that problem)

Recommended Answers

All 3 Replies

I am trying to copy large files over a network using sockets and channels. Here is a code snippet

//sender side
FileInputStream in = new FileInputStream(inputFileName);
FileChannel fromFile = in.getChannel();
SocketChannel toSocket = sendSocket.getChannel();
fromFile.transferTo(0, 999999, toSocket );

However, in the line where I call transferTo I get NullPointerException. Any idea why? (I also enclosed it in a while loop as I've been advised, but that's just for robustness and doesn't treat that problem)

You code is working. I think sendSocket.getChannel() method return null. Please check the status of socket channel - whether it is available and writable or not.

yeah, I know it should be good....but how do I do that, and how to I fix it???

yeah, I know it should be good....but how do I do that, and how to I fix it???

Please check your firewall settings. I think it blocks the server activities.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.