Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have declared a variable globally then i have assigned variable value in a method.
whenever i try to get variable value in another method id shows null or empty.
how can i solve this issue.

This is My code:
Java
public class MainActivity extends ActionBarActivity {
     String n="";
    Timer timer;
    TimerTask timerTask;
    final android.os.Handler handler = new android.os.Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       startTimer();
        Test2();
    }
    public void startTimer() {
//        Pull();
        //set a new Timer
        timer = new Timer();

        //initialize the TimerTask's job
        initializeTimerTask();

        //schedule the timer, after the first 5000ms the TimerTask will run every 10000ms
        timer.schedule(timerTask,0, 15000); //
    }
    public void Timer15()
    {

        timer =new Timer();
        initializeTimerTask();
        timer.schedule(timerTask,0,5000);
    }

    public void initializeTimerTask() {

        timerTask = new TimerTask() {
            public void run() {

                //use a handler to run a toast that shows the current timestamp
                handler.post(new Runnable() {
                    public void run() {
                        Test1();


//                        Toast.makeText(getApplicationContext(),"hai",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };
    }
   public void Test1()
   {
     n="vetri";//Assigning value for n

   }
    public  void Test2()
    {
        if (n=="vetri")//Here n value is null
        {
            timer.cancel();
            Timer15();
            Toast.makeText(getApplicationContext(),n,Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
        }

    }
    
}
Posted
Updated 18-Jun-15 21:40pm
v3
Comments
Richard MacCutchan 16-Jun-15 12:50pm    
Where have you declared it and how are you trying to access it? Please show some of your code to explain the problem better.
Member 11420908 19-Jun-15 0:44am    
This is my Code

public class MainActivity extends ActionBarActivity {
String n="";
Timer timer;
TimerTask timerTask;
final android.os.Handler handler = new android.os.Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startTimer();
Test2();
}
public void startTimer() {
// Pull();
//set a new Timer
timer = new Timer();

//initialize the TimerTask's job
initializeTimerTask();

//schedule the timer, after the first 5000ms the TimerTask will run every 10000ms
timer.schedule(timerTask,0, 15000); //
}
public void Timer15()
{

timer =new Timer();
initializeTimerTask();
timer.schedule(timerTask,0,5000);
}

public void initializeTimerTask() {

timerTask = new TimerTask() {
public void run() {

//use a handler to run a toast that shows the current timestamp
handler.post(new Runnable() {
public void run() {
Test1();


// Toast.makeText(getApplicationContext(),"hai",Toast.LENGTH_SHORT).show();
}
});
}
};
}
public void Test1()
{
n="vetri";

}
public void Test2()
{
if (n=="vetri")
{
timer.cancel();
Timer15();
Toast.makeText(getApplicationContext(),n,Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
}

}

}
Richard MacCutchan 19-Jun-15 3:44am    
You are accessing the variable in different threads, without synchronisation, so there is no guarantee which check will be first. You call startTimer then immediately call Test2 so the variable may well not be changed at that point.
Dadecki 18-Jun-15 4:22am    
Hello, please show the declaration of the value and method where you trying to use it.
Member 11420908 19-Jun-15 0:45am    
This is my Code

public class MainActivity extends ActionBarActivity {
String n="";
Timer timer;
TimerTask timerTask;
final android.os.Handler handler = new android.os.Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startTimer();
Test2();
}
public void startTimer() {
// Pull();
//set a new Timer
timer = new Timer();

//initialize the TimerTask's job
initializeTimerTask();

//schedule the timer, after the first 5000ms the TimerTask will run every 10000ms
timer.schedule(timerTask,0, 15000); //
}
public void Timer15()
{

timer =new Timer();
initializeTimerTask();
timer.schedule(timerTask,0,5000);
}

public void initializeTimerTask() {

timerTask = new TimerTask() {
public void run() {

//use a handler to run a toast that shows the current timestamp
handler.post(new Runnable() {
public void run() {
Test1();


// Toast.makeText(getApplicationContext(),"hai",Toast.LENGTH_SHORT).show();
}
});
}
};
}
public void Test1()
{
n="vetri";

}
public void Test2()
{
if (n=="vetri")
{
timer.cancel();
Timer15();
Toast.makeText(getApplicationContext(),n,Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
}

}

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900