在Java中将字符数组转换为String时出现空指针异常

时间:2017-10-14 18:24:47

标签: java arrays string

在我的代码段中,我尝试首先生成一个Character数组,然后将其汇总到一个String,但我一直在点NullPointerException。是否有内存未指向并且在此处被访问是否存在某种类型不匹配?

源代码:

private int getRandomDigit() {
    return (random.nextInt(10));
}

private Character getRandomCharacter() {
    return (char) (65 + random.nextInt(26));
}

public void setRandomizedCustomerID() {

    Character[] customerID_ = new Character[10];

    customerID_[0] = Character.forDigit(getRandomDigit(), 10);
    customerID_[1] = Character.forDigit(getRandomDigit(), 10);
    customerID_[2] = getRandomCharacter();
    customerID_[3] = getRandomCharacter();
    customerID_[4] = getRandomCharacter();
    customerID_[5] = getRandomCharacter();
    customerID_[6] = Character.forDigit(getRandomDigit(), 10);
    customerID_[7] = getRandomCharacter();
    customerID_[8] = getRandomCharacter();
    customerID_[9] = Character.forDigit(getRandomDigit(), 10);

    for (Character c : customerID_) 
        customerID += c.toString(); /* customerID is a field of type String */
}

主要方法:

public static void main(String[] args) {
    GenerateRecords[] recordArray = new GenerateRecords[1000];
    String[] recordsToBeWritten = new String[1000];

    for (int i = 0; i < 1000; i++) {
        recordArray[i].setRandomizedCustomerID(); /* NullPointerException thrown here */
        recordArray[i].setRandomizedOccupationID();
        recordArray[i].setRandomizedIncome();
        recordArray[i].setRandomizedDebt();
        recordsToBeWritten[i] = recordArray[i].getRandomizedCustomerID() + "," + recordArray[i].getRandomizedState()
                + "," + recordArray[i].getRandomizedGender() + "," + recordArray[i].getRandomizedAge() + ","
                + recordArray[i].getRandomizedRace() + "," + recordArray[i].getRandomizedMaritalStatus() + ","
                + recordArray[i].getRandomizedOccupation() + "," + recordArray[i].getRandomizedIncome() + ","
                + recordArray[i].getRandomizedDebt() + "," + recordArray[i].getRandomizedLoanType() + "," + "?";
    }

    for (int i = 0; i < 1000; i++)
        System.out.println(recordsToBeWritten[i] + "\n");
}

0 个答案:

没有答案