• NullPointerException... god damn it! (java)
    10 replies, posted
I'm getting a null pointer exception, and i have no idea why. There really isnt much more to say about this, so here. Error: [code] Exception in thread "main" java.lang.NullPointerException at java.nio.HeapByteBuffer.get(HeapByteBuffer.java:143) at ClientNetwork.WranglePacket(ClientNetwork.java:50) at ClientNetwork.ExtractPacket(ClientNetwork.java:35) at ClientNetwork.Read(ClientNetwork.java:25) at Client.run(Client.java:29) at java.lang.Thread.run(Thread.java:636) at Server.ListenForConnection(Server.java:28) at Server.<init>(Server.java:13) at Server.main(Server.java:34) [/code] And the class file that is causing it: It starts in WranglePacket here: [code] case 0x02: short Length = inbuffer.getShort(); System.out.println("Got a length: "+Length+","+inbuffer.position()); if(Length>0){ byte[] str = null; inbuffer.get(str, 0, Length); //line 50 String string = new String(str,"UTF8"); System.out.println("Aha! Got a username: "+string); username = string; } else{ System.out.println("False alarm!"); } [/code] The rest of the file: [code] import java.io.*; import java.net.*; import java.nio.ByteBuffer; public class ClientNetwork{ Socket sock; int ID; String username; ByteBuffer inbuffer; ByteBuffer outbuffer; ClientNetwork(Socket soc, int id){ sock = soc; ID = id; inbuffer = ByteBuffer.allocate(4096); outbuffer = ByteBuffer.allocate(4096); InitPackets(); } public void InitPackets(){ new Handshake(); } public void Read(byte[] data) throws UnsupportedEncodingException{ inbuffer.put(data); System.out.println("Reading in "+data.length+" bytes of data."); ExtractPacket(); } public void ExtractPacket() throws UnsupportedEncodingException{ int Position = inbuffer.position(); inbuffer.position(0); byte i; while((i = inbuffer.get())!=0){ System.out.println(i); if(Packets.IsPacket(i)){ WranglePacket(i); } } inbuffer.position(Position); } public void WranglePacket(int ID) throws UnsupportedEncodingException{ System.out.println("Whoopee! Wrangling!"); inbuffer.position(1); switch (ID){ case 0x02: short Length = inbuffer.getShort(); System.out.println("Got a length: "+Length+","+inbuffer.position()); if(Length>0){ byte[] str = null; inbuffer.get(str, 0, Length); String string = new String(str,"UTF8"); System.out.println("Aha! Got a username: "+string); username = string; } else{ System.out.println("False alarm!"); } default: System.out.println("Unknown ID: "+ID); } } } [/code] If you can see the error, that would be great. I can't :P Thanks in advance!
Which line is line 50 in WranglePacket Wait [code] byte[] str = null; inbuffer.get(str, 0, Length); //line 50 [/code] I'm guessing it's that one. Assign str before you use it.
[code] byte[] str = null; inbuffer.get(str, 0, Length); //line 50 [/code] The issue is that str isn't initialized, and is null. You'd need to create a byte array of size Length there. [url][noparse]http://download.oracle.com/javase/1.4.2/docs/api/java/nio/ByteBuffer.html#get%28byte[],%20int,%20int%29[/noparse][/url] [quote] In other words, an invocation of this method of the form src.get(dst, off, len) has exactly the same effect as the loop for (int i = off; i < off + len; i++) dst[i] = src.get(); [/quote]
Ah, great. Thank you very much. [code] Got a length: 10 Aha! Got a username: bobthe2lol [/code] Works perfectly :) I have another question aswell, if possible. How can I get the number of elements in a ByteBuffer? There seems to be no function to do this, and buffer.array().length just returns the length that i allocated it with...?
[QUOTE=bobthe2lol;27433446] I have another question aswell, if possible. How can I get the number of elements in a ByteBuffer? There seems to be no function to do this, and buffer.array().length just returns the length that i allocated it with...?[/QUOTE] arrayOffset() could be it.
Thanks. I gave up on ByteBuffer and wrote my own wrapper for byte with all the write and read functions etc. I may be an idiot, and was using the wrong thing in the first place, but oh well :v:
[QUOTE=bobthe2lol;27439276]Thanks. I gave up on ByteBuffer and wrote my own wrapper for byte with all the write and read functions etc. I may be an idiot, and was using the wrong thing in the first place, but oh well :v:[/QUOTE] As long as you know what a terrible, terrible idea that is :v:
Why so? It's simple really. It has a byte[] in it, and then it has write/read byte/int/short/long/string etc. Why is it a bad idea?
[QUOTE=bobthe2lol;27439368]Why so? It's simple really. It has a byte[] in it, and then it has write/read byte/int/short/long/string etc. Why is it a bad idea?[/QUOTE] The best code you'll ever write is code you never have to write.
True, so can you point me to a byte[] wrapper that has function to write and read string/byte/byte[]/int/long/short/etc that is already written?
[QUOTE=bobthe2lol;27439797]True, so can you point me to a byte[] wrapper that has function to write and read string/byte/byte[]/int/long/short/etc that is already written?[/QUOTE] [url=http://download.oracle.com/javase/1.4.2/docs/api/java/nio/ByteBuffer.html]ByteBuffer[/url]? (As for Strings - convert them to byte arrays or byte buffers first (e.g. String.getBytes()). Remember that there are many different ways to encode a string.)
Sorry, you need to Log In to post a reply to this thread.