Having problems with CharBuffer
I am trying to create a command line chat client, but my server keeps returning NullPointerException error when it gets to the CharBuffer. They connect to each other fine. It's the sending that has trouble.
Here's the code:
Code:
package chat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;
import java.util.Scanner;
import java.net.ServerSocket;
public class App {
public App() throws java.io.IOException {
this.startChat();
}
public static void print(String msg){
System.out.println(msg);
}
public static void startChat() throws java.io.IOException {
Scanner input = new Scanner(System.in);
CharsetEncoder encoder = Charset.forName("US-ASCII").newEncoder();
print("Select server/client");
String text = input.nextLine();
if(text.equals("server")){
//==================================================================
// SERVER
//==================================================================
print("Input port.");
int port = input.nextInt();
ServerSocketChannel server = ServerSocketChannel.open();
server.socket().bind(new java.net.InetSocketAddress(port));
for(;;){
print("waiting for connection...");
SocketChannel client = server.accept();
print("Connected");
client.write(encoder.encode(CharBuffer.wrap("test txt")));
}
}
else if(text.equals("client")){
//==================================================================
//CLIENT
//==================================================================
Socket conSocket;
PrintWriter out;
BufferedReader in;
print("Input IP/hostname");
String ip = input.nextLine();
print("Input Port");
int serport = input.nextInt();
try {
conSocket = new Socket(ip, serport);
out = new PrintWriter(conSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(conSocket.getInputStream()));
} catch (UnknownHostException e) {
print("Couldn't find host: " + ip);
System.exit(1);
} catch (IOException e) {
print("Couldn't get I/O for the connection to: " + ip);
System.exit(1);
}
//SocketChannel socket = SocketChannel.open(new InetSocketAddress(ip,serport));
print("Connected");
ReadableByteChannel source = null;
Charset charset = Charset.forName("US-ASCII");
CharsetDecoder decoder = charset.newDecoder();
ByteBuffer bb = ByteBuffer.allocateDirect(2048);
//HERES THE PROBLEM LINE BELOW: THIS ALWAYS RETURNS AN ERROR
CharBuffer cb = CharBuffer.allocate(2048);
decoder.onMalformedInput(CodingErrorAction.IGNORE);
decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
decoder.reset();
while(source.read(bb) != -1){
bb.flip();
decoder.decode(bb, cb, false);
cb.flip();
System.out.print(cb);
cb.clear();
bb.compact();
}
}
}
}
Thankful for any help!