简体   繁体   中英

Break statement doesn't work

This is a small part of my code. Here the break statement doesn't work. The if condition is executed but the break statement takes the control to the start of the while loop. "assign" and "clsAssign" are two array list. "clustersRefGlobal()" is a function and I don't want to pass "assign" when it is empty. However due to break not working it is called even when "assign" is empty. I am not sure why break statement doesn't stop the while loop

   Wh:while (i < n) {
        System.out.println("Start");
        get = clustersRefGlobal(assign);
        clsAssign.add(get.get(0));
        assign = get.get(1);
        if(assign.isEmpty()){
            System.out.println("Inside");
            break Wh;
        } 
        System.out.println("End");
        i++;
    }

Here is the output

Start

End

Start

Inside

Start

Exception in thread "main" java.lang.NullPointerException
    at softwareClustering.DominantSetClustering.clustersRefGlobal(DominantSetClustering.java:54)
    at softwareClustering.DominantSetClustering.buildDominatSetClustering(DominantSetClustering.java:76)
    at trees.PrototypeSelectionTree.clustersRefLocal(PrototypeSelectionTree.java:214)
    at trees.PrototypeSelectionTree.clustersRefGlobal(PrototypeSelectionTree.java:180)
    at trees.PrototypeSelectionTree.buildTree(PrototypeSelectionTree.java:59)
    at trees.PrototypeSelectionTree.buildClassifier(PrototypeSelectionTree.java:235)
    at weka.classifiers.Evaluation.crossValidateModel(Evaluation.java:617)
    at trees.TestClassifier.main(TestClassifier.java:45)
Java Result: 1

The exception is because the "clustersRefLocal()" function is called with empty "assign" parameter. If any one knows whats the problem or what I am missing?

public double[] buildDominatSetClustering(int n) throws Exception {
    int i = 1;
    ArrayList<ArrayList<Integer>> clsAssign = new ArrayList<>();
    ArrayList<Integer> assign = new ArrayList<>();
    ArrayList<ArrayList<Integer>> get;
    for (int j = 0; j < data.numInstances(); j++) {
        assign.add(j);
    }
    Wh:
    while (i < n) {
        System.out.println("hello");
        get = clustersRefGlobal(assign);
        clsAssign.add(get.get(0));
        assign = get.get(1);
        if(assign.isEmpty()){
            System.out.println("inside  "+assign.size());
            break Wh;
        } 
        System.out.println(assign.size());
        i++;
    }
    if(!assign.isEmpty())
    clsAssign.add(assign);
    double[] indexAssToClus = new double[data.numInstances()];
    int count = 0;
    for (ArrayList<Integer> a : clsAssign) {
        for (int k = 0; k < a.size(); k++) {
            indexAssToClus[a.get(k)] = count;
        }
        count++;
    }
return indexAssToClus;
}

This is the function in which the code exist

The simple explanation to what you are seeing is that in fact the break is stopping the loop ... but the code around the snippet you have shown us is starting it again.

This will be apparent if you add a traceprint immediately before the labelled while statement.


The exception is because the "clustersRefLocal()" function is called with empty "assign" parameter.

I suspect that you are confusing "empty" with null . An empty string is a non-null String that has zero length. If you try to test if a null String is empty by calling String.isEmpty() you will get an NPE. The correct test for a non-null, non-empty String is this:

if (assign == null || assign.isEmpty()) {
    // null or empty ... bail out
    break;
}

I recommend you simply reverse the logic:

 if(! assign.isEmpty()){
     i++;
 } 

But failing that, check what is happening to your variable i :

Your check is for: while (i < n)

But the only place i is ever changed, it is incremented.

labeling your loops and using break label is discouraged. (its like the goto and causes spaghetti code.)

Besides, it's not making sense here, since you don't have nested loops. You can just change break Wh; to break;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM