简体   繁体   English

为什么此字符串值为null?

[英]Why is this string value null?

So I'm finishing up my code, and encountered an error and don't know why! 所以我正在整理我的代码,遇到错误,不知道为什么!

The String "Size" is null when it gets to the "baseArea()" Method. 到达“ baseArea()”方法时,字符串“ Size”为空。

No errors are displayed. 没有错误显示。

EDIT: Added the Main method of the class, where everything is referenced, please note this is not the full code in the class ! 编辑:添加了类的Main方法,其中引用了所有内容,请注意,这不是该类中的完整代码!

here is the relevant code: 这是相关代码:

public class OrderingSystem 
{
private Canvas canvas;
private double Price;
private String Topping1 = setTopping1();
private String Topping2 = setTopping2();
private String Sauce = setSauce();
private String Size;
private String Crust;
private double BaseArea;

/**
 * Constructor for the ordering system.
 */
public OrderingSystem()
{
    canvas = new Canvas("Pizza Ordering", 900, 650);         
}

/**
 * Method to draw the outline of the order screen.
 */
public void drawOrderScreen()
{
    canvas.setForegroundColor(Color.BLACK);
    // vertical dividers
    canvas.drawLine(300, 0, 300, 600);
    canvas.drawLine(600, 0, 600, 600);

    // halfway divider
    canvas.drawLine(0, 300, 900, 300);
    setSauce();
    startToppings();
    startOrdering();
    setSize();
    baseArea(BaseArea, Size);
    Crust();
}

public String setSize(){
    System.out.print("What size would you like: Large, Medium or Small? :   ");
    Scanner sizescanner = new Scanner(System.in);
    String Size = sizescanner.nextLine();

    if (Size.equals("Large")){
        System.out.print( "Large selected !  ");
    }
    else if (Sauce.equals("Medium")){
        System.out.print( "Medium selected !"  );
    }

    else if (Sauce.equals("Small")){
        System.out.print( "Small selected !"  );
    }

    else {
        sizescanner.reset();
        System.out.print("Invalid Size! ");
        setSize();
    }
    return Size;
}

public double baseArea(double baseArea,String Size){

    if (Size.equals("Large")){
        baseArea = 176.7150;
    }
    else if (Size.equals("Medium")){
        baseArea = 113.0976;
    }

    else if (Size.equals("Small")){
        baseArea = 78.54;
    }
    return baseArea;
}

Your setSize method gets the size from the Scanner but never actually sets the value of the Size field, only returns it. 您的setSize方法从扫描仪获取大小,但从不实际设置Size字段的值,仅返回它。

Change the return type of your setSize method to void , and replace the return Size; setSize方法的返回类型更改为void ,并替换return Size; call in that method with this.Size = Size; this.Size = Size;调用该方法this.Size = Size; .

Another note, your recursion is also slightly broken. 另外请注意,您的递归也略有中断。 After you call setSize in the else block, you'll need to return from the method to prevent the field being updated with the invalid Size. 在else块中调用setSize之后,您需要从该方法返回以防止使用无效的Size更新该字段。

In your setSize() method, unless you enter 'Large' everything else will become null as you check if the Sauce value is equal to what was entered: setSize()方法中,除非您输入“ Large”,否则其他所有内容都将为null,因为您检查Sauce值是否等于输入的值:

if(Size.equals("large"){
  doSomething();
} else if (Size.equals("medium"){
  doSomething();
} else if(Size.equals("small"){
  doSimething();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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