-1
decibelChart.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            int chartValue;
                            decibelChart.setNoDataText("Drawing Decibel Chart Error !");
                            decibelChart.setDragEnabled(false);
                            decibelChart.setScaleEnabled(false);
                            decibelChart.setDrawGridBackground(false);
                            decibelChart.setPinchZoom(false);
                            decibelChart.setBackgroundColor(Color.TRANSPARENT);
                            decibelChart.setData(lineData);

                            if (mediaRecorderForDB != null) {
                                chartValue = mediaRecorderForDB.getMaxAmplitude();
                            } else if (mediaRecorder != null) {
                                chartValue = mediaRecorder.getMaxAmplitude();
                            } else
                                chartValue = 0;

                            LineData myData = decibelChart.getData();

                            if (myData != null) {
                                ILineDataSet set = myData.getDataSetByIndex(0);

                                if (set == null){
                                    LineDataSet dataSet = new LineDataSet(null,"Dynamic");
                                    dataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
                                    dataSet.setLineWidth(3f);
                                    dataSet.setColor(Color.WHITE);
                                    dataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
                                    dataSet.setCubicIntensity(1f);
                                    myData.addDataSet(dataSet);
                                }

                                myData.addEntry(new Entry(set.getEntryCount(), chartValue + 5), 0);
                                myData.notifyDataChanged();
                                decibelChart.setMaxVisibleValueCount(150);
                                decibelChart.moveViewToX(myData.getEntryCount());
                            }

                            Log.e("[CHART_ERROR]", "CHART DRAWING");

                        }
                    },250);

set.getEntryCount() returns null and throws exception. How can i get over it ? I tried a lot things but cannot achived. The values are coming from the microphone they are decibel values. I want to draw a line chart for these values.

a_local_nobody
  • 6,231
  • 5
  • 18
  • 42
  • 1
    you have `ILineDataSet set = myData.getDataSetByIndex(0);` and then `if (set == null)` but you never change the value of set if it _is_ null, so why _wouldn't_ it be null still – a_local_nobody Apr 30 '21 at 18:48
  • No it didn't answer my question. Still need help. – FanFiniFinFon Apr 30 '21 at 18:54
  • did you read my first comment, any response you'd like to make regarding that ? – a_local_nobody Apr 30 '21 at 18:55
  • 1
    Please undo your edit so that the answer below makes sense. You still need your question there to help future people. – NomadMaker Apr 30 '21 at 20:58
  • i've done a rollback like @NomadMaker suggested, please consider the fact that this community isn't just here to help you out, it's for people in future as well having the same issues – a_local_nobody Apr 30 '21 at 21:28

1 Answers1

0

Let's go over it line by line

if (myData != null) {  //here you ask if myData is NOT null
    ILineDataSet set = myData.getDataSetByIndex(0);
    if (set == null){ // here you ask if set IS null 
        LineDataSet dataSet = new LineDataSet(null,"Dynamic"); //If set IS null you create the dataset
        //....
        myData.addDataSet(dataSet);
    }
    myData.addEntry(new Entry(set.getEntryCount(), chartValue + 5), 0); //what happens if set is still null? It will thow exception
}

A possible solution would be

if (myData != null) {  //here you ask if myData is NOT null
    ILineDataSet set = myData.getDataSetByIndex(0);
    if (set == null){ // here you ask if set IS null 
        LineDataSet dataSet = new LineDataSet(null,"Dynamic"); //If set IS null you create the dataset
        //....
        myData.addDataSet(dataSet);
        myData.addEntry(new Entry(dataSet.getEntryCount(), chartValue + 5), 0); //here we are sure dataSet is not null because you created it
    } else { // this means set is NOT null
        myData.addEntry(new Entry(set.getEntryCount(), chartValue + 5), 0); //here we are sure set is not null because it is the ELSE for the IS NULL question
    }
    myData.notifyDataChanged();
    decibelChart.setMaxVisibleValueCount(150);
    decibelChart.moveViewToX(myData.getEntryCount());
}

Happy coding!

  • Thanks for your help. :) But i am getting "java.lang.NegativeArraySizeException: -6" error now do you have any idea ? I am getting these types of errors first time sorry for my silly questions. – FanFiniFinFon Apr 30 '21 at 20:21
  • Don't worry. This error means you have an array, which is basically a collection of things with an index that starts with 0 and ends with n-1 (n being the number of elements you have). For example an array of 4 letters could be [A|B|C|D] so, index 0 = A, index 1 = B, index 2 = C and index 3 = D. As you can see there are no negative indexes in an array so the error is saying that there is an array and it is trying to get the index -6 which, of course, does not exist. In this code I don't see where this could be happening so I think it happens on another place of your code. @FanFiniFinFon – Luis Guillermo Torres-Ribero Apr 30 '21 at 20:29