You have to set the process variable to save historical data before you attempt to read the count of the historical data. That is done using the setDatasetEnableUpdate method, as seen in our scrolling examples. You don't have to set the AutoTruncateMin and Max values.
If you plan to check process variables that may have not been initialized for historical data yet, check for null first and return 0 in that case.
if (processvar1 != null)
if (processvar1.getProcessVarDataset() != null)
n = processvar1.getProcessVarDataset().getNumberDatapoints();