Results 1 to 6 of 6
  1. #1
    TechNickL is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default 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:
    Java 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!
    Last edited by TechNickL; 07-07-2011 at 12:49 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    It is impossible for the indicated line to throw a NullPointerException. All it is doing is calling a static method in the CharBuffer class. copy and paste the full and exact error message.

  3. #3
    TechNickL is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    copy and paste the full and exact error message.
    Error on serverside:
    Java Code:
    Exception in thread "main" java.io.IOException: An existing connection was forcibly closed by the remote host
    	at sun.nio.ch.SocketDispatcher.write0(Native Method)
    	at sun.nio.ch.SocketDispatcher.write(Unknown Source)
    	at sun.nio.ch.IOUtil.writeFromNativeBuffer(Unknown Source)
    	at sun.nio.ch.IOUtil.write(Unknown Source)
    	at sun.nio.ch.SocketChannelImpl.write(Unknown Source)
    	at chat.App.startChat(App.java:48)
    	at chat.App.<init>(App.java:24)
    	at chat.MyChat.main(MyChat.java:9)
    Error on clientside:
    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at chat.App.startChat(App.java:85)
    	at chat.App.<init>(App.java:24)
    	at chat.MyChat.main(MyChat.java:9)

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    The error message says the exception occured on line 85 which is
    Java Code:
    decoder.decode(bb, cb, false);
    and not the one you claimed. So on that line "decoder", "bb" or "cb" could be false. Add one or more print statements before this line to see which variable is null. Then trace back to see why it is null.

  5. #5
    TechNickL is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    I know its cb. When I place
    Java Code:
    print("doing good")
    in between every line it prints those right up until the line indicated in the first post, which i assume is because it calls cb.


    Last edited by TechNickL; 07-07-2011 at 01:13 AM.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    That is not debugging it correctly. What I meant was try something like:
    Java Code:
    System.out.println("decoder is " + decoder);
    This will identify which of your variables is null. Otherwise you will get something like Classname@hexstring

Similar Threads

  1. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 11:12 AM
  2. JAR problems
    By Evil Smurf in forum New To Java
    Replies: 4
    Last Post: 09-01-2009, 01:17 AM
  3. Problems, problems, problems!
    By Empower5315 in forum New To Java
    Replies: 2
    Last Post: 08-28-2009, 04:27 AM
  4. Few Problems Help!
    By elad_bj in forum New To Java
    Replies: 4
    Last Post: 04-07-2009, 03:45 AM
  5. write nio.CharBuffer to file
    By Beju in forum New To Java
    Replies: 5
    Last Post: 10-19-2008, 08:20 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •