简体   繁体   English

Java字符串返回为null

[英]Java string returns as null

I am attempting to get one class to return a string from another class, though the return I get is null. 我正在尝试让一个类从另一类返回字符串,尽管我得到的返回值为null。 I have a set method that works in setting the string in the original class, but when calling the method in my second class, I get a return of null. 我有一个set方法可以在原始类中设置字符串,但是在第二个类中调用该方法时,返回的是null。

Here is the first class; 这是头等舱;

public class IceCream
{
    // instance variables - replace the example below with your own
    private String flavour;
    public static double price;


    /**
     * Constructor for objects of class IceCream
     */
    public IceCream()
    {
        // initialise instance variables
        String flavour = getFlavour();
        price = 0.50;

    }

    /**
     * Gets price in pence.
     * 
     * 
     * @returns the price of the ice cream.
     */
    public static double getPrice()
    {
        // put your code here
        return price;
    }

    public int getScoops()
    {
        return scoop;
    }

public void setPrice(int newPrice)
{
    price = newPrice;
}

public void setScoops(int scoopNumber)
{
    scoop = scoopNumber;
}

public double totalCost()
{
    double cost;
    cost = scoop * price;
    return cost;

}

public String getFlavour()
{
  return flavour; 
}

public void setFlavour(String whatFlavour)
{
    flavour = whatFlavour;
}

} }

And the second class, in which I am trying to call the string I input in the setFlavour method in the println of the sundaeDetails method. 第二类,我试图在其中调用在sundaeDetails方法的println中的setFlavour方法中输入的字符串。

import java.util.ArrayList;
/**
 * Write a description of class Sundae here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Sundae
{
    // instance variables - replace the example below with your own
    private IceCream flavour;
    private Topping SundaeTopping;
    private int scoops;


    /**
     * Constructor for objects of class Sundae
     */
    public Sundae()
   {
     flavour = new IceCream();
     SundaeTopping = new Topping();
     scoops = 0;
   }
   /**
    * Set scoop number.
    */

    public void setScoops(int scoopNumber)
   {
    scoops = scoopNumber;
   }
   /**
    * Return scoop variable.
    */
   public int getScoops()
   {
       return scoops;
   }
   /**
    * Get the price of the sundae.
    */ 
   public void getPrice()
   {
        double cost;
        double scoopPrice = scoops * IceCream.getPrice();
        if ( scoops > 0) {
            cost = scoopPrice * Topping.getToppingPrice();
            System.out.println("Cost of Sundae: " + cost);
         }
        else {
        System.out.println("Need to have a scoop of ice cream in your Sundae.");
    }
    }

    /**
     * Return the details of the sundae; price, flavour, scoops etc.
     */
   public void sundaeDetails()
   {
       System.out.println("You have " + scoops + " scoops of " + flavour.getFlavour() + "ice cream");
   }
}

In IceCream class constructor you have: 在IceCream类构造函数中,您可以:

String flavour = getFlavour()

You have created a local variable instead a reference to a instance property. 您已经创建了一个局部变量,而不是对实例属性的引用。 getFlavour() method return the property instance that you never set, so its null. getFlavour()方法返回您从未设置的属性实例,因此它为null。 You should have something like this in the constructor: 您应该在构造函数中具有以下内容:

this.flavour = "default value";

Or set a flavour parameter on constructor header: 或在构造函数标头上设置一个flavor参数:

public IceCream(String flavour) {
    this.flavour = flavour;
    (...)
}

And call it like: 并这样称呼:

IceCream chocolat = new IceCream(" chocolat");

And if you want change the flavour use the setter. 如果您想改变口味,请使用二传手。

您永远不会调用flavour.setFlavour() ,因此flavour.getFlavour()返回null flavour.getFlavour()

Flavour is not initialized private String flavour; 风味不是初始化的private String flavour; as anything, when calling IceCream() you set String flavour = getFlavour() , which sets the local variable flavour inside the constructor to null, because getFlavour() returns null . String flavour = getFlavour() ,在调用IceCream()您都设置了String flavour = getFlavour() ,这String flavour = getFlavour()构造函数内部的局部变量flavour设置为null,因为getFlavour()返回null Also the local String flavour variable inside the constructor overshadows the String flavour field of your IceCream class. 同样,构造函数中的本地String flavour变量也会使IceCream类的String flavour字段蒙上阴影。 Try making flavour a parameter of the constructor and then setting the field to that string 尝试将flavor作为构造函数的参数,然后将字段设置为该字符串

public IceCream(String f) {
    this.flavour = f;
    price = 0.50;
}

or call setFlavour() before you work with the object you created. 或在创建对象之前调用setFlavour()

The problem starts in the constructor of your IceCream class. 问题始于IceCream类的构造函数。

private String flavour;

public IceCream()
{
    // initialise instance variables
    String flavour = getFlavour();
    price = 0.50;
}

public String getFlavour()
{
  return flavour; 
}

The first problem is that you are shadowing your member variable flavour - you are declaring another variable with the same name but whose scope is restricted to the constructor. 第一个问题是,您正在隐藏成员变量的flavour -您在声明另一个具有相同名称但范围仅限于构造函数的变量。 The second problem is you are assigning the value returned by getFlavour() , which is just an accessor that returns the member variable itself (whose initial value is null). 第二个问题是您要分配getFlavour()返回的值,该值只是一个返回成员变量本身(其初始值为null)的访问器。

To fix it you need to assign an initial value in the constructor 要修复它,您需要在构造函数中分配一个初始值

public IceCream()
{
    // initialise instance variables
    this.flavour = "Vanilla"
    price = 0.50;
}

Its also possible to assign the initial value when you declare it 声明初始值时也可以赋值

private String flavour = "Vanilla";

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

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