grnmachine
asked on
Why am I getting a null value when trying to access a variable?
Hi, in my code, I am setting a variable in one method and trying to retrieve that variable in another but I keep getting a null value when trying to access that set variable? I've attached a snippet of my code. The varialbe is objectHolder. I have getters and setters defined for this variable in my class. Thanks.
public void start(String filename) {
Scanner in = null;
String timeParam;
String eventName;
String bellRing;
eventsFile = filename;
Class [] classParm = new Class[1];
Field classParm1;
File file = new File(eventsFile);
try {
in = new Scanner(file);
//For each line in the file, create an event
// if(currentEvent == ""){
// this.threadCollection = new ArrayList<Thread>();
while(in.hasNext()){
//Split the line by the comma.
String[] array1 = in.nextLine().split(",");
//The first token in the array is the EventName:
String[] array2 = array1[0].split("=");
eventName = array2[1];
//The second Token is the time parameter
array2 = array1[1].split("=");
timeParam = array2[1];
time = Long.parseLong(timeParam);
//classParm = (Class[])time;
//Create the new Event, and addEvent
try {
Class cl = Class.forName(eventName);
classParm[0] = long.class;
// System.out.println("Class name " + cl);
Constructor co = cl.getConstructor(classParm);
//System.out.println("Constuctor " + co);
//System.out.println("New instance " + co.newInstance(time));
objectParm = co.newInstance(time);
System.out.println("object param " + objectParm);
This is the var-> objectHolder = objectParm;
System.out.println("getting set object param " + this.getObjectParm());
System.out.println(objectParm.getClass().getDeclaredField("t"));
Method m = cl.getMethod("suspendThread");
m.invoke(objectParm,null);
//System.out.println();;
//threadCollection.add((Thread)classParm1);
} catch (Exception e) {
e.printStackTrace();
}
if(eventName.equals("Bell"))
{
try{
String[] array3 = array1[2].split("=");
bellRing = array3[1];
rings = Integer.parseInt(bellRing);
addEvent(new Bell(time));
currentEvent = "Bell";
}catch(ArrayIndexOutOfBoundsException e){
addEvent(new Bell(time));
currentEvent = "Bell";
}
}
}
}catch(Throwable e) {
e.printStackTrace();
}
finally {
in.close();
}
}
public void suspendThread() {
System.out.println("in suspend " + objectHolder);
// Method m = cl.getMethod("suspendThread");
// m.invoke(objectParm,null);
try {
System.out.println(this.getObjectParm());
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
suspended = true;
}
Can you please post what's generated by your System.out calls?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@CEHJ:
Here is the output.
Event Thermostat on night setting
object param Thermostat on night setting
getting set object param Thermostat on night setting
public java.lang.Thread ThermostatNight.t
Thread suspended
In run method
in suspend null
null
Here is the output.
Event Thermostat on night setting
object param Thermostat on night setting
getting set object param Thermostat on night setting
public java.lang.Thread ThermostatNight.t
Thread suspended
In run method
in suspend null
null
ASKER
@sciuriware:
I have the declaration at the top of my page for objectHolder which I left out of the snippet.
I have the declaration at the top of my page for objectHolder which I left out of the snippet.
The suspend thread is probably running before another thread has initialized objectHolder
How can we deal with objectHolder = objectParm;
when we can not check that the assignment is valid?
What is the declaration?
;JOOP!
when we can not check that the assignment is valid?
What is the declaration?
;JOOP!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
all you do is create a new instance of the class and then call suspendThread
where did you expect the setter to be being called?
where did you expect the setter to be being called?
Excuse me. I cannot read your program. Are these two methods from the same class?? I suspect they are copied from different classes.
Moreover, for your code:
public void start(String filename) {
...
Class cl = Class.forName(eventName);
...
Method m = cl.getMethod("suspendThrea d");
m.invoke(objectParm,null);
...
}
public void suspendThread() {
...
}
If you intend to invoke the method suspendThread() of the same instance, this presumes objectParam = this.
I wonder if it is making any sense!?
Moreover, for your code:
public void start(String filename) {
...
Class cl = Class.forName(eventName);
...
Method m = cl.getMethod("suspendThrea
m.invoke(objectParm,null);
...
}
public void suspendThread() {
...
}
If you intend to invoke the method suspendThread() of the same instance, this presumes objectParam = this.
I wonder if it is making any sense!?
ASKER
I took a look at my declaration for the variable and noticed that it was not declared properly thus returning a null value. I added static to the declaration so I could use the variable in other methods. This worked. Thanks for your help.