• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

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

Open in new window

0
grnmachine
Asked:
grnmachine
  • 3
  • 2
  • 2
  • +2
2 Solutions
 
CEHJCommented:
Can you please post what's generated by your System.out calls?
0
 
sciuriwareCommented:
I don't see a declaration for 'objectHolder'.

;JOOP!
0
 
grnmachineAuthor Commented:
@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

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
grnmachineAuthor Commented:
@sciuriware:

I have the declaration at the top of my page for objectHolder which I left out of the snippet.
0
 
CEHJCommented:
The suspend thread is probably running before another thread has initialized objectHolder
0
 
sciuriwareCommented:
How can we deal with                objectHolder = objectParm;
when we can not check that the assignment is valid?
What is the declaration?

;JOOP!
0
 
objectsCommented:
you never set objectHolder
and you don't set anything in objectParm either
0
 
objectsCommented:
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?
0
 
josephtsangCommented:
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("suspendThread");
   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!?
0
 
grnmachineAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now