more wierdness with session vars

I've been coming across all sorts of inconsistent behaviour with session variables lately... The latest problem is that I have an initialize script which sets a number of session variables that need to be referred to in varous scripts throughout the session.

In particular I've set the following:
session.setVariable("MAX_POSTS",50);

now in some scripts I can access the variable directly as a parameter for another method i.e. session.setVariable("Iterations",session.getVariable("MAX_POSTS"));

Later on in the session I have to resort to:
x=session.getVariable("MAX_POSTS");
session.setVariable("Iterations",x);

even further on even attempting to send MAX_POSTS straight to the log or any reference at all (like the x= line from the previous example) results in the following error:

Sourced file:

I've seen different error messages along the way, including nullpointerExceptions but this is the only one I can reproduce at the moment...

I know it's not a syntax issue because if I comment out these lines everything runs ok. The exact same line copied and pasted to an earlier running script will work ok... If I replace the session.getVariable statements with literal values it also works.

This particular session starts with an init script (setting numerous session vars) then a couple of scrapeable files running in sequence then quite a few more scrapeable files called from an iterative script. It's all within in a single session so I don't there's any scope issues.

I'm running this in the latest alpha version of the pro version eval... 4.0.57a

Any insight would be appreciated...

As a side note, given a lot of the problem I've had to work out recently I think one of the most useful bits of documentation SS could produce would be an explicit explanation of how the SS classes handle data types. This would take a lot of the guesswork out of some of these problems.

cheers

Steve

Hi Tim, All that code had

Hi Tim,

All that code had changed too much for it to be useful to copy it in now. But I did come across exactly the same problem in another instance. This time with the session Var "CRAWL_DELAY".

It's set in an initialisation script which is copied below from start until the line where it's set. It's not referenced again in this script after that.


// Forum parameters
session.setVariable("SOURCE_URL","http://url.com");
session.setVariable("USERNAME","uname");
session.setVariable("PASSWORD","password");
session.setVariable("BBCODE_UID","9e5eb91b8e");

// output file
session.setVariable("OUTPUT_PATH","c:/fes/");
session.setVariable("OUTPUT_FILE_PREFIX","fes_");

//error handling
session.setVariable("CRAWL_DELAY",5000);

Then in several other scripts it is referenced:


crawlDelay=session.getVariable("CRAWL_DELAY");
session.pause(crawlDelay); //called multiple times within the same script

I got around it by explicitly typing the value before I set it:

//Init Script extract...

//error handling
long crawlDelay = 5000;
session.setVariable("CRAWL_DELAY",crawlDelay);

//script where it's referenced

long crawlDelay=session.getVariable("CRAWL_DELAY");
session.pause(crawlDelay);

an interesting couple of experiments I did though... I tried the following:

//Init Script extract...

//error handling
session.setVariable("CRAWL_DELAY",5000);

//script where it's referenced

crawlDelay=(session.getVariable("CRAWL_DELAY")).longValue();
session.pause(crawlDelay);

as well as:

//Init Script extract...

//error handling
Integer crawlDelay = 5000;
session.setVariable("CRAWL_DELAY",crawlDelay);

//script where it's referenced

crawlDelay=session.getVariable("CRAWL_DELAY");
session.pause(crawlDelay.longValue());

both with the same error message. It was only when I explicity typed the value as 'long' before putting it in the session variable that I could get it to work.

Come to think of it.. I can't

Come to think of it.. I can't remember if I've ever called a sessionVariable directly into a session.pause call.

I was just looking over some source code for screen-scraper, and session.pause() takes a "long" as an argument... Looks like Beanshell is converting inline integers into longs, but when you call a sessionVariable, it's really looking up something in a Hashtable, and then giving an address in memory. Beanshell has failed us :P

So for now, either put your session variable into a local variable (as you've done before in your examples) so that Beanshell can stick its fingers in it, or you could try doing this:

// Init script extract

session.setVariable("CRAWL_DELAY",5000);

// script where it's referenced

session.pause( (long)session.getVariable("CRAWL_DELAY") );

Hopefully casting the int into a long just before it goes off to the pause method should help out.

I'll see if I can push a session.pause(int) method into the next alpha, since there's only a method for taking in longs for now.

Thanks for helping track this down.
Tim

That's an odd error.. can you

That's an odd error.. can you post your script where you initialize the variable for the first time? Don't cut anything out before setting the variable... There's got to be something strange.

That whole "inline eval" including an import statement suggests that your session variable maybe includes some runaway text somehow.

I'm not sure how much there is to explain about SS's data types, at the end-user level. Session variables are kept in a Hashtable object. DataRecords are almost literally a Hashtable as well. It's only extended to block out methods that would throw strange GUI errors, such as putting a null into a variable. (Instead, we just remove the key.)

A DataSet is.. an ArrayList of DataRecord objects, it seems, with extra methods, yada yada.

At any rate, I've used the sorts of things you're trying to do in your scripts, and the behavior is definitely not normal. I'm kind of hoping that the init script will reveal something to work off of...