halfhp / androidplot

Charts and plots for Android
http://androidplot.com
Apache License 2.0
505 stars 159 forks source link

CatmullRomInterpolator fails with null Pointer Exception #66

Closed GremlinsInGame closed 6 years ago

GremlinsInGame commented 6 years ago

At codeLine 146 and 147 in CatmullRomInterpolator.java

int n = series.size() -1; dx = series.getX(n).doubleValue() - series.getX(n-1).doubleValue();

CodeLine 102: public int size() { return series.size() + 2; }

You override the size of the Series by +2 and then try to access thnull e index at n-1 which is 1 past the end of its bounds. This throws a nullPointer Exception and fails.
This originally was working in my application but now as started failing. Breakingpoint right before it fails shows Size is 21 but series only has 19 elements. then Element 20 is accessed which fails.

halfhp commented 6 years ago

Thanks for the report - will take a look

halfhp commented 6 years ago

@GremlinsInGame I'm not seeing the bug.

Breakingpoint right before it fails shows Size is 21 but series only has 19 elements. then Element 20 is accessed which fails.

Believe it or not, this is expected. ExtrapolatedXYSeries models it's data using an XYSeries plus two individual starting and ending points; this internal series size should always be two less than the actual series size. The implementation of getX(int) and getY(int) take this into account.

Also:

This originally was working in my application but now as started failing.

The last change to this code was in May of 2016, and that change was a simple name change, so it seems unlikely something recently broke in the library. That's not to say there isn't a bug, I'm just not seeing it. If you can provide a unit test or example of how to reproduce I'm happy to investigate further :)

halfhp commented 6 years ago

I added these basic unit tests for the CatmullRomInterpolator - feel free to add onto them.