0

Hello I am having fun with Arduino :D

And I have serialEvent

    public synchronized void serialEvent(SerialPortEvent oEvent) {
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {
            String inputLine = input.readLine();
            System.out.println(inputLine);

        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
}

How should look like the code to display text in JLabel (java swing)

lblText.setText(inputLine.toString());

But I have error inputLine cannot be resolved

1 Answers1

0
  1. inputLine cannot be resolved because it's being accessed out of scope. Declare inputLine at in class-level scope instead of method-level scope to solve your problem.
  2. You access lblTxt in your searialEvent(...) method and your initialize() method, so that also has to be at the class-level scope too.

A short example in context:

... //Imports and package declaration

public class ArduinoGUI implements SerialPortEventListener {

    private String inputLine;
    private JLabel lblTxt;

    ... //Other private variables, initialize2(), and close()

    public synchronized void serialEvent(SerialPortEvent oEvent) {
        if(oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
            try {
                inputLine = input.readLine(); //Don't forget to use the                             
                                              //class-level scope variable here
                System.out.println(inputLine);
                lblTxt.setText(inputLine); //.toString() unnessesary since 
                                           //inputLine is a String
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }

    ... //Main method and constructor

    private void initialize() {
        ... //GUI stuff
        lblTxt = new JLabel("txt"); //Don't forget to use the 
                                    //class-level scope variable here too
        ... //Adding lblTxt to frame
        lblTxt.setText(inputLine); //Again, .toString() unnessesary
    }
}
MasterBlaster
  • 918
  • 2
  • 13
  • 24
  • line 92: window.frame.setVisible(true); – Robert Niegowski Jun 10 '16 at 21:35
  • What kind of errors? I can't compile your code and find out because I don't have the external .jar files. – MasterBlaster Jun 10 '16 at 21:38
  • You posted the stack trace but you left out what type of exception happened, which is what I need to know to help you solve your problem because I'm not getting an error at line 92. – MasterBlaster Jun 10 '16 at 21:58
  • Look at [this question](http://stackoverflow.com/q/218384/4475997) to help you solve `NullPointerException`s. – MasterBlaster Jun 10 '16 at 22:05
  • solved :) Another problem, Nothing appears in the label :/ In console works well – Robert Niegowski Jun 10 '16 at 22:22
  • In your `main(String[] args)` method you create two `ArduinoGUI`s. You set the `frame` to visible in one, and call `initialize()` on the other. Create **one** instance, initialize it and set it to visible. – MasterBlaster Jun 10 '16 at 22:31
  • Changed http://pastebin.com/1DeG7KT6 But i think that it doesn't work but it outside EventQueue ?? – Robert Niegowski Jun 10 '16 at 22:33
  • But i think that it doesn't work cause it is outside EventQueue ??* – Robert Niegowski Jun 10 '16 at 22:40
  • Looking at the [JavaDocs for Thread#start](https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#start()), it says that an `IllegalThreadState` exception will be thrown it the `Thread` is already started and this method is called. Remove the call to `start()` inside the `run` method. – MasterBlaster Jun 10 '16 at 22:43
  • I know and it works in console but I want to put it into label (gui). How can i run initialize2? without copy ArduinoGUI main = new ArduinGUI(); – Robert Niegowski Jun 10 '16 at 22:49
  • Is there something wrong with calling `initialize2()` after `initialize()` in the `try` block in the `run` method of the `EventQueue`? What am I missing? – MasterBlaster Jun 10 '16 at 22:55
  • http://pastebin.com/Zqtqt17n and now I have only java.lang.NullPointerException in interval without erros – Robert Niegowski Jun 10 '16 at 22:55
  • Where is "in interval"? Do you have any idea of what is causing the `NullPointerException`? – MasterBlaster Jun 10 '16 at 23:01
  • It is null when I wanna put it into label lblTxt i Did something liek this inputLine = input.readLine(); System.out.println(inputLine); lblTxt.setText(inputLine); and I received -> Text -> Null -> Text ->Null ( NUll is when is time to put it into label ) – Robert Niegowski Jun 10 '16 at 23:10
  • My only piece of help would be to make sure that you are using the class-level scope `lblTxt` in your `initialize()` method. – MasterBlaster Jun 10 '16 at 23:16
  • Thank you very much it works when I added frame.getContentPane().add(lblTxt, BorderLayout.CENTER); Thank you – Robert Niegowski Jun 10 '16 at 23:23
  • Just a note for the future: When you're asking questions in the comments make sure to be very specific and tell the responder exactly what you mean. Unfortunately, most people won't be as lenient as I was. – MasterBlaster Jun 10 '16 at 23:43
  • Thank you one more time :) – Robert Niegowski Jun 11 '16 at 00:00