954,523 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! Arraylist<String>

here is my entire code. now, the only problem I have stumbled upon is in the last method, getRow, where i have to extract a row from an array of string. could anyone please help me with this, i've been stuck for hours, not knowing why it calls "missing return statement", when i clearly do it -.-' thank you in advance

import java.util.ArrayList;

public class Photograph
{
    
    private String[] photograph;
    
    public Photograph(String[] photograph)
    {
        if (photograph == null)
            photograph = new String[] {""};
        else this.photograph = photograph;
    }

    public int getWidth()
    {
        return (photograph[0]).length();
    }

    public int getHeight()
    {
        return photograph.length;
    }
   

    /**
     * @null if the index is out of bounds.
     */
    public String getLine(int index)
    {
        if (index < 0 || index >= getHeight())
            return null;
        return photograph[index];
    }
    
    public String getRow(int index)
    {
        if (index < 0 || index >= getWidth())
            return null; 
        for (int row = 0; row < getHeight(); row++) 
             return photograph[row].substring(index, index + 1);  
    }
    
}
Mehnad
Newbie Poster
5 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Both of your returns are in conditional statements. The compiler requires a guaranteed return value for all possible conditions.

Add a default return value at the end to cover the case of your other conditions not being satisfied.

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

thank you very much for the quick response! but i dont think i completely understand what you mean.. to me the getLine and getRow methods function in principally the same way, and that's why i don't see why it wouldn't work..

Mehnad
Newbie Poster
5 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

getLine has a guaranteed return even if the if() statement is false.

getRow() has two returns but one is in if() and the other in for(). You need a return in case those do not execute.

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

getLine has a guaranteed return even if the if() statement is false.

getRow() has two returns but one is in if() and the other in for(). You need a return in case those do not execute.

Ahh, thank you very much, now i get it!

Mehnad
Newbie Poster
5 posts since Nov 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: