public class X {
// some fields
private Route[] tabT = new Route[50]; // [1]
public String toString() {
int i;
String descT = "";
for (i = 0; i < tabT.length; i++)
descT += tabT[i].toString();
String description;
description = "MESSAGE " + lastName
+ "\nMESSAGE " + firstName
+ "\nMESSAGE " + year
+ "\nMESSAGE " + address
+ "\nMESSAGE " + number
+ "\nMESSAGE " + descT
+ "\n";
return description;
}
My class contains some fields including a list of objects from another class tabT
. In toString()
method, I want to show those fields and the fields of the other object but I don't know why it shows an error. When I make a tab of on element it doesn't show an error.
Exception in thread "main" java.lang.NullPointerException
at Chauffeur.toString(Chauffeur.java:38)
at java.lang.String.valueOf(String.java:2854)
at java.io.PrintStream.println(PrintStream.java:821)
at AutoSuperieur.main(AutoSuperieur.java:6)
It's exactly on this line descT += tabT[i].toString();