1

Please check the below Android code

public void setTitles(final List<AppDataBean> appDataBeanList){
        dataBeanArrayList = new ArrayList<DataBean>();
        outer:for(a=0;a<appDataBeanList.size();a++)
        {
            appDataBeanListIndex=a;
            //Get data from the Bean list
            final AppDataBean appDataBean = appDataBeanList.get(a);
            if (InternetConnectivity.isConnectedToInternet(context)==true){
                try {
                    LinkReader linkReader = new LinkReader();
                    linkReader.onFinish(new LinkReader.OnTaskCompleted() {
                        @Override
                        public void onTaskCompleted(ArrayList<DataBean> list) {
                            inner:for (int i = 0; i < list.size(); i++) {
                                if (InternetConnectivity.isConnectedToInternet(context)== true){
                                    //Gather all information into DataBean
                                    DataBean dataBean = new DataBean();
                                    dataBean.setTitle(list.get(i).getTitle());

                                    //Add the DataBean to the bean list
                                    dataBeanArrayList.add(dataBean);


                                }

                                if(i==4)
                                {
                                    break outer;;
                                }
                            }
                        }
                        @Override
                        public void onError() {
                        }
                    });
                }
                catch(NullPointerException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

In here,notice how I have labelled the for loops as outer and inner. Inside the second loop I am trying to break the outer loop when a certain condition is met, but it is not working because it is giving the compile error "undefined label: outer".

Why is this? I tried to wrap the outer content with curly bracers, which then covers inner section too, and I tried without the curly bracers, the same error.

PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • 4
    Just a small comment, you have WAY too many nested loops and ifs in this method. You might want to simply and clean up your code a bit in order to help with debugging. – Jacob B. Feb 27 '18 at 05:13
  • 1
    That is an ***anonymous* inner `class`**. The block isn't local to that class. – Elliott Frisch Feb 27 '18 at 05:15
  • @ElliottFrisch: There we go, how to handle this then? – PeakGen Feb 27 '18 at 05:15
  • 1
    The loop will have completed long before that `onFinish` actually triggers so I'm not sure why you're trying to `break` it. – Elliott Frisch Feb 27 '18 at 05:18
  • Yes, that argument is right. I am trying to break it because I am trying to read just 4 records. But yes, I can adjust the loop index directly, hmm, didn't come to my mind still now.... – PeakGen Feb 27 '18 at 05:22
  • 1
    Possible duplicate https://stackoverflow.com/questions/6638321/how-to-exit-two-nested-loops – duggu Feb 27 '18 at 05:35
  • take a field `check_inner = false;` and `on load finished` make `check_inner = true;` and finally in tge outer loop make a checking `if(!check_inner) a--;` i think this will work..update if it works. – Santanu Sur Feb 27 '18 at 05:38

1 Answers1

2

From the Oracle docs

The break statement terminates the labeled statement; it does not transfer the flow of control to the label. Control flow is transferred to the statement immediately following the labeled (terminated) statement.

which means that you cannot treat it as goto.

This post gives following example:

first:
for( int i = 0; i < 10; i++) {
  second:
  for(int j = 0; j < 5; j ++ )
  {
    break xxx;
  }
}

third:
for( int a = 0; a < 10; a++) {

}

You could replace xxx with first or second since both loops are being executed, but replacing xxx with third won't compile.

Now in your case, an anonymous inner class is getting created here:

linkReader.onFinish(new LinkReader.OnTaskCompleted() {
                        @Override
                        public void onTaskCompleted(ArrayList<DataBean> list) {

and you are trying to break within the scope of this inner class:

if(i==4)
{
   break outer;
}

If you move your condition outside the scope of this inner class, it would work.

tryingToLearn
  • 10,691
  • 12
  • 80
  • 114