从方法返回对象

时间:2014-07-02 07:52:17

标签: java

我已经定义了一个名为“findRegion()”的方法,其返回类型为“Screen”。尽管在我的方法结束时我返回一个“Screen”对象,我的eclipse IDE仍然显示有关返回类型不匹配的错误。方法如下:

public Screen findRegion(){
        try{
            Screen screen = new Screen();
            int x = screen.find("C:\\Users\\skhan\\Desktop\\Images\\VVX500\\Capture2.PNG").getX() - 10; 
            int y = screen.find("C:\\Users\\skhan\\Desktop\\Images\\VVX500\\Capture2.PNG").getY() - 10; 
            int w = x + 370;
            int h = y + 300;
            screen.setRect(x,y,w,h);
            return screen;
        }
        catch(FindFailed e){
            e.printStackTrace();
        }

    }

此代码的目标是创建具有相应尺寸的区域。 请在我的代码中建议错误。 谢谢

5 个答案:

答案 0 :(得分:0)

由于您的try-catch块而发生该错误。如果发生错误(FindFailed)该怎么办?然后你就什么也不回来了!

try {
  Screen screen = new Screen();
  int x = screen.find("C:\\Users\\skhan\\Desktop\\Images\\VVX500\\Capture2.PNG").getX() - 10; 
  int y = screen.find("C:\\Users\\skhan\\Desktop\\Images\\VVX500\\Capture2.PNG").getY() - 10; 
  int w = x + 370;
  int h = y + 300;
  screen.setRect(x,y,w,h);
  return screen;
} catch (FindFailed e) {
  e.printStackTrace();
  // what to do if this happens?
  // proper error handling must be done here. Either return 'null' or propagate the error to the calling method!
}
// you can even make a return statement here if it is more convenient for you

答案 1 :(得分:0)

您的方法必须有一个默认的return语句。

(参见return语句的评论)

public String findRegion(){
        try{
            Screen screen = new Screen();
            int x = screen.find("C:\\Users\\skhan\\Desktop\\Images\\VVX500\\Capture2.PNG").getX() - 10; 
            int y = screen.find("C:\\Users\\skhan\\Desktop\\Images\\VVX500\\Capture2.PNG").getY() - 10; 
            int w = x + 370;
            int h = y + 300;
            screen.setRect(x,y,w,h);
            return screen;   // may not come here if any brakage before
        }
        catch(FindFailed e){
            e.printStackTrace();
        }
        return null;

    }

最佳做法是只为该方法提供一个return语句。然后你的代码变成了

public String findRegion(){
        Screen screen= null;
        try{
            screen = new Screen();
            int x = screen.find("C:\\Users\\skhan\\Desktop\\Images\\VVX500\\Capture2.PNG").getX() - 10; 
            int y = screen.find("C:\\Users\\skhan\\Desktop\\Images\\VVX500\\Capture2.PNG").getY() - 10; 
            int w = x + 370;
            int h = y + 300;
            screen.setRect(x,y,w,h);
        }
        catch(FindFailed e){
            e.printStackTrace();
        }
        return screen;

    }

答案 2 :(得分:0)

在try块之前声明你的屏幕对象(你可以在catch中初始化它)并在catch块之后返回它。

答案 3 :(得分:0)

如果你正在捕捉异常,那么你也需要从那里返回一些东西。或者你也可以在下面

public Screen findRegion(){
    Screen screen = null;
    try{
        screen = new Screen();
        int x = screen.find("C:\\Users\\skhan\\Desktop\\Images\\VVX500\\Capture2.PNG").getX() - 10; 
        int y = screen.find("C:\\Users\\skhan\\Desktop\\Images\\VVX500\\Capture2.PNG").getY() - 10; 
        int w = x + 370;
        int h = y + 300;
        screen.setRect(x,y,w,h);

    }
    catch(FindFailed e){
        e.printStackTrace();
    }
    return screen;

}

答案 4 :(得分:0)

如果发生FindFailed异常,你没有返回任何内容,这就是eclipse显示return type mismatch的原因。

您应该执行以下类型的代码:

public Screen findRegion(){
    Screen screen = null; //initialize your variable here with null
    try{
        screen = new Screen();
        int x = screen.find("C:\\Users\\skhan\\Desktop\\Images\\VVX500\\Capture2.PNG").getX() - 10; 
        int y = screen.find("C:\\Users\\skhan\\Desktop\\Images\\VVX500\\Capture2.PNG").getY() - 10; 
        int w = x + 370;
        int h = y + 300;
        screen.setRect(x,y,w,h);
    }
    catch(FindFailed e){
        e.printStackTrace();
    }
    return screen; //return screen object if all goes well or null if any exception occurs.
}
  

注意:看起来FindFailed是您的自定义Exception,您应该编写自定义异常类名称以Exception结尾(它可以减少混淆并看起来像一个异常)