lightweight-charts: Improve error message for: Error: Value is null at ensureNotNull (lightweight-charts.development.mjs:205:15)

Lightweight Charts™ Version:

4.2.0 🚭

Steps/code to reproduce:

I have a React Hook that updates lines, candles and Area chart based on different states of my app.

It generally works but sometimes i get that error

Error: Value is null at ensureNotNull (lightweight-charts.development.mjs:205:15)

When I look through the stack trace, the further I can go is a requestAnimationFrame; making it very hard to figure out where the null value is coming from.

Ideally, the error message would have a label indicating what kind of value is null, making it easier to debug and figure out where the null value is coming from.

When this happens, the entire chart doesn’t render, which is a big issue. It also makes me wonder if some sort of try/catch should exist in the library so that other parts of the chart would still render and not get affected if only a LINE or an AREA chart has a null value.

I’m assuming it’s very unlikely the problem is with the candles as the candles actually don’t change so it would be really helpful if at least the candles would render.

Still, there is a chance a candle that comes with null from a third-party API from time to time could have a null value (I’m still figuring it out).

Unfortunately, this error occurs very rarely, making it difficult to reproduce and determine the source of the problem.

Could we add a string to this error pointing out which type of value or series the problem is?

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (7 by maintainers)

Most upvoted comments

Had same error with no clue where to start digging in to resolve issue.

TLDR:

Hook with this triggers the problem:

mainSeriesRef.current?.setData(candles)

Hook with this, doesn’t trigger the problem:

mainSeriesRef.current?.setData([])
mainSeriesRef.current?.setData(candles)

Conclusion: maybe internally when new candles are set the previous candles could be erased the same way i did.

This was helpful. Thanks

If you were to keep track of the last timestamp of data that you’ve provided to the library then you could do something like this (types might be wrong since I’m doing this from memory):

function setNewData(newData: LineData[], currentLastTimestamp: Time, series: ISeriesApi<'Line'>): Time {
   // assuming that your 'Time' is a timestamp number, then
  const updateData = newData.filter((d: LineData) => d.time >= currentLastTimestamp);
  updateData.forEach((d: LineData) => {
    /*
        you can only use a single data point for update,
        so we can do this in a loop if we have potentially
        more than one point
    */
    series.update(d);
  });
  return updateData[updateData.length - 1].time; // new current last timestamp
}

The library isn’t responsible for the cloning of the data, and it will use the values as provided.

With setData we aren’t merging it with an existing array or keeping the old array for anything. So there isn’t really anything the library can do extra here besides making it’s own copy of the data as soon as it is provided to the library.

The reason why we don’t do this is performance and memory usage. Doing an extra clone of data takes a bit of CPU time, and the data is then duplicated in the memory. For most use cases this overhead isn’t required, and for the few use-cases where it is required (like here with a React hook) then the responsibility for providing data which won’t be mutated can be handled externally to the library.

TLDR; we don’t do internal cloning of the data provided for performance reasons.

const candles = [{ time: 1, value: 10 }];

// mutate the data (either externally by your code, react, or internally by the library)
candles[0].value = 20;

const newCandles = [...candles, { time: 2, value: 10 }];
console.log(newCandles); // → [{ time: 1, value: 20 }, { time: 2, value: 10 }]

👆 new candles still contains the modified values

Clearing the candles array before creating the newCandles array will have a different end result:

let candles = [{ time: 1, value: 10 }];
candles[0].value = 20;

candles = [];
const newCandles = [...candles, { time: 2, value: 10 }];
console.log(newCandles); // → [{ time: 2, value: 10 }]

👆 new candles doesn’t contain any of the old candles

Sure thing, i understand all of that.

What i suggested tough is when we do setData for the candles the library should internally “erase” the previous array.

In my case i do setData([]) to avoid that problem which i guessed the library could also do internally to avoid the problem i was having.

TLDR:

Hook with this triggers the problem:

    mainSeriesRef.current?.setData(candles)

Hook with this, doesn’t trigger the problem:

mainSeriesRef.current?.setData([])
mainSeriesRef.current?.setData(candles)

Conclusion: maybe internally when new candles are set the previous candles could be erased the same way i did.