Search code examples
javaarraystypesdefined

Java Array of user defined type


I'm trying to populate an array of user-defined type - composite of 3 Strings, and 1 Boolean. The type name is PrinterType. with methods to SET and GET for these 3 Strings and Boolean values.

I'm trying to use it in another class file where array population will be executed but when I run the project, I'm getting java.lang.NullPointerException.

I understand that this is a problem of string Field not initialized but I don't know how to initialize the String fields of a user-defined-type array. I hope somebody can help me on this. The user defined type and implementing classes are below.

by the way, error points to line of implementing class: Printers[i].setPersonality(aLineOfText[0].toString());

//implementing class =======================================
package MainPkg;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadDefinitions
{
final String sFileName = "C:/Definitions OEvP/Printers.txt";
private static int iNoOfLines;
public PrinterType[] Printers;


private int getNumberOfLines() throws IOException
{   
FileReader fr = new FileReader(sFileName);
BufferedReader br = new BufferedReader(fr);

iNoOfLines = 0;

while (br.readLine() != null)
{
    iNoOfLines++;
}

br.close();
fr.close();

return iNoOfLines;
}

public void setArray() throws IOException
{
Printers = new PrinterType[this.getNumberOfLines()];
int i = 0;

FileReader fr = new FileReader(sFileName);
BufferedReader br = new BufferedReader(fr);
String[] aLineOfText = {};

while (i != this.getNumberOfLines())
{   
    aLineOfText = br.readLine().split(";");
    Printers[i].setPersonality(aLineOfText[0].toString());
    Printers[i].setProductNumber(aLineOfText[1].toString());
    Printers[i].setPL(aLineOfText[2].toString());

    if ("1" == aLineOfText[3].toString())
    {
        Printers[i].setHipot(Boolean.TRUE);
    }
    else
        {
            Printers[i].setHipot(Boolean.FALSE);
        }

    i++;
    aLineOfText = null;
    System.gc();
}
}   

}


//user defined Class ======================================

package MainPkg;

public class PrinterType
{
private String sPersonality;
private String sProductNumber;
private String sPL;
private Boolean bHipot;

//constructor
PrinterType()
{
sPersonality = "test";
sProductNumber = "test";
sPL = "test";
}

//printer information
//set methods
public void setPersonality(String localString)
{
    sPersonality = localString; 
}

public void setProductNumber(String localString)
{
    sProductNumber = localString;
}

public void setPL(String localString)
{
    sPL = localString;
}

public void setHipot(Boolean localBoolean)
{
    bHipot = localBoolean;
}

//get methods
public String getPersonality()
{
    return sPersonality;
}

public String getProductNumber()
{
    return sProductNumber;
}

public String getPL()
{
    return sPL;
}

public Boolean getHipot()
{
    return bHipot;
}   
}

Solution

  • You are not initializing the items of your Printer array.

    You are just making an array of null items of PrinterType.

    Printers = new PrinterType[this.getNumberOfLines()];
    
    // Initialization
    for(int i = 0, l = this.getNumberOfLines(); i < l; i++)
        Printers[i] = new PrinterType();