0

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;
}   
}
3
  • Why are you using "Printers[i].setPersonality(aLineOfText[0].toString());" instead of just "Printers[i].setPersonality(aLineOfText[0]);" because as i see your code it is string type,so there is no point to call toString() on it? Commented Nov 9, 2014 at 9:09
  • Hi Mad, thanks, I overlooked that one. So fond with using toString(). But I already modified the code and still having the same error. Could it be that its not possible in java? Commented Nov 9, 2014 at 10:28
  • The trick is to read the error stack trace, to know where the error comes from. Without this stack trace, we're forced to guess, instead of knowing. Commented Nov 9, 2014 at 11:53

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Marcs! It was a great help!
I'm glad, choose this answer and close your question then.
I was having the same issue with a 2D array, I instantiated a new 2d array of user defined type, but didn't initialize with new. Thanks to you bro fixed it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.