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; //implements the JBtree public class JBtree { //Declare variables private int nodesCount; private String dataInput[]; private JTree treeIns;
//creates the constructor public JBtree() { nodesCount=10; dataInput=new String[nodesCount]; treeIns=new JTree(); }
//Defines the FileDataRead() public void FileDataRead(String fileName) throws FileNotFoundException { //reads the FileInputStream() FileInputStream inputFile=new FileInputStream(fileName); InputStreamReader inputFileReader=new InputStreamReader(inputFile); BufferedReader inputFileReaderbuff=new BufferedReader(inputFileReader); String linesof; int nodeCounts=0; //try blocks try { while((linesof=inputFileReaderbuff.readLine())!=null) { if(linesof.matches("^-?\\d+$")) { nodesCount=Integer.parseInt(linesof); } else { dataInput[nodeCounts]=linesof; nodeCounts++; } } //files close inputFile.close(); //files close() inputFileReader.close(); inputFileReaderbuff.close(); } catch (IOException e1) {
e1.printStackTrace(); } }
//Defines the function populate tree public void populateTreeS() { //creates the node DefaultMutableTreeNode nodeRoot=new DefaultMutableTreeNode(); //for loop for(int it=0;it //computesthe GUI public void createAndShowGUI() { //create the frame JFrame setFrames=new JFrame("JBtree Computation"); setFrames.add(treeIns); setFrames.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setFrames.pack(); setFrames.setVisible(true); } //Program starts with main public static void main(String[] args) throws FileNotFoundException { //Declares file input String nameFile="data.txt"; //Create the tree data JBtree treeDemo=new JBtree(); //checks the location and name of the file treeDemo.FileDataRead(nameFile); treeDemo.populateTreeS(); treeDemo.createAndShowGUI(); } } Breaks when ran: java.lang.NullPointerException at JBtree.populateTreeS(JBtree.java:68) at JBtree.main(JBtree.java:109) 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) Data.txt: 10 Al Bob Carol Bob Debby Elaine Carol Debby 

Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
