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

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

Cannot invoke " " because array is null

问题

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

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

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

  1. public class TestMain {
  2. public static void main(String[] args) {
  3. TestActor[] actor = new TestActor[2];
  4. // Actor act1 = new Actor(" ", " ", 0);
  5. actor[0] = ("");
  6. actor[0].setName("Jack Nicholson");
  7. actor[0].setAddress("Miami.");
  8. actor[0].setAge(74);
  9. actor[0].printAct();
  10. }
  11. }

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

  1. public class TestActor {
  2. private String name;
  3. private String address;
  4. private int age;
  5. public TestActor(String s, String g, int p) {
  6. this.name = s;
  7. this.address = g;
  8. this.age = p;
  9. }
  10. public void setName(String s) {
  11. name = s;
  12. }
  13. public void setAddress(String g) {
  14. address = g;
  15. }
  16. public void printAct() {
  17. System.out.println("The actor's name is " + name + " and age is " + age + ". They live in " + address);
  18. }
  19. public void setAge(int p) {
  20. age = p;
  21. }
  22. public String toString() {
  23. return "The actor's name is " + name + " and age is " + age + ". They live in " + address;
  24. }
  25. }

我知道目前 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
'''

  1. public class TestMain {
  2. public static void main(String[] args) {
  3. TestActor[] actor = new TestActor[2];
  4. //Actor act1 = new Actor(" ", " ", 0);
  5. actor[0]= ("");
  6. actor[0].setName("Jack Nicholson");
  7. actor[0].setAddress("Miami.");
  8. actor[0].setAge(74);
  9. actor[0].printAct();
  10. }

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

  1. public class TestActor {
  2. private String name;
  3. private String address;
  4. private int age;
  5. public TestActor(String s, String g, int p) {
  6. this.name = s;
  7. this.address = g;
  8. this.age = p;
  9. }
  10. public void setName(String s) {
  11. name = s;
  12. }
  13. public void setAddress(String g) {
  14. address = g;
  15. }
  16. public void printAct() {
  17. System.out.println("The actor's name is " + name + " and age is " + age + ". They live in " + address);
  18. }
  19. public void setAge(int p) {
  20. age = p;
  21. }
  22. public String toString() {
  23. return "The actor's name is " + name + " and age is " + age + ". They live in " + address;
  24. }
  25. }

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对象。你应该这样做:

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

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

  1. public TestActor() { }

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

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

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

  1. actor[0] = new TestActor("Jack Nicholson", "Miami.", 74);
  2. 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:

  1. public TestActor() { }

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

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

答案2

得分: 0

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

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

或者

  1. TestActor[] actor = new TestActor[2];
  2. actor[0] = new TestActor("Jack Nicholson", "Miami", 74);
  3. 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:

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

OR

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

答案3

得分: 0

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

  1. actor[0] = ("");

请尝试改为:

  1. 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:

确定