2017-05-11 9 views
-5

私のBinaryTree.javaファイルの私のdisplayStudent()メソッドの中に、私は自分のバイナリツリーをファイルに書き込もうとしています。私が持っているコードは私にエラーを投げている。私は成功していないので、なぜ私はここにこのファイルを書くためにさまざまな方法を試してみました。バイナリツリーをファイルに書き込めないのはなぜですか?

ここに私のNode.javaファイルです:

import java.io.IOException; 

public class Main { 
    public static void main(String[] args) throws IOException { 
     Student student1 = new Student("Mike", "Piazza", "S3123456"); 
     Student student2 = new Student("Jack", "Jill", "S3123456"); 
     Student student3 = new Student("Alice", "Jones", "S3123456"); 

     BinaryTree bt = new BinaryTree(); 
     bt.insertStudent(student1); 
     bt.insertStudent(student2); 
     bt.insertStudent(student3); 
     bt.displayStudent(bt.root); 
     } 
    } 

ここに私のStudent.txtファイルがあります:

import java.io.*; 

public class BinaryTree { 
    public Node root; // Declaring Node that's a root. 

    public void insertFaculty(Faculty faculty) { 
     root = insertFaculty(faculty,root); // Let root equal faculty being inserted. 
    } 

    private Node insertFaculty(Faculty faculty, Node t) { 
     if(root == null) { // If the root is empty. 
      return new Node(faculty); // Return a faculty node. 
     } 

     // If inserted faculty last name is less than the current faculty last name 
     if(faculty.getLastName().compareTo(t.data2.getLastName()) < 0) { 
      if(t.left == null) { // If the left node is empty. 
       t.left = new Node(faculty); // Create a left a node that accepts a faculty name. 
      } else { 
       t.left = insertFaculty(faculty,t.left); // Insert faculty members to the left recursively. 
      } 
      return t; // Return node. 
     } 

     // If inserted faculty last name is less than the current faculty last name. 
     else if(faculty.getLastName().compareTo(t.data2.getLastName()) > 0) { 
      if(t.right == null) { // If the right node is empty. 
       t.right = new Node(faculty); // Create a right a node that accepts a faculty name. 
      } else { 
       t.right = insertFaculty(faculty,t.right); // Insert faculty members to the right recursively. 
      } 
     } 
     return t; // Return node. 
    } 

    public void insertStudent(Student s) { 
     root = insertStudent(s,root); // Let root equal student being inserted. 
    } 

    private Node insertStudent(Student s, Node t) { 
     if(root == null) { // If the root is empty. 
      return new Node(s); // Return a Student node. 
     } 

     // If inserted student last name is less than the current student last name. 
     if(s.getLastName().compareTo(t.data.getLastName()) < 0) { 
      if(t.left == null) { // If the left node is empty. 
       t.left = new Node(s); // Create a left a node that accepts a student name. 
      } else { 
       t.left = insertStudent(s,t.left); // Insert student members to the left recursively. 
      } 
      return t; // Return node; 
     } 

     // If inserted student last name is greater than the current student last name. 
     else if(s.getLastName().compareTo(t.data.getLastName()) > 0) { 
      if(t.right == null) { // If the right node is empty. 
       t.right = new Node(s); // Create a right a node that accepts a student name. 
      } else { 
       t.right = insertStudent(s,t.right); // Insert student members to the right recursively. 
      } 
     } 
     return t; // Return name. 
    } 

    public Node studentDelete(Student s, Node t) { 
     if(t != null) { // If t is empty. 

      // If inserted student last name is less than the current student last name. 
      if(s.getLastName().compareTo(t.data.getLastName()) < 0) { 
       t.left = studentDelete(s,t.left); // Left node will delete appropriate name recursively. 
       // If inserted student last name is greater than the current student last name. 
      } else if(s.getLastName().compareTo(t.data.getLastName()) > 0) { 
       t.right = studentDelete(s,t.right); // Right node will delete appropriate name recursively. 
      } else { 
       if(t.left == null) { // If left node is empty. 
        return t.right; // Return right node. 
       } else if(t.right == null) { // If right node is empty. 
        return t.left; // Return left node. 
       } 
      } 
     } 
     return t; // Return node. 
    } 

    public Node facultyDelete(Faculty faculty, Node t) { 
     if(t != null) { // If t is empty.   
      // If inserted faculty last name is less than the current faculty last name. 
      if(faculty.getLastName().compareTo(t.data2.getLastName()) < 0) { 
       t.left = facultyDelete(faculty,t.left); // Left node will delete appropriate name recursively. 
       // If inserted faculty last name is greater than the current faculty last name. 
      } else if(faculty.getLastName().compareTo(t.data2.getLastName()) > 0) { 
       t.right = facultyDelete(faculty,t.right); // Right node will delete appropriate name recursively. 
      } else { 
       if(t.left == null) { // If left node is empty. 
        return t.right; // Return right node. 
       } else if(t.right == null) { // If right node is empty. 
        return t.left; // Return left node. 
       } 
      } 
     } 
     return t; // Return node. 
    } 

