0

In my code snippet, I am trying to first generate a Character array and then aggregate it into a String but I keep hitting a NullPointerException. Is there memory not pointed to and being accessed here or is there some kind of type mismatch?

Source code :

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 */
}

main method :

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");
}
Abhijeet Mohanty
  • 334
  • 1
  • 6
  • 21
  • 2
    `GenerateRecords[] recordArray = new GenerateRecords[1000];` only creates array *for* GenerateRecords objects, but it doesn't fill it with such objects (it doesn't decide for you which `GenerateRecords` constructor to use or what parameters should be passed). Instead that array is filled with nulls. So before you will be able to use `recordArray[i].setRandomizedCustomerID();` you need to assign `recordArray[i] = new GenerateRecords(yourArguments);`. Duplicate also explains it in this answer: https://stackoverflow.com/a/23852556/1393766 – Pshemo Oct 14 '17 at 18:28
  • Yes I know it throws the same exception as NullPointerException as the page I have supposedly duplicated but NullPointerException could arise in different scenarios. @Pshemo – Abhijeet Mohanty Oct 14 '17 at 18:28
  • https://stackoverflow.com/questions/3426843/what-is-the-default-initialization-of-an-array-in-java – sagarr Oct 14 '17 at 18:30
  • "but NullPointerException could arise in different scenarios" yes, but your question is about specific scenario which is described by your code. I tried explain your specific problem in my first comment. If you are interested in other potential scenarios where NPE can be thrown then it is already answered in duplicate question. – Pshemo Oct 14 '17 at 18:46
  • Thanks I'll keep in mind not to duplicate questions. – Abhijeet Mohanty Oct 14 '17 at 18:47

0 Answers0