Cannot Convert String to Java INT

I have this script that creates a dataset. Runs fine in the script console with the same path provided, but I get an type conversion error when using runScript() on an expression dataset tag.

def getDistributorDropdownDataset(path):
	'''
		Get the position values and strings and return it as a dataset for use in a dropdown selector.
		
		Args:
			path	(string)	:	string path to the dataset for the dropdown data
			
		Returns:
			dataset	(dataset)	:	dataset of options
	'''
	parentTagPath = path.rsplit('/',1)[0]
	tagsToRead = [parentTagPath+'/'+str(num) for num in range(1,17)]
	
	destinations = [tag.value for tag in system.tag.readBlocking(tagsToRead)]
	
	headers = ['POS', 'DEST']
	data = []
	
	for i, destination in enumerate(destinations):
		data.append([i+1, destination])
		
	return system.dataset.toDataSet(headers, data)

Cannot convert "path" into into type: class java.lang.Integer

Why is it trying to convert the path into an integer? Where, even?

me too I can't see error :laughing:
Show us logs (error line)

I would have to assume that one of the tag's that you're reading's value is actually "path" (unfortunate that it matches the function parameter name). Generally, you'll see this type of error when creating a dataset if the values in your data list are not of the same types.

The to*DataSet() functions will assume the type of the value in the first row for that column.

So for instance, if the first tag value is an integer, then the column will be typed as an integer column. If then next value is a string, you will get this error.

If you have a mix of integer values and string values then you will need to use the DatasetBuilder to explicitly specify that the column type should be String.

from com.inductiveautomation.ignition.common.util import DatasetBuilder
from java.lang import Integer, String

def getDistributorDropdownDataset(path):
    parentTagPath = path.rsplit('/',1)[0]
    tagsToRead = [parentTagPath+'/'+str(num) for num in range(1,17)]
	
    destinations = [tag.value for tag in system.tag.readBlocking(tagsToRead)]

    builder = DatasetBuilder.newBuilder()
    builder.colNames(["POS","DEST"])
    builder.colTypes([Integer, String])

    for i, destination in enumerate(destinations):
        builder.addRow(i,destination)

    return builder.build()
2 Likes

The path is not actually "path" that's just pseudo-code.

It makes less that 0 sense that it works in the script console but not from the runScript()

The very first row is Integer, String. I've even added type conversions just to be sure.

def getDistributorDropdownDataset(path):
	'''
		Get the position values and strings for a distributor and return it as a dataset for use in a dropdown selector.
		
		Args:
			path	(string)	:	string path to the dataset for the dropdown data
			
		Returns:
			dataset	(dataset)	:	dataset of options
	'''
	parentTagPath = path.rsplit('/',1)[0]
	tagsToRead = [parentTagPath+'/'+str(num) for num in range(1,17)]
	
	destinations = [str(tag.value) for tag in system.tag.readBlocking(tagsToRead)]
	
	headers = ['POS', 'DEST']
	data = []
	
	for i, destination in enumerate(destinations):
		row = [int(i+1), destination]
		print row
		data.append(row)
		
	return system.dataset.toDataSet(headers, data)


You're using a tag path there as if it's going to give you tag value.
Edit: Wow i need coffee
Edit Edit: Wait no, the error explicitly states the value it's attempting to coerce, which looks like a tag path.

1 Like

That error is not being generated by your script, it's in the evaluation of the expression.

Show your expression.

Just figured that out.

Legacy call works like this

runScript('Grain.Data.getDistributorDropdownDataset("'+{[.]DropdownData.path}+'")')

(I have a custom string property "path"

Yeah, that should work.

The preferred syntax would be:

runScript('Grain.Data.getDistributorDropdownDataset',0,{[.]DropdownData.path})

Feels odd to me to be keeping dropdown data in a tag, as opposed to a top level script variable, but I'm sure you have your reasons.

2 Likes

What's the "0" ?

The poll rate. Looking at the preferred syntax:
runScript(scriptFunction, [pollRate],[arg1],[arg2],[arg...])

So, if you need to pass any arguments, you need to specify a poll rate.

3 Likes