0

This is my write function

void writeText(Text[] t) {
    try {
        writer = new PrintWriter(new FileWriter(FILE_PATHPhrase));
        // Loop through each Person in the Person array
        for (int i = 0; i < t.length; i++) {
            // Write the name, then a # sign, then the surname
            writer.println(t[i].getTitle() + "#" + t[i].getText());

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    writer.close();
}

The nullpointer points here

 writer.println(t[i].getTitle() + "#" + t[i].getText());

I have no clue on why it does this..

SatyaTNV
  • 4,053
  • 3
  • 13
  • 30
Alexx
  • 1
  • might be `t[i]` is null. add null check condition before accesing it. – bNd Jan 28 '16 at 07:07
  • Where are you calling `writeText()`? Just check whether the `Text[]` created or not. – SatyaTNV Jan 28 '16 at 07:12
  • 3
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – hotzst Jan 28 '16 at 07:16

1 Answers1

1

Try with null check. You have to check why its coming null. for safe side you can check it before accessing.

 for (int i = 0; i < t.length; i++) {
            // Write the name, then a # sign, then the surname
            if(t[i]!=null){
            writer.println(t[i].getTitle() + "#" + t[i].getText());
}
}
bNd
  • 7,168
  • 4
  • 35
  • 69