Why is it an error?

Ardi1899

New member
Joined
Jan 17, 2025
Posts
3
Likes
0
Hi all.. I'm new here so please bear with me.
I'm relatively new in Java but a bit experienced in Thinkscript and Pinescript.

I'm stuck and very confused at the moment. Been trying to find any solution for days but unsuccessful so I seek help.
It just a short code, please let me know if it needs more.

It's a study and when I do this:

Double lossema=series.ema(index,atrPeriod,Values.TRUERANGE);

all good, lines visible on the chart.
But when I do this:

Double lossema=1.5*series.ema(index,atrPeriod,Values.TRUERANGE);

Suddenly I have this error:
"java.lang.NullPointerException: Cannot invoke "java.lang. Double.doubleValue()" because "<local33>" is null at study_examples.ATR_TrailingStop.calculate(ATR_TrailingStop.java:129)"

It should be very simple and yet I can't solve it. Anyone can tell me what's wrong?
Thank you
 
Double lossema=series.ema(index,atrPeriod,Values.TRUERANGE);
....

Do println of the lossema. It must be null.
When you do 1.5*series.ema(index,atrPeriod,Values.TRUERANGE) the Double object gets converted into double and this where your get the error.

cheers.
 
Double lossema=series.ema(index,atrPeriod,Values.TRUERANGE);
....

Do println of the lossema. It must be null.
When you do 1.5*series.ema(index,atrPeriod,Values.TRUERANGE) the Double object gets converted into double and this where your get the error.

cheers.
Thanks!

I kinda guess it too but talking to you I get an idea.

So I do this:

if(lossema!=null) lossema=lossema*1.5;

and it works!
But perhaps there's a better solution?
 
Last edited:
Top