# StringBuffer behavior in LWJGL

Okay, I've been programming in Java for about ten years, but am entirely new to LWJGL. I have a specific problem whilst attempting to create a text console. I have built a class meant to abstract input polling to it, which (in theory) captures key presses from the Keyboard object and appends them to a StringBuilder/StringBuffer, then retrieves the completed string after receiving the ENTER key.

The problem is, after I trigger the String return (currently with ESCAPE), and attempt to print it to System.out, I consistently get a blank line. I can get an appropriate string length, and I can even sample a single character out of it and get complete accuracy, but it never prints the actual string.

I could swear that LWJGL slipped some kind of thread-safety trick in while I wasn't looking.

==UPDATE==

I've now determined that a null value was appended at the beginning of my StringBuffer. The problem can (sloppily) be solved with a command.delete(0, 1) line, freeing the actual String data. However, I don't know how that null got in there to begin with. If anyone sees it before I do, please let me know.

Here's my code:

static volatile StringBuffer command = new StringBuffer();

@Override
public void chain(InputPoller poller) {
this.chain = poller;
}

@Override
public synchronized void poll() {
//basic testing for modifier keys, to be used later on
boolean shift = false, alt = false, control = false, superkey = false;

if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT))
shift = true;
alt = true;
if(Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) || Keyboard.isKeyDown(Keyboard.KEY_RCONTROL))
control = true;
if(Keyboard.isKeyDown(Keyboard.KEY_LMETA) || Keyboard.isKeyDown(Keyboard.KEY_RMETA))
superkey = true;

while(Keyboard.next())
if(Keyboard.getEventKeyState()) {
command.append(Keyboard.getEventCharacter());
}

if (Framework.isConsoleEnabled() && Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
System.out.println("Escape down");
System.out.println(command.length() + " characters polled"); //works
System.out.println(command.toString().length()); //works
System.out.println(command.toString().charAt(4)); //works
System.out.println(command.toString().toCharArray()); //blank line!
System.out.println(command.toString()); //blank line!

Framework.disableConsole();
}

//TODO: Add command construction and console management after that
}
}


Maybe the answer's obvious and I'm just feeling tired, but I need to walk away from this for a while. If anyone sees the issue, please let me know. This machine is running the latest release of Java 7 on Ubuntu 12.04, Mate desktop environment.

Many thanks.

• Have you set a breakpoint and looked at it in the debugger? – MichaelHouse Nov 5 '13 at 23:30
• Is the first character in the toCharArray a null character or a special character that causes everything after the initial character not to be displayed? – nejinx Nov 6 '13 at 6:18
• That was it, nejinx! A simple command.delete(0, 1) got it working perfectly. However, I would very much like to know how that null got in there, as I didn't intentionally buffer it onto command. It would be better to trim the problem away at the source, wouldn't it? – Michael Oberlin Nov 6 '13 at 21:54
• You really should learn to utilize debugger well. It would have revealed the problem by inspecting the content of the array and also how that null character got there. – msell Nov 7 '13 at 6:17
• @msell: I agree that software debuggers are handy, but only to a point. The only two I really rely on are print, and reading my code, especially for the kind of dynamic and creative software I typically write. The debugger, as an example, might have informed me of the null characters, but I fail to see how it would show me where they were coming from. That took base intuition. – Michael Oberlin Nov 8 '13 at 0:11

Try using a System.out.flush() after your System.out.print()
• You might want to validate what's coming from Keyboard.getEventCharacter(). – ThorinII Nov 7 '13 at 1:08