NullPointer in a notifier

19 views
Skip to first unread message

Gilad Baruchian

unread,
Aug 31, 2015, 2:57:35 PM8/31/15
to Jenkins Developers
I created this class 

package notifier;

import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.tasks.*;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

/**
* Created by giladba on 8/31/2015.
*/
public class BuildNotifier extends Notifier {
private final String to;

@DataBoundConstructor
public BuildNotifier(String to){
if(to==null)
to="";
this.to=to;
}

@Override
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {
// build.getEnvVars()["myVar"]
// listener.getLogger().println("Build Success Notifier Finished.");
return true;
}

@Override
public BuildStepMonitor getRequiredMonitorService() {
return null;
}

public String getTo() {
return to;
}

@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl)super.getDescriptor();
}

@Extension
public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> {

public DescriptorImpl() {
load();
}

@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}

@Override
public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
save();
return super.configure(req,formData);
}

@Override
public String getDisplayName() {
return "Build Success Email";
}
}

}


I added the config.jelly (it works, i see the UI in the post build) :

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<!--
This jelly script is used for per-project configuration.

See global.jelly for a general discussion about jelly script.
-->

<!--
Creates a text field that shows the value of the "name" property.
When submitted, it will be passed to the corresponding constructor parameter.
-->
<f:entry title="To" field="to">
<f:textarea />
</f:entry>
</j:jelly>


But when I run the build I get this Exception : 
java.lang.NullPointerException
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:770)
at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:734)
at hudson.model.Build$BuildExecution.post2(Build.java:183)
at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:683)
at hudson.model.Run.execute(Run.java:1770)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:89)
at hudson.model.Executor.run(Executor.java:240)

which refers to this code from jenkins (im guessing that in the last line mon=null):

/**
* Calls a build step.
*/
protected final boolean perform(BuildStep bs, BuildListener listener) throws InterruptedException, IOException {
BuildStepMonitor mon;
try {
mon = bs.getRequiredMonitorService();
} catch (AbstractMethodError e) {
mon = BuildStepMonitor.BUILD;
}
Result oldResult = AbstractBuild.this.getResult();
for (BuildStepListener bsl : BuildStepListener.all()) {
bsl.started(AbstractBuild.this, bs, listener);
}
boolean canContinue = mon.perform(bs, AbstractBuild.this, launcher, listener);

What am i missing here?

Gilad Baruchian

unread,
Aug 31, 2015, 3:04:50 PM8/31/15
to Jenkins Developers
Solved my Slide-O-Mix, my mistake was that i returned null, it needs to be like this :

@Override
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}

 

Daniel Beck

unread,
Aug 31, 2015, 7:17:36 PM8/31/15
to jenkin...@googlegroups.com
getRequiredMonitorService must not return null. Read the Javadoc on what it’s supposed to return.
Reply all
Reply to author
Forward
0 new messages