Results 1 to 6 of 6
Thread: Having problems with CharBuffer
- 07-07-2011, 12:43 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
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:
Thankful for any help!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(); } } } }Last edited by TechNickL; 07-07-2011 at 12:49 AM.
- 07-07-2011, 12:49 AM #2
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.
- 07-07-2011, 12:52 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
Error on serverside:
Error on clientside: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)
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)
- 07-07-2011, 12:56 AM #4
The error message says the exception occured on line 85 which is
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.Java Code:decoder.decode(bb, cb, false);
- 07-07-2011, 01:00 AM #5
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
I know its cb. When I place
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.Java Code:print("doing good")
Last edited by TechNickL; 07-07-2011 at 01:13 AM.
- 07-08-2011, 12:36 AM #6
Similar Threads
-
InputStream/Jar Problems/File IO Problems
By rdjava in forum Advanced JavaReplies: 31Last Post: 01-17-2011, 11:12 AM -
JAR problems
By Evil Smurf in forum New To JavaReplies: 4Last Post: 09-01-2009, 01:17 AM -
Problems, problems, problems!
By Empower5315 in forum New To JavaReplies: 2Last Post: 08-28-2009, 04:27 AM -
Few Problems Help!
By elad_bj in forum New To JavaReplies: 4Last Post: 04-07-2009, 03:45 AM -
write nio.CharBuffer to file
By Beju in forum New To JavaReplies: 5Last Post: 10-19-2008, 08:20 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks