Question: Using DrJava create a program that does the following: You are given a file that contains data describing a binary tree whose nodes are strings.

Using DrJava create a program that does the following:

You are given a file that contains data describing a binary tree whose nodes are strings. The file begins with an integer N, the number of nodes in the tree, on a line by itself. This first line is followed by N additional lines where each line specifies child information for a node in the tree. The line for a node X starts with X itself followed by a list of the children of X. Names of nodes are separated by whitespace. For example, the data set

5

Al Bob Carol

Bob Debby Elaine

Carol

Debby

Elaine

represents a binary tree in which Al has two children named Bob and Carol; Bob has two children named Debby and Elaine; and Carol, Debby, and Elaine have no children. Write a program that reads in data from such a file and displays it in a JTree component.

This requires you to read in a file with the characteristics given in the text. You will have to read in this file and then display the tree by

Read in the first line of the file and create a binary tree.

Add nodes to the binary tree using the subsequent lines in the file.

Display your tree graphically using javax.swing

Thank you to all who take a look and try to help!

My code:

import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode;

/** * JTreeDemo class * */ public class JTreeDemo { private int numberOfNodes; //number of nodes in tree private String data[]; //tree data private JTree tree; //JTree component //Constructor public JTreeDemo() { numberOfNodes=5; data=new String[numberOfNodes]; tree=new JTree(); } //reads tree data from file public void readData(String fileName) throws FileNotFoundException { FileInputStream inputStream=new FileInputStream(fileName); InputStreamReader reader=new InputStreamReader(inputStream); BufferedReader bufferedReader=new BufferedReader(reader); String line; int countNode=0; try { while((line=bufferedReader.readLine())!=null) { if(line.matches("^-?\\d+$")) { numberOfNodes=Integer.parseInt(line); } else { data[countNode]=line; countNode++; } } inputStream.close(); reader.close(); bufferedReader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //populates tree data in JTree public void populateTree() { DefaultMutableTreeNode rootNode=new DefaultMutableTreeNode(); for(int i=0;i1) { rootNode.add(new DefaultMutableTreeNode(nodeValues[0])); for(int j=1;j

/** * main() function * @param args * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException { // TODO Auto-generated method stub String fileName="data.txt"; //input file //create and show tree JTreeDemo treeDemo=new JTreeDemo(); treeDemo.readData(fileName); treeDemo.populateTree(); treeDemo.createAndShowGUI(); } }

Breaks when ran:

java.lang.NullPointerException at JTreeDemo.populateTree(JTreeDemo.java:64) at JTreeDemo.main(JTreeDemo.java:104) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!