maheshwarirohit87 / typica

Automatically exported from code.google.com/p/typica
Apache License 2.0
0 stars 0 forks source link

ReservationDescription throwing NullPointerExceptions #130

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
This issue was discovered when utilizing the Typica library against an 
OpenStack cluster, which is EC2 compliant.  In particular, I am working on a 
Java auto-scaling application wherein I make use of the Jec2 method 
runInstances(LaunchConfiguration config).  The method call creates the image 
successfully, but a NullPointerException is thrown during the creation of the 
ReservationDescription object that is returned from the runInstances method.

What steps will reproduce the problem?
1. Create a LaunchConfiguration object that contains an image ID and a key name 
where the image ID was set in the constructor, and the key name was set using 
the setKeyName method.
2. Call the runInstances method on a Jec2 object pointing to an OpenStack cloud 
using the LaunchConfiguration object created in step 1.
3. The instance will be created, but the runInstances method will throw a 
NullPointerException.

What is the expected output? What do you see instead?
I expect the runInstances method to complete successfully, and return a 
ReservationDescription object representing the slot instance reservation, but 
get a NullPointerException from the runInstances method before it completes, 
but after the instance it created.

What version of the product are you using? On what operating system?
I am using version 1.7.2 on a Windows environment (within Eclipse) 
communicating to an OpenStack cloud running on RedHat 5.6.

Please provide any additional information below.
After digging around in the source code of the ReservationDescription class, it 
looks like the NullPointerException is being thrown at line 227 and line 237.  
Line 227 is included in the following loop:

for (ProductCodesSetItemType type : rsp_item.getProductCodes().getItems()) {
   codes.add(type.getProductCode());
}

The problem occurs because when this class is run against an image in our 
OpenStack cluster, the method call rsp_item.getProductCodes() returns null, and 
hence the getItems() call produces the NullPointerException.

I experimented with updating the above code as follows:

if(rsp_item.getProductCodes() != null) {
   for (ProductCodesSetItemType type : rsp_item.getProductCodes().getItems()) {
      codes.add(type.getProductCode());
   }
}

And the NullPointerException was resolved.  After this was corrected, I got 
another NullPointerException at line 237:

this.monitoring = rsp_item.getMonitoring().getState().contains("enabled");

Where the call to rsp_item.getMonitoring() was returning null.  Again, I just 
put a simple test around this line:

if(rsp_item.getMonitoring() != null)
   this.monitoring = rsp_item.getMonitoring().getState().contains("enabled");

No more NullPointerExceptions were thrown, and the Instance constructor in the 
ReservationDescription class completed without error resulting in a successful 
execution of the runInstances method of the Jec2 class.

Original issue reported on code.google.com by etblackw...@gmail.com on 21 Feb 2012 at 7:03

GoogleCodeExporter commented 9 years ago
This same type of problem exists when I try to terminate an instance using the 
Jec2.terminateInstances(String[] instanceIds) method within the same 
environment described above.  Again, the problem can be traced back to a 
specific line of code that makes assumptions about the existence of a specific 
object.  In this case, it is line 781 of the Jec2 class within the 
List<InstanceStateChangeDescription> terminateInstances(List<String> 
instanceIds) method that says:

Iterator instances_iter = set.getItems().iterator();

In my described environment, the call:

InstanceStateChangeSetType set = response.getInstancesSet();

directly above line 781 returns null for the set object.  To fix this, I simply 
put another null test around the code that uses the set object:

if(set != null) {
   Iterator instances_iter = set.getItems().iterator();
   while (instances_iter.hasNext()) {
      InstanceStateChangeType rsp_item = 
            (InstanceStateChangeType)instances_iter.next();
      res.add(new InstanceStateChangeDescription(
            rsp_item.getInstanceId(), rsp_item.getPreviousState().getName(),
            rsp_item.getPreviousState().getCode(),
            rsp_item.getCurrentState().getName(),
            rsp_item.getCurrentState().getCode()));
   }
}

And everything worked without producing the NullPointerException.

Original comment by etblackw...@gmail.com on 21 Feb 2012 at 8:36

GoogleCodeExporter commented 9 years ago
Thanks to the OP. You saved my life ;)
I recompiled and it works.
I attached the fixed ReservationDescription (changed from the 1.7.2 version). 
If someone could take this fix, and others, and make a new release it would be 
great.

P.S.: I also included an unofficial build with this fix. I called it 1.7.3.

Original comment by petersai...@gmail.com on 18 Nov 2012 at 12:05

Attachments: