[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Why am I getting a null value when trying to access a variable?

Posted on 2009-12-30
10
Medium Priority
?
389 Views
Last Modified: 2013-11-23
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
Comment
Question by:grnmachine
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 26149630
Can you please post what's generated by your System.out calls?
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 750 total points
ID: 26149632
I don't see a declaration for 'objectHolder'.

;JOOP!
0
 

Author Comment

by:grnmachine
ID: 26149678
@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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:grnmachine
ID: 26149683
@sciuriware:

I have the declaration at the top of my page for objectHolder which I left out of the snippet.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 26149711
The suspend thread is probably running before another thread has initialized objectHolder
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 26149718
How can we deal with                objectHolder = objectParm;
when we can not check that the assignment is valid?
What is the declaration?

;JOOP!
0
 
LVL 92

Assisted Solution

by:objects
objects earned 750 total points
ID: 26149722
you never set objectHolder
and you don't set anything in objectParm either
0
 
LVL 92

Expert Comment

by:objects
ID: 26150449
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
 
LVL 5

Expert Comment

by:josephtsang
ID: 26152494
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
 

Author Closing Comment

by:grnmachine
ID: 31671431
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month17 days, 22 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question