Bug 47646 - NullPointerException in the "Random Variable" element
NullPointerException in the "Random Variable" element
Status: RESOLVED FIXED
Product: JMeter
Classification: Unclassified
Component: Main
2.3.4
All All
: P2 major with 1 vote (vote)
: ---
Assigned To: JMeter issues mailing list
:
Depends on:
Blocks:
  Show dependency tree
 
Reported: 2009-08-05 03:24 UTC by Miguel Parreño
Modified: 2009-09-17 03:41 UTC (History)
1 user (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Miguel Parreño 2009-08-05 03:24:57 UTC
In a "Random Variable" element set true the "Per Thread(User)" option, and launch test plan in distributed mode (Remote Start).

ERROR - jmeter.threads.JMeterThread: Test failed! java.lang.NullPointerException

	at org.apache.jmeter.config.RandomVariableConfig.iterationStart(RandomVariableConfig.java:90)

	at org.apache.jmeter.control.GenericController.fireIterationStart(GenericController.java:318)

	at org.apache.jmeter.control.GenericController.fireIterEvents(GenericController.java:308)

	at org.apache.jmeter.control.GenericController.next(GenericController.java:128)

	at org.apache.jmeter.control.GenericController.nextIsAController(GenericController.java:182)

	at org.apache.jmeter.control.GenericController.next(GenericController.java:146)

	at org.apache.jmeter.control.GenericController.nextIsAController(GenericController.java:185)

	at org.apache.jmeter.control.GenericController.next(GenericController.java:146)

	at org.apache.jmeter.control.GenericController.nextIsAController(GenericController.java:182)

	at org.apache.jmeter.control.GenericController.next(GenericController.java:146)

	at org.apache.jmeter.control.GenericController.nextIsAController(GenericController.java:182)

	at org.apache.jmeter.control.GenericController.next(GenericController.java:146)

	at org.apache.jmeter.control.LoopController.next(LoopController.java:109)

	at org.apache.jmeter.threads.ThreadGroup.next(ThreadGroup.java:129)

	at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:242)

	at java.lang.Thread.run(Unknown Source)

The method:

public void iterationStart(LoopIterationEvent iterEvent) {
        Random randGen=null;
        if (getPerThread()){
            randGen = (Random) perThreadRandom.get();          <------ At line 90, the perThreadRandom object IS NULL.
        } else {
            synchronized(this){
                if (globalRandom == null){
                    init();
                    globalRandom = new Random(getRandomSeedAsLong());
                }
                randGen=globalRandom;
            }
        }
        if (n <=0){
            return;
        }
       long nextRand = minimum + randGen.nextInt(n);
       // Cannot use getThreadContext() as we are not cloned per thread
       JMeterVariables variables = JMeterContextService.getContext().getVariables();
       variables.put(getVariableName(), formatNumber(nextRand));
    }

The declaration:

private transient ThreadLocal perThreadRandom = new ThreadLocal() {
            protected Object initialValue() {
                init();
                return new Random(getRandomSeedAsLong());
            }};


¿Why transient? Javadoc says that ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread. However, in this case not in possible, because init() and getRandomSeedAsLong() are non-static methods in the class. I comment out these calls, set static the declaration and work fine.

References: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ThreadLocal.html
I use: java version "1.6.0_14"
Comment 1 Sebb 2009-08-05 17:32:32 UTC
Thanks for the report.

The perThreadRandom field cannot be static.
There may be several instances of the Random Config element and each one needs its own PRNG.

The field must be transient for the same reason.

Solution is to use readResolve() to create the field.

URL: http://svn.apache.org/viewvc?rev=801478&view=rev
Log:
Bug 47646 -  NullPointerException in the "Random Variable" element
Comment 2 Sebb 2009-09-17 03:41:53 UTC
Resolved in SVN; code is in builds after (and including) r801478