PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.64k stars 9.01k forks source link

[Bug] NPE : attempt to read from null array #3744

Open trashCoder555 opened 6 years ago

trashCoder555 commented 6 years ago

Drawing combined chart with line and bara data. All procedure with graph in try-catch block and still crashes. Is it expected behaviour?

(Off top. There also was errors, if i put in combined chart only line data after drawing it with line+bar data (loaded line+bar, changed in settings to only line, refreshed page -> crash). So i solved it just hiding bars, not best solution because of some useless operations, but it works)

Where i can place "if" condition to not draw graph in this case?

Exception java.lang.NullPointerException: Attempt to read from null array com.github.mikephil.charting.renderer.BarChartRenderer.drawDataSet (BarChartRenderer.java:136) com.github.mikephil.charting.renderer.BarChartRenderer.drawData (BarChartRenderer.java:80) com.github.mikephil.charting.renderer.CombinedChartRenderer.drawData (CombinedChartRenderer.java:89) com.github.mikephil.charting.charts.BarLineChartBase.onDraw (BarLineChartBase.java:231) android.view.View.draw (View.java:15122) android.view.View.updateDisplayListIfDirty (View.java:14056) android.view.View.getDisplayList (View.java:14079) android.view.View.draw (View.java:14846) android.view.ViewGroup.drawChild (ViewGroup.java:3404) android.view.ViewGroup.dispatchDraw (ViewGroup.java:3198) android.view.View.updateDisplayListIfDirty (View.java:14051) android.view.View.getDisplayList (View.java:14079) android.view.View.draw (View.java:14846) android.view.ViewGroup.drawChild (ViewGroup.java:3404) android.view.ViewGroup.dispatchDraw (ViewGroup.java:3198) android.view.View.draw (View.java:15125) android.view.View.updateDisplayListIfDirty (View.java:14056) android.view.View.getDisplayList (View.java:14079) android.view.View.draw (View.java:14846) android.view.ViewGroup.drawChild (ViewGroup.java:3404) android.support.v7.widget.RecyclerView.drawChild (RecyclerView.java:4581) android.view.ViewGroup.dispatchDraw (ViewGroup.java:3198) android.view.View.draw (View.java:15125) android.support.v7.widget.RecyclerView.draw (RecyclerView.java:3987) android.view.View.updateDisplayListIfDirty (View.java:14056) android.view.View.getDisplayList (View.java:14079) android.view.View.draw (View.java:14846) android.view.ViewGroup.drawChild (ViewGroup.java:3404) android.view.ViewGroup.dispatchDraw (ViewGroup.java:3198) android.view.View.draw (View.java:15125) android.view.View.updateDisplayListIfDirty (View.java:14056) android.view.View.getDisplayList (View.java:14079) android.view.View.draw (View.java:14846) android.view.ViewGroup.drawChild (ViewGroup.java:3404) android.view.ViewGroup.dispatchDraw (ViewGroup.java:3198) android.view.View.draw (View.java:15125) android.view.View.updateDisplayListIfDirty (View.java:14056) android.view.View.getDisplayList (View.java:14079) android.view.View.draw (View.java:14846) android.view.ViewGroup.drawChild (ViewGroup.java:3404) android.support.design.widget.CoordinatorLayout.drawChild (CoordinatorLayout.java:1229) android.view.ViewGroup.dispatchDraw (ViewGroup.java:3198) android.view.View.updateDisplayListIfDirty (View.java:14051) android.view.View.getDisplayList (View.java:14079) android.view.View.draw (View.java:14846) android.view.ViewGroup.drawChild (ViewGroup.java:3404) android.view.ViewGroup.dispatchDraw (ViewGroup.java:3198) android.view.View.draw (View.java:15125) android.support.v4.view.ViewPager.draw (ViewPager.java:2418) android.view.View.updateDisplayListIfDirty (View.java:14056) android.view.View.getDisplayList (View.java:14079) android.view.View.draw (View.java:14846) android.view.ViewGroup.drawChild (ViewGroup.java:3404) android.view.ViewGroup.dispatchDraw (ViewGroup.java:3198) android.view.View.updateDisplayListIfDirty (View.java:14051) android.view.View.getDisplayList (View.java:14079) android.view.View.draw (View.java:14846) android.view.ViewGroup.drawChild (ViewGroup.java:3404) android.support.v4.widget.DrawerLayout.drawChild (DrawerLayout.java:1366) android.view.ViewGroup.dispatchDraw (ViewGroup.java:3198)

