0

I am having an issue creating a custom console, on the following code:

public class UserConsole {

    protected static BlockingQueue<String> inputData;

    private final static JTextArea textArea = new JTextArea();
    private static JTextField textField = new JTextField("");

private void createGUI() {

    final KeyListener returnAction = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyChar() == '\n') {
                    returnInput();
                   }
            }};
    }

private void returnInput() {


//Here is the problem, the BlockingQueue throws a NullPointerException, which is strange, because the...
//...right after "System.out.println(textField.getText());" works perfectly fine.
   inputData.offer(textField.getText());
        System.setOut(userStream);
        System.out.println(textField.getText());
        textField.setText("");
        System.setOut(nebulaStream);

          }
}

I tried searching online, but didn't find anything, also tried adding .toString() but it does not work as well.

As far as I know, a BlockingQueue cannot be initialized... So my final question is. Why is the BlockingQueue not reading the JTextField's string, and how can it be solved?

I hope it is not something obvious that I missed, every help is appreciated!

Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
CLR 123
  • 38
  • 1
  • 5
  • 2
    It's clear that the BlockingQueue object has never been initiated in your code. – Ahmad R. Sedighi Jul 23 '20 at 20:57
  • @AhmadR.Sedighi, No, it hasn't... I thought that as it cannot be instantiated that he couldn't be initialized, was that the mistake? I have seen code examples of the BlockingQueue before and it also was never initialized... – CLR 123 Jul 23 '20 at 21:57
  • Your title doesn't express your actual problem in any way. `BlockingQueues` don't read, and the `JTextField` is completely irrelevant. See the stack trace. – user207421 Jul 23 '20 at 23:35
  • Where you got 'as far as I know, a `BlockingQueue` cannot be initialized' from is another mystery. Are you confusing 'initialized' with 'instantiated'? – user207421 Jul 24 '20 at 00:07

1 Answers1

0

As far as I know, a BlockingQueue cannot be initialized... So my final question is. Why is the BlockingQueue not reading the JTextField's string, and how can it be solved?

We aren't seeing some of the important sections of the code but here are some possible problems.

  1. I don't see where the blocking queue is actually instantiated. Maybe the initialization line should be something like:

    protected static final BlockingQueue<String> inputData = new LinkedBlockingQueue<>();
    
  2. We don't see where the string is removed from the blocking queue. If you want to check to see if the result has been calculated then you should use poll() which will return null until the reset is offered to the blocking queue.

    // this will not wait for the result but will return null initially
    String jtextResult = inputData.poll();
    

    If you need to wait for the result then use take() but you should never do this on a GUI thread because your UI will pause until the result is calculated negating the need for the background thread.

Gray
  • 108,756
  • 21
  • 270
  • 333
  • Thank you! My mistake, obviously, but I didn't know I should have to use the constructor of the `new LinkedBlockingQueue<>();`. It works fine now. By the way, so should I use a separate class from the UI for this operation? – CLR 123 Jul 23 '20 at 22:02
  • Not 100% sure about the separate class @CLR123. Classes have very little overhead however unless you are creating a crap-ton of them. So if it makes the code more readable/maintainable, then you should consider it. – Gray Jul 29 '20 at 16:50