cancel
Showing results for 
Search instead for 
Did you mean: 

Using a VAR in getData Statement

JamesRedman
Associate
Associate
0 Kudos
126

Hi All,

Does anyone know why this works:-

var TW = "36" ;

// Calculate the Shopper Stats

var ITEM_VAR = SHOPPER_STATS.getData("BASKET_ITEM_AVG", {"WEEK_NUM": TW }).value

But when I try to use a variable such as

// Get the max week

var WEEK_ARRAY = SHOPPER_STATS.getMembers("WEEK_NUM", 300) ;

var MAX_WEEK = 0 ;

WEEK_ARRAY.forEach(function(element, index) {

  if (Convert.stringToInt(element.text) > MAX_WEEK) {

  MAX_WEEK = Convert.stringToInt(element.text) ;

  }

}) ;

// Set this week and last week

var TW = MAX_WEEK+"" ;

// Calculate the Shopper Stats

var ITEM_VAR = SHOPPER_STATS.getData("BASKET_ITEM_AVG", {"WEEK_NUM": TW }).value

I get the error "Wrapped java.lang.NullPointerException: while trying to invoke the method java.lang.String.startsWith(java.lang.String) of a null object loaded from local variable 'memberKey'"

I have put TW into an alert and the number is shows me is 36 so I know that is the same.

Cheers,

James.

View Entire Topic
Karol-K
Product and Topic Expert
Product and Topic Expert
0 Kudos

can you try to find the line which is breaking the code (by commenting the lines - line by line).

check also the line:

var TW = MAX_WEEK+"" ;

should not be

var TW = "" + MAX_WEEK;

-> this should convert it to string, I do not know if your line is working correctly.


what is actually the value of "TW"  at the end?

JamesRedman
Associate
Associate
0 Kudos

It is breaking at the getData function. It doesn't seem to like the parameter if I set it as:-

var TW = MAX_WEEK+"" ;

I get the same error with:-

var TW = ""+MAX_WEEK ;


However, if I use


var TW = "36" ;


then it works OK.


I ran APPLICATION.alert(TW) and this shows 36 so I know that bit is OK.

Karol-K
Product and Topic Expert
Product and Topic Expert
0 Kudos

strange, this remands me on an issue where Strings were not correctly transformed. there was some thread in 1.4 SP0 on this, should be fixed in patch2, latest in 1.4 SP1. What are you on?

try this (just to check the situation):

1.

// this will not change the value itself, but can change the internal type of the variable...

var TW2 = TW.substring(0, TW.length);

2. other option.

* place somewhere a TEXT component and make additionally following.

TEXT_1.setText(TW2);

var TW3 = TEXT_1.getText();

option 2 definitelly will return a string.

now, pass the TW2 or TW3 to the function.

will this work?

Karol