Search code examples
jmeterjmeter-plugins

Using UltimateThreadGroup with Java API gives Nullpointer exception


I am trying to use UltimateThreadGroup with the JMeter Java API, I am creating the UltimateThreadGroup object as in the below code[2] and add it to the Hashtree. Finally handover it to the JMeter engine to execute. 

But It gives the following NullPointerException in the middle of execution. As I debugged the code, the Issue seems coming from JMeterThread class following method.

public JMeterThread(HashTree test, JMeterThreadMonitor monitor, ListenerNotifier note, Boolean isSameUserOnNextIteration)

But issue throws on different code lines from execution to execution. So it's difficult figure out what causing the NullPointer.

Does anybody have an idea on what's going on here? Appreciate your answers.

[1]

    2020-11-03 13:08:20 DEBUG TestCompiler:273 - adding controller: kg.apc.jmeter.threads.UltimateThreadGroup@30b2b76f to sampler config
2020-11-03 13:08:22 ERROR JMeterThread:319 - Test failed!
java.lang.NullPointerException
    at org.apache.jmeter.threads.AbstractThreadGroup.addTestElement(AbstractThreadGroup.java:122)
    at org.apache.jmeter.threads.AbstractThreadGroup.addTestElementOnce(AbstractThreadGroup.java:131)
    at org.apache.jmeter.threads.TestCompiler.subtractNode(TestCompiler.java:151)
    at org.apache.jorphan.collections.HashTree.traverseInto(HashTree.java:997)
    at org.apache.jorphan.collections.HashTree.traverseInto(HashTree.java:994)
    at org.apache.jorphan.collections.HashTree.traverse(HashTree.java:976)
    at org.apache.jmeter.threads.JMeterThread.initRun(JMeterThread.java:704)
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:252)
    at java.lang.Thread.run(Thread.java:748)

[2]

UltimateThreadGroup ultimateThreadGroup = new UltimateThreadGroup();
ultimateThreadGroup.setName(threadGroupName);
ultimateThreadGroup.setProperty(AbstractThreadGroup.ON_SAMPLE_ERROR, AbstractThreadGroup.ON_SAMPLE_ERROR_CONTINUE);
PowerTableModel dataModel = new PowerTableModel(UltimateThreadGroupGui.columnIdentifiers, UltimateThreadGroupGui.columnClasses);
dataModel.addRow(new Integer[]{2, 4, 10, 60, 10});
dataModel.addRow(new Integer[]{3, 4, 10, 120, 10});
CollectionProperty prop = JMeterPluginsUtils.tableModelRowsToCollectionProperty(dataModel, UltimateThreadGroup.DATA_PROPERTY);
ultimateThreadGroup.setData(prop);
ultimateThreadGroup.setEnabled(setEnabled);
ultimateThreadGroup.setProperty(TestElement.TEST_CLASS, UltimateThreadGroup.class.getName());
ultimateThreadGroup.setProperty(TestElement.GUI_CLASS, UltimateThreadGroupGui.class.getName());

Solution

    1. First you need to create a Loop Controller instance like:

      LoopController loopController = new LoopController();
      loopController.setLoops(1);
      loopController.setFirst(true);
      loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
      loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
      loopController.initialize();
      
    2. Second you need to add the Loop Controller from the step 1 to your Ultimate Thread Group:

      ultimateThreadGroup.setSamplerController(loopController);
      

    More information:

    Going forward just compare the .jmx which you generate programatically with the one which is created by JMeter GUI, you will be able to see what fields, properties, etc. are missing