    public void displayStudent(Node root) throws IOException { 
     if(root != null) { // If root isn't empty. 
      displayStudent(root.left); // Recursively display left nodes. 
      System.out.println(root.data.toString()); // Print to the console data that's in the root in order. 
      displayStudent(root.right); // Recursively display right nodes. 
     } 

     String file = "Student.txt"; // Student text file, this will be used to write in student information. 
     FileWriter fw = null; // The file writer used to write in student information initially set to null. 

     try { 
      fw = new FileWriter(file, true); // Place file in FileWriter, true allows file to appended writer. 
      for(int i = 0; i < 30; i++) { 
       fw.write(root.data.toString()); 
      } 
      fw.close(); // Close file. 
     } catch(FileNotFoundException e) { // Exception will be thrown if the file isn't found. 
      System.out.println("File not found!"); // Message will print if the file isn't found. 
     } 


    } 

    public void displayFaculty(Node root) { 
     if(root != null) { // If root isn't empty. 
      displayFaculty(root.left); // Recursively display left nodes. 
      System.out.println(root.data2.toString()); // Print to the console data that's in the root inorder. 
      displayFaculty(root.right); // Recursively display right nodes. 
     } 
    } 
} 

ここに私のMain.javaファイルがあります:

class Node { 
Student data; 
Faculty data2; 
Node left; 
Node right; 

public Node(Student data) { 
    this.data = data; 
    this.left = null; 
    right = null; 
} 

public Node(Faculty data2) { 
    this.data2 = data2; 
    this.left = null; 
    this.right = null; 
    } 
} 

ここに私のBinaryTree.javaファイルだが、

null null null null null null null 

ここで私は、コンソールに取得していerrorです:

Exception in thread "main" java.lang.NullPointerException 
at BinaryTree.displayStudent(BinaryTree.java:118) 
at BinaryTree.displayStudent(BinaryTree.java:107) 
at BinaryTree.displayStudent(BinaryTree.java:107) 
at Main.main(Main.java:13) 
+0

本当に重複する例はありません。はい、それはnullポインタの例外ですが、それらを作成する方法はたくさんあります。彼らはヌルノードをたくさん作成し、私たちがヌルでない親ノードがあるかどうかだけをチェックしました。 –

+0

@GlenPierceこの質問は将来の訪問者を助けるとは思われない方法で解決されたか、または解決されます。より一般的な複製は、この質問が提供する将来のあらゆる価値を包含し、複製としてそれを閉じることは適切と思われる。 OPが[MCVE](https://stackoverflow.com/help/mcve)を提供していれば(コードはずっと長くなるはずです)、彼ら自身が問題を理解している可能性があります。彼らはそうしなかった、質問は今よりもはるかに大きな未来価値を持っていただろうが、それはおそらく複製にはるかに近いだろう。 – Dukeling

答えて

2

それは印刷した理由「ヌルヌルヌルヌルヌルヌル」あなたdisplayStudent()メソッドで見つけることができます。あなたはstudent(root.right)を表示するように求めていますが、それらのノードをコンストラクタでnullに設定しています。

public Node(Student data) { 
    this.data = data; 
    this.left = null; 
    right = null; 
} 

ですから、そのデータを表示するために行くとき、あなたは効果的displayStudent(null);

if(root != null) { // If root isn't empty. 
    displayStudent(root.left); // Recursively display left nodes. 
    System.out.println(root.data.toString()); // Print to the console data that's in the root in order. 
    displayStudent(root.right); // Recursively display right nodes. 
} 

を実行している私は、あなたがこのようにこの問題を解決することができると思う:あなたがしている

if(root != null) { // If root isn't empty. 
    if(root.left != null){ 
     displayStudent(root.left); // Recursively display left nodes. 
    } 
     System.out.println(root.data.toString()); // Print to the console data that's in the root in order. 
    if(root.right != null){ 
     displayStudent(root.right); // Recursively display right nodes. 
    } 
    } 

理由nullポインタ例外を取得するということは、呼び出し前にヌル値をチェックしていないことです。fw.write(root.data.toString());

代わりに、displayStudent()の各呼び出しの代わりに、StringBuilderを使用して、一度ファイルに書き込むStringに追加します。私はOPへの練習としてそれを残す。

ランダムアドバイス:
は、あなたが持っている方法で学生と教員の両方が含まれているバイナリツリーは、おそらくあなたに悲しいいつかを作るつもりされました。ツリー全体に1つのデータ型しか持たないことをお勧めします(生徒と教員が抽象クラスから拡張されている可能性があります)。

+0

あなたのランダムなアドバイスを本当に感謝します。私は後でそれを考慮に入れますが、まずツリー上のものをファイルに書きたいだけです。 – nampa

+0

私は提案された修正を含むように私の答えを更新しました。 –

+0

もう一度、大変感謝します。 – nampa

関連する問題