无法调用“ ”,因为数组为空。

huangapple go评论81阅读模式
英文:

Cannot invoke " " because array is null

问题

我需要添加关于两位演员的信息,如他们的姓名、地址和年龄,之前在没有使用数组的情况下已经轻松完成了,但现在需要使用数组。然而,我一直在遇到以下错误:

"无法调用"TestActor.setName(String)",因为"actors[0]"为空
在TestMain.main(TestMain.java:5)"

这只是我用来测试的一个测试主类:

public class TestMain {
    public static void main(String[] args) {
        TestActor[] actor = new TestActor[2];
        // Actor act1 = new Actor(" ", " ", 0);
        actor[0] = ("");
        actor[0].setName("Jack Nicholson");
        actor[0].setAddress("Miami.");
        actor[0].setAge(74);
        actor[0].printAct();
    }
}

这是我正在使用的演员类,我需要从中设置信息:

public class TestActor {
    private String name;
    private String address;
    private int age;

    public TestActor(String s, String g, int p) {
        this.name = s;
        this.address = g;
        this.age = p;
    }

    public void setName(String s) {
        name = s;
    }

    public void setAddress(String g) {
        address = g;
    }

    public void printAct() {
        System.out.println("The actor's name is " + name + " and age is " + age + ". They live in " + address);
    }

    public void setAge(int p) {
        age = p;
    }

    public String toString() {
        return "The actor's name is " + name + " and age is " + age + ". They live in " + address;
    }
}

我知道目前 toString 方法没有实际作用,它的设置方式只是临时的。可能会有些混乱,我可能走错了方向。在没有使用数组的情况下,我能够相对轻松地完成,但使用数组后我有些困惑,不太确定应该采取哪个方向,可能会把整个事情搞砸。

英文:

I need to add information about 2 actors such as their name, address and age and have done so without arrays easily but it's necessary, I keep getting the error

"Cannot invoke "TestActor.setName(String)" because "actors[0]" is null
at TestMain.main(TestMain.java:5)"

This is just a test main I'm using to test it
'''

public class TestMain {
public static void main(String[] args) {
    TestActor[] actor = new TestActor[2];
    //Actor act1 = new Actor(" ", " ", 0);
    actor[0]= ("");
    actor[0].setName("Jack Nicholson");
    actor[0].setAddress("Miami.");
    actor[0].setAge(74);
    actor[0].printAct();
}

And this is the actor class I'm using that I need to set the information from

public class TestActor {
    private String name;
    private String address;
    private int age;

    public TestActor(String s, String g, int p) {
        this.name = s;
        this.address = g;
        this.age = p;
    }

    public void setName(String s) {
        name = s;
    }

    public void setAddress(String g) {
        address = g;
    }

    public void printAct() {
        System.out.println("The actor's name is " + name + " and age is " + age + ". They live in " + address);
    }

    public void setAge(int p) {
        age = p;
    }

    public String toString() {
        return "The actor's name is " + name + " and age is " + age + ". They live in " + address;
    }

}

I know the toString doesn't do anything there the way I have it setup just for the time being. It might be a bit of a mess and I might be in totally the wrong track. I was able to do it relatively easily wihtout using arrays but they've kinda stumped me and I'm not 100% sure on the direction to go in without maybe butchering the whole thing.

答案1

得分: 2

编译器报错是因为你没有正确初始化TestActor对象。你应该这样做:

actor[0] = new TestActor("杰克·尼科尔森", "迈阿密。", 74);
actor[0].printAct();

如果你不想这样做,而是手动使用设置器,那么你需要在TestActor中定义一个默认构造函数:

public TestActor() {  }

然后你可以像这样在数组中使用它:

actor[0] = new TestActor();
actor[0].setName("杰克·尼科尔森");
actor[0].setAddress("迈阿密。");
actor[0].setAge(74);
actor[0].printAct();
英文:

Compiler is complaining because you're not initializing TestActor object correctly. You should rather do this:

actor[0] = new TestActor("Jack Nicholson", "Miami.", 74);
actor[0].printAct();

If you don't want to do this and use setters manually, then you need to define a default constructor in TestActor:

public TestActor() {  }

then you should be able to use it in your arrays like this:

actor[0] = new TestActor();
actor[0].setName("Jack Nicholson");
actor[0].setAddress("Miami.");
actor[0].setAge(74);
actor[0].printAct();

答案2

得分: 0

当你创建 TestActor[] actor = new TestActor[2]; 时,你正在创建一个引用类型对象的数组。所以,actor[0]actor[1] 都指向 null。首先创建 TestActor 并将对象分配给数组。例如:

TestActor actorOne = new TestActor("s", "g", 0);
actor[0] = actorOne;

或者

TestActor[] actor = new TestActor[2];
actor[0] = new TestActor("Jack Nicholson", "Miami", 74);
actor[0].printAct();
英文:

When you create TestActor[] actor = new TestActor[2];, you are creating an array of a reference type object. So, actor[0] as well as actor[1] refer null. First create TestActor and assign the objects to the array. Like:

TestActor actorOne = new TestActor("s", "g", 0);
actor[0] = actorOne;

OR

TestActor[] actor = new TestActor[2];
actor[0]= new TestActor ("Jack Nicholson", Miami.", 74);
actor[0].printAct();

答案3

得分: 0

似乎您的问题在于将 Actor 初始化为一个空字符串,而不是一个 TestActor 对象:

actor[0] = ("");

请尝试改为:

actor[0] = new TestActor("Jack Nicholson", "Miami.", 74);

每个 TestActor 都需要被实例化,因此这将实例化 TestActors 并填充您想要的字段。

英文:

It seems your problem is with initializing the Actor to an empty string instead of a TestActor object:

actor[0] = ("");

Instead try:

actor[0] = new TestActor("Jack Nicholson", "Miami.", 74);

Each TestActor needs to be instantiated so this will instantiate the TestActors and also populate the fields you want.

huangapple
  • 本文由 发表于 2020年10月21日 04:45:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64453079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定