Seeing “runtime error: ‘chart’ is null or not an object”

56 Views Asked by At

Active window on the application is a chart but when i run the javascript using cscript Amibroker.js, i see error:Microsoft JScript runtime error: 'chart' is null or not an object on line chart.GotoDateTime(dateTime);

// Define the year, month, and date
var year = 2023;
var month = 11;
var date = 11;

// Create a Date object
var dateObj = new Date(year, month - 1, date); // Subtract 1 from month

// Create the main OLE automation object for AmiBroker
var AB = new ActiveXObject("Broker.Application");

// Get the active chart
var chart = AB.ActiveDocument.ActiveWindow.Chart;

// Format the date in the required format
var dateTime = dateObj.getFullYear() + "-" + ("0" + (dateObj.getMonth() + 1)).slice(-2) + "-" + ("0" + dateObj.getDate()).slice(-2) + " 00:00:00";

// Go to the specific date
chart.GotoDateTime(dateTime);
WScript.Echo(dateTime);
1

There are 1 best solutions below

5
On

The error message you're encountering, "Microsoft JScript runtime error: chart is null or not an object," indicates that the chart object is not being retrieved correctly from the AmiBroker application. This could be due to a few reasons, such as no active chart being open or the AmiBroker application not being properly initialized.

Try using a delay: There might be a timing issue where the script is trying to access the chart object before it's fully available. You can try adding a delay before attempting to access the chart object, using the WScript.Sleep method.

For example:

// Create the main OLE automation object for AmiBroker
var AB = new ActiveXObject("Broker.Application");

// Wait for a few seconds to ensure the application is fully loaded
WScript.Sleep(2000);  // Wait for 2 seconds

// Get the active chart
var chart = AB.ActiveDocument.ActiveWindow.Chart;