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

On this line:

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

Don't you mean:

System.out.println("Key msg:" +getKey());
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.