0

I want to store the data received from serial port in a string variable that will be accessed in another class. I wrote up code that prints the data received from com port but when the variable is accessed out of the method it returns null.. Please help me out.. I am using RxTx library for this.

public class ProtocolImpl implements Protocol {  

    byte[] buffer = new byte[1024];  
    int tail = 0;  
    public String message;

    public void onReceive(byte b) {  
        // simple protocol: each message ends with new line  
        if (b=='\n') {  
            onMessage();  
        } else {  
            buffer[tail] = b;  
            tail++;  
        }  
    }  

    public void onStreamClosed() {  
        onMessage();  
    }  

    /* 
     * When message is recognized onMessage is invoked  
     */  
    private void onMessage() {  
        if (tail!=0) 
        {  
            // constructing message  
            message = getMessage(buffer, tail);  
            //rmess = message;
            System.out.println("RECEIVED MESSAGE: " + message);  

            if ("KITM".equalsIgnoreCase(message)) {  
                CommPortSender.send(getMessage("OK"));  
            } 
            tail = 0;  
        }  
    }  

    public String rmess() /*this method is returning null.. please help me out*/
    {
        if (tail!=0) {  
        message = getMessage(buffer, tail); 
        }
        return message;
    }

    // helper methods   
    public byte[] getMessage(String message) {  
        return (message).getBytes();  
    }  

    public String getMessage(byte[] buffer, int len) {  
        return new String(buffer, 0, tail);  
    }  
}
Rüdiger Hanke
  • 6,215
  • 2
  • 38
  • 45
Manu
  • 73
  • 1
  • 9
  • Can you add the code with how you're attempting to access the variable "out of the method" where it returns `null`? Or indicate what part of the posted code is the part that's doing this? – Roddy of the Frozen Peas Jul 24 '12 at 20:38
  • if the rmess() method is called inside onmessage(): onMessage() { ........... .......... System.out.println("RECEIVED MESSAGE: " + message); System.out.println(rmess()); ....... ....... } here it returns the value of the received message means both line gives same output (obviously) but when the rmess() method is called out of this class it returns null.. – Manu Jul 25 '12 at 04:32

2 Answers2

1

You are using an instance variable message. There is one instance of this variable for each ProtocolImpl object. Presumably the ProtocolImpl object on which onMessage is called is a different ProtocolImpl object on which rmess is called.

The easy fix is just to make message a static variable so that there is only one instance of that variable in the whole program. Be careful, though, this can cause some subtle problems like synchronization and object independence. A better solution is to make sure you are using the same ProtocolImpl object to call both onMessage and rmess.

Keith Randall
  • 22,985
  • 2
  • 35
  • 54
  • I don't wanna use the rmess() method i'm doing it just to get the value of the received message in a string so that i can use the variable out of the class.. the S.O.P. line in onMessage() method is printing the received message but i was unable to catch it so i created this return type method but this was a fail.. Lemme check the code by make the message variable static.. – Manu Jul 25 '12 at 04:37
  • Keith Randall >>heyy thanks buddy, i made the message variable static.. and it worked.. thanks a lot... why didn't it came to my mind before :P – Manu Jul 25 '12 at 04:53
0

Your message object is not serialized so you are getting message is null. You can implement java.io.Serializable

Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37