Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 320586 - NullPointerException in AntActor internalPerform()
Summary: NullPointerException in AntActor internalPerform()
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Buckminster (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: buckminster.core-inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-07-22 02:14 EDT by Jakob Braeuchi CLA
Modified: 2019-02-25 14:40 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jakob Braeuchi CLA 2010-07-22 02:14:40 EDT
Build Identifier: 

The problem is caused by the call to toString() for properties with null value
...
for (Map.Entry<String, ? extends Object> entry : ctx.getProperties().entrySet())
	props.put(entry.getKey(), entry.getValue().toString()); 
...


instead of toString() String.valueOf(entry.getValue()) should be used.

Reproducible: Always
Comment 1 Jakob Braeuchi CLA 2010-07-22 02:43:57 EDT
to reproduce it:

1. create a plugin with a valueVariables extension:
<plugin>
   <extension
         point="org.eclipse.core.variables.valueVariables">
      <variable
            initializerClass="bla.MyInitializer"
            name="envtest.variable"
            readOnly="true">
      </variable>
   </extension>
</plugin>

2. read an undefined env-variable in the initializer:
public class MyInitializer implements IValueVariableInitializer {
	@Override
	public void initialize(IValueVariable variable) {
		variable.setValue(System.getenv("MY_HOME"));
	}
}

3. create a buckminster ant-action in cspex of your plugin:
	<actions>
		<public actor="ant" name="myaction">
			<actorProperties>
                <property key="buildFile" value="build/ant.xml"/>
                <property key="targets" value="myaction"/>
            </actorProperties>			
		</public> 
	</actions>

4. read the variable in ant-file abt.xml of the action:
<project>
	<target name="myaction">
	<echo>VAR=${envtest.variable}</echo>
	</target>
</project>

5. invoke the ant-action "myaction" and you'll run into NPE in AntActor#internalPerform() line 236.
Comment 2 Thomas Hallgren CLA 2010-07-22 04:06:04 EDT
(In reply to comment #0)
> The problem is caused by the call to toString() for properties with null value
> ...
> for (Map.Entry<String, ? extends Object> entry :
> ctx.getProperties().entrySet())
>     props.put(entry.getKey(), entry.getValue().toString()); 
> ...
> 
> 
> instead of toString() String.valueOf(entry.getValue()) should be used.
> 
The String.valueOf(null) returns the string "null" so perhaps this is better:

for (Map.Entry<String, ? extends Object> entry : ctx.getProperties().entrySet()) {
  Object val = entry.getValue();
  props.put(entry.getKey(), val == null ? null : val.toString());
}

Another alternative is to simply skip a null property. I.e.:

for (Map.Entry<String, ? extends Object> entry : ctx.getProperties().entrySet()) {
  Object val = entry.getValue();
  if(val != null)
    props.put(entry.getKey(), val.toString());
}

What would be most appropriate for your use case?
Comment 3 Matthias Kappeller CLA 2010-07-22 04:31:39 EDT
(In reply to comment #2)
> The String.valueOf(null) returns the string "null" so perhaps this is better:
> 
> for (Map.Entry<String, ? extends Object> entry :
> ctx.getProperties().entrySet()) {
>   Object val = entry.getValue();
>   props.put(entry.getKey(), val == null ? null : val.toString());
> }
> 
I think this is a good solution. But i don't know exactly what happens in ant if a property is requested whose value is null. (hopefully a understandable error message :) )
> 
> for (Map.Entry<String, ? extends Object> entry :
> ctx.getProperties().entrySet()) {
>   Object val = entry.getValue();
>   if(val != null)
>     props.put(entry.getKey(), val.toString());
> }
> 
In this case a property is skipped and a user might wonder why. I think it's neccessary to know that the property is null.

=> So in our case the first solution whould be the better one.
Comment 4 Thomas Hallgren CLA 2010-07-22 04:46:35 EDT
I also prefer the first solution. It's now released to helios-maintenance, rev 11517.
Comment 5 Thomas Hallgren CLA 2010-07-22 04:46:54 EDT
.