java - 我的数组成员为空,即使它们已初始化
问题描述
所以我试图访问我的关系数组中的成员,一旦它被初始化。但是它不允许我这样做。我究竟做错了什么?
public class FamilyRelations {
private Relationship [] relation;
public static void main(String[] args) {
FamilyRelations family = new FamilyRelations();
Scanner scanner = new Scanner(test);
family.run(scanner);
}
private void run(Scanner scanner) {
noOfRelations = 3;
relation= new Relationship [3];
System.out.println(relation[0].child); // Why does this raise nullpointerexception?
// Here I want to do relation[0].father = "John"; for example
}
public class Relationship {
public String child;
public String father;
public String mother;
public Relationship() {
this.child = "hej";
this.father = "father";
// this.mother = mother;
}
解决方案
推荐阅读
- python - 在python中将行转换为具有特定顺序的列
- javascript - 部分字符串匹配javascript后开始比较
- django - 通过电子邮件激活的 Django/ 用户注册 - 基于类的视图
- c# - C# foreach 修改列表。收藏已修改;可能无法执行枚举操作
- kubernetes - 使用虚拟服务在 2 个 ClusterIP k8s 服务之间拆分流量
- rsync - rsync 不会从包含文件中读取
- c++ - 我对为什么我的程序不会显示从字符串代码中删除元音的输出感到困惑
- shell - 定义默认 PATH 环境变量的位置
- pandas - 使用 Pandas 中另一个数据集的匹配行值重命名列名?
- grid - Acumatica - 在后面的代码中从网格中获取多条选定的行