954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Missing return statement error, why?

Hello, I'm a biologist trying to do some bioinformatics, so new to Java and new to this forum. I've searched all over the forum for previous threads on this topic, but can't find an answer that has helped me. Apologies if I've missed something already posted that may help solve my problem. I'm currently trying to write a method that will return a boolean true or false. I am getting an error though saying 'missing return statement'. I can't for the life of me work out what the issue is. I've tried changing the braces, rearranging the remove statements in and around the braces, but no luck.

public boolean FailurePoint(int selectedNodes[], CyNetwork network)  {
//error is here 'missing return statement'

            int kids[] = network.getAdjacentEdgeIndicesArray(selectedNodes[0], false, true, true);
            int[] ChildNode = new int[kids.length];



            for (int c = 0; c < ChildNode.length; c++) {

                int node = network.getEdgeSourceIndex(kids[c]);
                if (node == selectedNodes[0]) {
                    node = network.getEdgeTargetIndex(kids[c]);
                }

                ChildNode[c] = node;
            }
            Vector<Integer> cnodes = new Vector<Integer>();

            for (int c = 0; c < ChildNode.length; c++) {

                int node = ChildNode[c];

                if (!cnodes.contains(node)) {
                    cnodes.add(node); 



            for (int i = 1; i < cnodes.size(); i++) {

                boolean isConnected = hasPath(cnodes.elementAt(0), cnodes.elementAt(i), selectedNodes[0], network);


                boolean OneKid = false;
                if (cnodes.size() < 2) {
                    OneKid = true;
                }
                 else {
                    OneKid = false;
                }

                boolean FailurePoint;
                if ((isConnected == true) || (OneKid == true)) {
                    FailurePoint = false;
                    return false;
  
                } 
				else {
                    FailurePoint = true;
                    return true;                  
                    
                }        
			return FailurePoint;   
        }
         
    }
}


I have tried writing the last piece as follows but get the exact same error:

if ((isConnected == true) || (OneKid == true)) {
                    
                    return false;
                    
                } else {
                    
                    return true;               
                    
                }        
    
        }
       
    }
}


I know all the braces don't tally up here, but there are other methods and class above, this. I didn't think it necessary to clog up space with all that code too.
I'll be grateful for any help, thanks in advance :icon_smile:

BioJavaPhobic
Newbie Poster
11 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Looks to me like the problem is the open curly at line 24:

if (!cnodes.contains(node)) {
                    cnodes.add(node);


or rather the lack of a close brace.
This means that if the condition is true, you skip all the way down to the end, past your return statements.

jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
 

Oops, Sorry, my mistake. I cut out some commented out notes near there before posting and seem to have deleted the curlies too...they are there in the original as follows:

public boolean FailurePoint(int selectedNodes[], CyNetwork network)  {


            int kids[] = network.getAdjacentEdgeIndicesArray(selectedNodes[0], false, true, true);
            int[] ChildNode = new int[kids.length];



            for (int c = 0; c < ChildNode.length; c++) {

                int node = network.getEdgeSourceIndex(kids[c]);
                if (node == selectedNodes[0]) {
                    node = network.getEdgeTargetIndex(kids[c]);
                }

                ChildNode[c] = node;
            }
            Vector<Integer> cnodes = new Vector<Integer>();

            for (int c = 0; c < ChildNode.length; c++) {

                int node = ChildNode[c];

                if (!cnodes.contains(node)) {
                    cnodes.add(node); 

				}
			}

            for (int i = 1; i < cnodes.size(); i++) {

                boolean isConnected = hasPath(cnodes.elementAt(0), cnodes.elementAt(i), selectedNodes[0], network);


                boolean OneKid = false;
                if (cnodes.size() < 2) {
                    OneKid = true;
                }
                boolean FailurePoint;
                if ((isConnected == true) || (OneKid == true)) {
                    FailurePoint = false;
                    return false;
  
                } 
				else {
                    FailurePoint = true;
                    return true;                  
                    
                }        
			return FailurePoint;   
        }
         
    }
}
BioJavaPhobic
Newbie Poster
11 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 
public boolean FailurePoint(int selectedNodes[], CyNetwork network) {

        int kids[] = network.getAdjacentEdgeIndicesArray(selectedNodes[0], false, true, true);
        int[] ChildNode = new int[kids.length];
        for (int c = 0; c < ChildNode.length; c++) {
            int node = network.getEdgeSourceIndex(kids[c]);
            if (node == selectedNodes[0]) {
                node = network.getEdgeTargetIndex(kids[c]);
            }
            ChildNode[c] = node;
        }

        Vector<Integer> cnodes = new Vector<Integer>();
        for (int c = 0; c < ChildNode.length; c++) {
            int node = ChildNode[c];
            if (!cnodes.contains(node)) {
                cnodes.add(node);
            }
        }
        
        boolean FailurePoint;
        for (int i = 1; i < cnodes.size(); i++) {
            boolean isConnected = hasPath(cnodes.elementAt(0), cnodes.elementAt(i), selectedNodes[0], network);
            boolean OneKid = false;
            if (cnodes.size() < 2) {
                OneKid = true;
            }
            //boolean FailurePoint;
            if ((isConnected == true) || (OneKid == true)) {
                FailurePoint = false;
                //return false;
            } else {
                FailurePoint = true;
                //return true;
            }
        }
        return FailurePoint;
    }
mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

Thanks for replying. If I do it that way, I lose the missing return statement error, but instead get an error saying variable FailurePoint has not been initialized

BioJavaPhobic
Newbie Poster
11 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Initialize the variable to a default value, whichever would make more sense as a default for your usage.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Wow, thank you so much, sorted now! No more horrible red error marks in my code
Cheers guys :icon_smile:

BioJavaPhobic
Newbie Poster
11 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: