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.


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();

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

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;
    if(Keyboard.isKeyDown(Keyboard.KEY_LMENU) || Keyboard.isKeyDown(Keyboard.KEY_RMENU))
        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;

        if(Keyboard.getEventKeyState()) {

        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!


        //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.

  • 1
    \$\begingroup\$ Have you set a breakpoint and looked at it in the debugger? \$\endgroup\$
    – House
    Nov 5, 2013 at 23:30
  • 2
    \$\begingroup\$ 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? \$\endgroup\$
    – nejinx
    Nov 6, 2013 at 6:18
  • \$\begingroup\$ 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? \$\endgroup\$ Nov 6, 2013 at 21:54
  • \$\begingroup\$ 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. \$\endgroup\$
    – msell
    Nov 7, 2013 at 6:17
  • \$\begingroup\$ @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. \$\endgroup\$ Nov 8, 2013 at 0:11

2 Answers 2


Judging with the code you provided, your STDOUT Stream likely isn't what you expect it to be due to a control character.

Try using a System.out.flush() after your System.out.print()

Let me know if this helps.

  • \$\begingroup\$ Clever, and potentially revealing, but the problem in this case seems to be a null value and not a control character. See Nejinx's comment above. Now I just need to figure out how I got that null value there to begin with. \$\endgroup\$ Nov 6, 2013 at 23:05
  • \$\begingroup\$ You might want to validate what's coming from Keyboard.getEventCharacter(). \$\endgroup\$
    – ThorinII
    Nov 7, 2013 at 1:08
  • \$\begingroup\$ Already done. Much of the time, Keyboard.getEventCharacter() is, in fact, null. I'm not yet certain what this means. It might mean that I'm at the end of the list of events, or that no character was typed, or any number of other things. Tragically, the Javadoc for LWJGL is severely lacking in many areas. Better documentation, beyond simple tutorials, on the methods would help. \$\endgroup\$ Nov 7, 2013 at 4:55

Solution found. To toggle the console mode, I use the tilde key ("~"). This requires a shift, and the grave character ("`", next to the numbers). After I release tilde, I generally still have the shift key down, which, incidentally, is a type of key event. Shift itself is not responsible for a proprietary character, yet is fully valid otherwise. Thus, when I request the character, I receive null, and append it to the ArrayList contained in StringBuffer. A little defensive programming will easily solve this.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .