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

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

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.

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..

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.

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!

This question has already been answered. Start a new discussion instead.