【问题标题】:A method always returning null [duplicate]一个总是返回 null 的方法 [重复]
【发布时间】:2021-07-24 02:21:43
【问题描述】:
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() 返回 null 并抛出异常。我怎样才能克服它?我尝试了很多东西,但无法实现。 这些值来自麦克风,它们是分贝值。我想为这些值绘制折线图。

【问题讨论】:

  • 你有ILineDataSet set = myData.getDataSetByIndex(0);,然后是if (set == null),但如果它 null,你永远不会改变set的值,那么为什么不会它仍然是空的
  • 不,它没有回答我的问题。仍然需要帮助。
  • 您是否阅读了我的第一条评论,您对此有何回应?
  • 请撤消您的编辑,以便下面的答案有意义。你仍然需要你的问题来帮助未来的人。
  • 我已经按照@NomadMaker 的建议进行了回滚,请考虑这样一个事实,即这个社区不仅仅是为了帮助你,它也是为了未来的人们以及遇到同样问题的人

标签: java android methods


【解决方案1】:

让我们逐行回顾一下

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
}

一个可能的解决方案是

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());
}

编码愉快!

【讨论】:

  • 感谢您的帮助。 :) 但是我现在收到“java.lang.NegativeArraySizeException:-6”错误你知道吗?对于我的愚蠢问题,我第一次遇到这些类型的错误。
  • 别担心。这个错误意味着你有一个数组,它基本上是一个索引以 0 开头并以 n-1 结尾的东西的集合(n 是你拥有的元素数)。例如,一个由 4 个字母组成的数组可以是 [A|B|C|D] 所以,索引 0 = A,索引 1 = B,索引 2 = C 和索引 3 = D。正如您所见,在一个数组,所以错误是说有一个数组,它试图获取索引-6,当然,它不存在。在这段代码中,我看不到这可能发生在哪里,所以我认为它发生在代码的另一个地方。 @FanFiniFinFon
猜你喜欢
  • 2020-08-16
  • 2011-02-17
  • 2019-08-05
  • 1970-01-01
  • 2016-07-10
  • 2016-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多