Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么在初始化字符串后会得到null?_Java_String_Null_Println - Fatal编程技术网

Java 为什么在初始化字符串后会得到null?

Java 为什么在初始化字符串后会得到null?,java,string,null,println,Java,String,Null,Println,我不明白为什么Sp1.location返回NULL。如果我运行该程序,我可以成功初始化位置。我以类似的方式将属性编码为整数,但这并没有给我带来任何问题 public class Database { static Scanner userInput = new Scanner(System.in); public static void main(String[] args) { System.out.println("Add a new spawnpoin

我不明白为什么Sp1.location返回NULL。如果我运行该程序,我可以成功初始化位置。我以类似的方式将属性编码为整数,但这并没有给我带来任何问题

public class Database {

    static Scanner userInput = new Scanner(System.in);

    public static void main(String[] args) {

        System.out.println("Add a new spawnpoint.\n");

        System.out.println("State the name of this spawnpoint: ");


        Spawnpoints Sp1 = new Spawnpoints(getSpawnName());

        System.out.println("Done");
        System.out.println("Location: " + Sp1.getLocation()); //return as null

    }

    public static String spawnName;

    public static String getSpawnName() {

        spawnName = userInput.next();
        return spawnName;
    }

    public void setSpawnName(String spawnName) {
        this.spawnName = spawnName;
    }
}


// Import libraries
import java.util.*;
这是我的另一节课

public class Spawnpoints extends Database {


        // Define scanner, so you can accept user input
        static Scanner userInput = new Scanner(System.in);

             // Define attributes of Spawnpoints


            private String location;
            private String iniLocation;


    // Creator, method for creating a instance of Spawnpoints. Will be the actual spawnpoints
    // I include a iniLocation so no user input is asked when calling on getLocation. 

    public Spawnpoints(String spawnName) {
        getIniLocation();

    }

    // Setters & Getters getLocation
    private String getIniLocation() {
        System.out.println("State the location of this spawnpoint:\n");
        pokemon = userInput.next ();
        return iniLocation;
    }

    public void setIniLocation(String iniLocation) {
        this.iniLocation = iniLocation;
    }


    public String getLocation() {
        location = iniLocation;
        return location;
    }


    public void setLocation(String location) {
        this.location = location;
    }



    public static void main (String[] args) {


    }

}

由于您没有设置
位置
,因此您将输入分配给
口袋妖怪
,而不是
iniLocation
,当您调用函数获取位置时,您将返回未分配任何值的
iniLocation
,因此为空。阅读代码中的注释

private String getIniLocation() {
    System.out.println("State the location of this spawnpoint:\n");
    pokemon = userInput.next (); // remove this
    iniLocation = userInput.next (); // with this
    return iniLocation;
}
如果在构造函数中初始化
扫描器
对象,这是一个很好的做法

class AnyClass{
 Scanner scan;
  public AnyClass(){
    scan= new Scanner(System.in);
  }
}