0

Hello All

Am having some challenges getting a value in a variable from another class; the code that computes the variable happens to be in a timertask method however if i move that same line of code to the constructor, the second class is able to fetch the value.

When i compile there is no error but a null value in the second class variable. Below is the first class where "Setkey" is where the expected value resides and in the second code is the class that fetches the value in the "Setkey" variable(in the first class)

Please have in mind that am able to see the expected value in the first class when i print out, so I know that variable is not null

public static void setKey(String encryptedkey ){
    Key_Exchange.encryptedkey = encryptedkey;
}
public static String getKey(){
    return Key_Exchange.encryptedkey;

}

public class keyresponse extends TimerTask{
        public void run() {

    while (i == 0){
                try {
                    String readmsg = din.readUTF();
                    System.out.println("incoming: " +readmsg)
                    if ((readmsg.length() == 131)){
                    setKey(readmsg.substring(88, 120));
                    System.out.println("Key msg:" +setKey);
                    }

                } catch (IOException ex) {
                    
                }

   }

        }
    }

   public void run() {

        synchronized(timer){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(Key_Exchange.class.getName()).log(Level.SEVERE, null, ex);
            }
            timer.schedule(new keyresponse (), seconds * 400);

       }
private byte[] theKey = null;
    private static byte[] theMsg;
    private byte[] theCph;
    Key_Exchange ke;

    public DesDecryption() throws UnknownHostException, IOException {

    theMsg = hexToBytes(ke.getkey());
    System.out.println("the message " +theMsg);
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Akill10
0

On this line:

System.out.println("Key msg:" +setKey);

Don't you mean:

System.out.println("Key msg:" +getKey());
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.