-1
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();

Roman C
  • 49,761
  • 33
  • 66
  • 176
mpluse
  • 1,857
  • 6
  • 18
  • 25
  • Can you post more code? Namely around tabT. It appears to be null. –  Mar 10 '13 at 22:55
  • 5
    It's likely that `tabT` or one of its elements is `null`. – Oliver Charlesworth Mar 10 '13 at 22:56
  • btw, you shouldn't use String to concat man strings. You'd better use StringBuilder – RiaD Mar 10 '13 at 23:01
  • Why don't you use an ArrayList instead of an array? Given your algorithm, it's obvious that some elements of the tabT array are null. So you should check that before calling `toString()` on them. – JB Nizet Mar 10 '13 at 23:05
  • @JBNizet Because array is more familiar to me. I can't use ArrayList very well – mpluse Mar 10 '13 at 23:10
  • Then you should definitely learn using it. You're reinventing the wheel and making your own life difficult. Lists are essential data structure that every Java programmer uses every day. – JB Nizet Mar 10 '13 at 23:12
  • @JBNizet I will. But, what about this problem? – mpluse Mar 10 '13 at 23:17
  • Even when I remove = "" from descT. It says « the local variable descT may not have been initialized » – mpluse Mar 10 '13 at 23:18
  • show `AutoSuperieur.main`, line 6 please – RiaD Mar 10 '13 at 23:31
  • @RiaD Check what i've added – mpluse Mar 10 '13 at 23:41
  • @mpluse: I told you about the problem, and several others also did: you have null elements in your array, but are calling toString() on these null elements. Check they're not null before calling toString on them. – JB Nizet Mar 11 '13 at 08:18
  • 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) – Raedwald Jan 24 '16 at 17:52

2 Answers2

0
if (tabT != null) 
  descT += (tabT[i] == null?"":tabT[i].toString());

To make sure you understand it how it works. You've got NullPointerException in the toString(). Then you should check what variable could be null value. After editing your question I see that tabT is initialized, so the if (tabT != null) check is not nessesary but it protect the code above from the NullPointerException because in the second line I use reference tabT that might not been initialized. Next the operator [] dereferncing the elements of the array that could be null value. The ternary operator ? checks that value and if it's null then returns "" otherwise toString() the value of the element. To make this call possible the value should not be null. Then I use grouping () to return the result string to the concatenate operator += that incrementally concatenates old and new values.

Roman C
  • 49,761
  • 33
  • 66
  • 176
0

You may use String.valueOf() to handle nulls.

descT += String.valueOf(tabT[i]);

Besides, operator += performes copying(at least in general case). You should better use StringBuilder to handle big concatenations.

RiaD
  • 46,822
  • 11
  • 79
  • 123