getsadzeg commented 6 years ago

Could you post a code snippet, too? and please, style with Markdown.

trashCoder555 commented 6 years ago
private void installGraph(final WatchlistTabAdapter.TGViewHolder holder, final String symbol) {

    Thread graphThread = new Thread(new Runnable() {
        @Override
        public void run() {

            try {

                final CombinedChart combinedChart = holder.v.findViewById(R.id.graph_coin_price);

                Activity activity = ((Activity) holder.v.getContext());

                if (activity != null) {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            combinedChart.setVisibility(View.GONE);
                        }
                    });
                }

                ArrayList<GraphPoint> graphData = getGraphData(symbol);

                if (graphData == null || graphData.size() == 0) {
                    return;
                }

                combinedChart.setTouchEnabled(false);

                combinedChart.setHighlightPerDragEnabled(false);
                combinedChart.setHighlightPerTapEnabled(false);

                combinedChart.getLegend().setEnabled(false);
                combinedChart.getDescription().setEnabled(false);

                combinedChart.getAxisLeft().setEnabled(false);
                combinedChart.getAxisRight().setEnabled(false);
                combinedChart.getXAxis().setEnabled(false);

                combinedChart.setScaleEnabled(false);

                combinedChart.setDoubleTapToZoomEnabled(false);

                combinedChart.setNoDataText(App.getContext().getResources().getString(R.string.graph_no_data));

                List<Entry> values = new ArrayList<>();

                if (PropertiesHelper.getWatchlistConvertSymbol().equals("ETH")) { //eval graph with Open price instead of Close
                    for (int i = 0; i < graphData.size(); ++i) {
                        values.add(new Entry((float) i, graphData.get(i).open.floatValue()));
                    }
                } else {
                    for (int i = 0; i < graphData.size(); ++i) {
                        values.add(new Entry((float) i, graphData.get(i).close.floatValue()));
                    }
                }

                LineDataSet lineDataSet = new LineDataSet(values, symbol);
                lineDataSet.setDrawValues(false);
                lineDataSet.setDrawCircles(false);
                lineDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);

                lineDataSet.setColor(context.getResources().getColor(R.color.graph_wl_color));
                lineDataSet.setLineWidth(1.4f);

                LineData lineData = new LineData(lineDataSet);

                CombinedData combinedData = new CombinedData();
                combinedData.setData(lineData);

                if (combinedData.getEntryCount() == 0) {
                    return;
                }

                List<BarEntry> valuesVolumeData = new ArrayList<>();

                for (int i = 0; i < graphData.size(); ++i) {
                    valuesVolumeData.add(new BarEntry((float) i, graphData.get(i).volume.floatValue()));
                }

                BarDataSet barDataSet = new BarDataSet(valuesVolumeData, context.getResources().getString(R.string.volume));
                barDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
                barDataSet.setColor(context.getResources().getColor(R.color.watchlist_volumes_bar));

                BarData barData = new BarData(barDataSet);
                combinedData.setData(barData);

                barDataSet.setVisible(showVolumes);

                combinedChart.setData(combinedData);

                ((Activity) holder.v.getContext()).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        combinedChart.setVisibility(View.VISIBLE);
                        combinedChart.invalidate();
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                FirebaseCrash.report(e);
            }
        }

    });

    graphThread.start();
}
akhilchakka-upl commented 8 months ago

Hi I am even facing the same issue can anyone let me know why is this issue coming