简体   繁体   English

用Java填充对象数组

[英]Filling an object array in Java

So, I've recently switched to Java from C++ (due to my education) and doing some practice. 因此,最近(由于受过教育),我从C ++转到Java,并做了一些练习。 I believe this question is not really smart, but I really need to know what am I doing wrong. 我相信这个问题并不是很聪明,但是我真的需要知道我在做什么错。

Simply, I have 3 classes: 简单来说,我有3个班级:

  • mainClass mainClass
  • Composer 作曲家
  • Concert 音乐会

My problem is in the main(): 我的问题在main()中:

public static void main(String[] args) {
    Composer Schubert = new Composer("Franz Schubert", "Classical Music", 6);
    Schubert.getConcert()[1].enterWholeConcertData(); //program just crashes here
}

Nothing goes after that, it just throws this exception: 此后什么也没有,它只会引发此异常:

Exception in thread "main" java.lang.NullPointerException at incredible_package.mainClass.main(mainClass.java:16) 线程“ main”中的异常java.lang.NullPointerException at amazing_package.mainClass.main(mainClass.java:16)

NOTE : Everything works just fine, if I call the enterWholeConcertData() directly from Concert class object and it's not an array of them (like this): 注意 :如果我直接从Concert类对象调用enterWholeConcertData(),并且它不是它们的数组(像这样),则一切工作都很好:

Concert concert = new Concert();
concert.enterWholeConcertData();

Composer has a field, which is an array of Concert class and an int variable, that defines the number of elements in the Concert class (plus default constructor, another setters/getters, "show fields" method, "enter the whole info" method): Composer具有一个字段,该字段是Concert类的数组和一个int变量,用于定义Concert类中的元素数量(加上默认构造函数,另一个setter / getter,“显示字段”方法,“输入整个信息”方法):

public class Composer{
        Composer(String name, String genre, int aNOC)
    {
        this.name = name;
        this.genre = genre;
        this.numberOfConcerts = aNOC;

        this.concert = new Concert[numberOfConcerts];
    }

    private int amountOfSpectators;
    private Concert[] concert;
}

Getter for concert in Composer class: 参加作曲家音乐会的吸气剂

public Concert[] getConcert()
    {
        Concert[] copy = new Concert[this.concert.length];
        System.arraycopy(this.concert, 0, copy, 0, copy.length);
        return copy;
    }        

Concert consists of (plus setters/getters, "show info" method, parameter constructor): Concert由(加上设置器/获取器,“显示信息”方法,参数构造器)组成:

public class Concert{
    Concert()
    {
        date = "--.--.----";
        amountOfSpectators = 0;
    }

    private String date;
    private int amountOfSpectators;

    public void enterWholeConcertData()
        {
            System.out.println("Enter the date:");

            Scanner sDate = new Scanner(System.in);
            date = sDate.nextLine();
            sDate.close();

            System.out.println("Enter the number of spectators: "
                    + "");

            Scanner sAOW = new Scanner(System.in);
            amountOfSpectators = sAOW.nextInt();
            sAOW.close();
        }
}

Sorry, if I didn't mention anything important. 抱歉,如果我没有提到任何重要事项。 You can ask me about that, I'll gladly add some new information. 您可以向我询问,我很乐意添加一些新信息。

From what I see: 从我看到的:

public static void main(String[] args) {
    Composer Schubert = new Composer("Franz Schubert", "Classical Music", 6);
    Schubert.get_concert()[1].enterWholeConcertData(); //program just crashes here
}

You create a new Compser, setting its name, genre and NoC. 您创建一个新的Compser,设置其名称,流派和NoC。 You also initialize the array of 6 elements. 您还初始化了6个元素的数组。

However, the elements in the array or Null, since you haven't initialize them. 但是,由于尚未初始化数组或Null中的元素。 You should have at least done something like: 您至少应该执行以下操作:

Shuber.get_concert()[1] = new Concert();

And same for any other items, before calling a method on. 在调用方法之前,对于其他任何项目也是如此。

You don't initialize any individual item of the Composer.concert array. 您无需初始化Composer.concert数组的任何单个项目。 In this case every item of Composer.concert array will be initialized to null and that's why you're getting NullPointerException . 在这种情况下, Composer.concert数组的每个项目都将初始化为null,这就是为什么要获取NullPointerException的原因。

Quoting JLS section 4.12.5 : 引用JLS第4.12.5节:

Each array component is initialized with a default value when it is created 每个数组组件在创建时都会用默认值初始化

For all reference types the default value is null 对于所有引用类型,默认值为null

You should initialize items of the Composer.concert array by yourself. 您应该自己初始化Composer.concert数组的项目。

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

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