Bean-oriented method problem

Hi,

I’m experiencing a strange problem with a method that uses the PieChart bean.
If I launch the method clicking on a button in the form, I get an error (but the chart is updated anyway) telling me that “Cannot convert null to an object.”
But if I execute the method from the editor, the error doesn’t show up.
The method is:

globals.resocontopiani = ""
var query_anno = ""
var query_anno = "select distinct yearnr from piani order by yearnr"// era "select yearnr from ordini order by yearnr"
var dataset_anniOrd = databaseManager.getDataSetByQuery(controller.getServerName(), query_anno, null, 100000);
var anni_ordini = dataset_anniOrd.getColumnAsArray(1)
var sceltannata = ""
var sceltannata = plugins.dialogs.showSelectDialog("Attenzione","Scegli l'anno",anni_ordini)

if(elements['pieChart'] != null)
{


var maxReturedRows = 10000;


var	query = "SELECT"+
	" ri.categoria, COUNT(*) as TotPrestazioni, SUM(ri.prezuni)"+
	" from rigpiani ri"+
	" where ri.annoprest =" + sceltannata  +
	" and ri.categoria <> 'NULL'" +
	" group by ri.categoria" +
	" order by 1 asc"

//clear pieChart
for(var i = 1 ; i <= 25 ; i++) //
	{
	elements.pieChart.setLegends(i-1,"")// set legends of chart
	elements.pieChart.setValues(i-1, 0, 0)
	}

var dataset = databaseManager.getDataSetByQuery(controller.getServerName(), query, null, maxReturedRows);


for( var i = 1 ; i <= 25 ; i++ ) //
{
	dataset.rowIndex = i;
		elements.pieChart.setLegends(i-1,dataset[1]+" ("+ dataset[2]+") "+ dataset[3].toFixed(2) )// set legends of chart
		elements.pieChart.setValues(i-1, 0, dataset[3])
	}
}

else
{
	HTML = HTML+'<tr class="normal"><td nowrap colspan=2>JavaBean non riconosciuto.</td></tr>';
}
globals.resocontopiani = HTML
controller.show();

OSX.2.8, Servoy 2RC3

very diffictult to say by just looking at that script.
we need a complete example for that.

You’re setting a loop for 25 times, but maybe your dataset has less results?

for( var i = 1 ; i <= 25 ; i++ ) // 
{ 
   dataset.rowIndex = i; 
      elements.pieChart.setLegends(i-1,dataset[1]+" ("+ dataset[2]+") "+ dataset[3].toFixed(2) )// set legends of chart 
      elements.pieChart.setValues(i-1, 0, dataset[3]) 
   } 
}

maarten:
You’re setting a loop for 25 times, but maybe your dataset has less results?

for( var i = 1 ; i <= 25 ; i++ ) // 

{
dataset.rowIndex = i;
elements.pieChart.setLegends(i-1,dataset[1]+" (“+ dataset[2]+”) "+ dataset[3].toFixed(2) )// set legends of chart
elements.pieChart.setValues(i-1, 0, dataset[3])
}
}

Yes. I set the loop to 25 times to be sure to include all the possible values.
Could this be causing the error message?

yes,

if your dataset only has eg. 10 rows, the loop will start giving errors after the 10th loop.

it’s better to start your loop with:

for( var i = 1 ; i <= dataset.getMaxRowIndex() ; i++ )

instead of:

for( var i = 1 ; i <= 25 ; i++ ) //

maarten:
yes,

if your dataset only has eg. 10 rows, the loop will start giving errors after the 10th loop.

it’s better to start your loop with:

for( var i = 1 ; i <= dataset.getMaxRowIndex() ; i++ )

instead of:

for( var i = 1 ; i <= 25 ; i++ ) //

You got it :smiley:
Now the problem’s gone: thanks a lot :wink: