я понял что ошибка в методе ToString. Именно при рекурсивном выводе строка обновляется, как мне ее исправить? public class Node { int x; Node l, r; public Node() {} public Node(int x){ this.x = x; } public void add(int x){ //Добавление элемента в дерево if(this == null){ // ТУТ ОШИБКА this.x = x; // Если дерево пусто то создаем его } if(x < this.x){ // Если добавляемое значение меньше значения узла if(l != null) l.add(x); // Если узел не пуст вызываем рекурсивно else{ l = new Node(x); // Если нашли пустое место создаем узел } } if(x > this.x){ // Если добавляемое значение больше значения узла if(r != null) r.add(x);// Если узел не пуст вызываем рекурсивно else{ r = new Node(x); // Если нашли пустое место создаем узел } } } public String toString() { String s= null; if(this != null) { // ТУТ ОШИБКА l.toString(); s += this.x + " "; r.toString(); } return s; } } 

Maine

 public static void main(String[] args) { Random r = new Random(); Node Tree = new Node(); for (int i = 0; i < 5; i++) { Tree.add(r.nextInt(100)); } System.out.println(Tree.toString()); } 

please tell me throws an error

 Exception in thread "main" java.lang.NullPointerException at Node.toString(Node.java:31) at Start.main(Start.java:13) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 
  • 3
    this be null ? This error report does not correspond to the given source code. Maybe something was not compiled there and a completely different version works? The error should be on the string s += this.x... because s = null at the very beginning and before it changes. Or just someone can not count the lines? - Sergey
  • 7
    Something I didn’t understand at all the essence of this == null checks, inside the non-static method of this by definition, cannot be null (and inside a static compiler will not allow you to refer to this in this context). Do you understand that this is a link to the current instance of a class (when you work inside non-static class methods, then you are by definition working with a specific instance of a class)? - StateItPrimitive
  • 2
    So, the system says that the error in Node.toString (Node.java:31) and it is quite possible that this method is called before the vertices l , r - Grundy are initialized
  • Add the main method code - Grundy
  • one
    @Artem add the code to the question (using the "edit" ), and not in the comments. - StateItPrimitive 1:01 pm

2 answers 2

The basic error is that you check this , which cannot be null , but the class fields

 Node l, r; 

Which are not initialized, do not check.

 public String toString() { String s= ""; if(l != null) { // ТУТ ОШИБКА s = l.toString(); } s += this.x + " "; if(r != null){ s += r.toString(); } return s; } 
     public String toString() { StringBuilder sb = new StringBuilder(); if (l != null) { // ТУТ ОШИБКА sb.append(l).append(" "); } sb.append(this.x); if (r != null) { sb.append(" ").append(r); } return sb.toString(); }