Expand|Select|Wrap|Line Numbers
- import java.util.*;
- import java.io.*;
- import java.math.*;
- import java.awt.event.*;
- class Node
- {
- String ortu,isi;
- int jumanak,jumchild;
- Node child[] = new Node[5];
- public Node(String parent, String a)
- {
- ortu=parent;
- isi=a;
- jumanak=jumchild;
- }
- }
- public class tree
- {
- private Node root;
- private boolean isEmpty()
- {
- return (root == null);
- }
- public void insert(String x,String y, int pos)
- {
- int coba;
- coba = 0;
- Node temp = new Node(x,y);
- if (isEmpty())
- {
- root = temp;
- System.out.println("root awal:"+root.isi);
- root.jumchild=0;
- }
- else
- {
- root.jumchild++;
- System.out.println("jum child:"+root.jumchild+"y:"+y);
- Node cursor = root,
- parent = null;
- coba = 1;
- for ( int i=1;i<root.jumchild;i++)
- {
- if ( root.child[i-1].isi == y )
- {
- coba = 0;
- System.out.println("child ke "+root.child[i-1].isi+" sama dengan "+y);
- }
- else
- {
- coba = 1;
- }
- }
- if ( coba == 1 )
- {
- while (cursor != null)
- {
- parent = cursor;
- cursor = cursor.child[pos];
- parent.child[pos] = temp;
- System.out.println("parent : "+root.child[pos].ortu+" isi : "+root.child[pos].isi);
- }
- }
- }
- }
- public void level2()
- {
- Node tempo=new Node("C1","C1.1");
- Node curs=root.child[0], ortu=null;
- ortu=curs;
- curs=curs.child[0];
- ortu.child[0] = tempo;
- System.out.println("parent : "+root.child[0].child[0].ortu+" isi : "+root.child[0].child[0].isi);
- }
- public void baca()
- {
- String file="bacatulis.txt";
- String [] kolomtxtcek;
- String baristxtcek,a,b;
- int c;
- int jum=0;
- try
- {
- BufferedReader baca = new BufferedReader(new FileReader(file));
- while ((baristxtcek = baca.readLine()) != null)
- {
- kolomtxtcek = baristxtcek.split(",");
- a=kolomtxtcek[0];
- b=kolomtxtcek[1];
- c=Integer.parseInt(kolomtxtcek[2]);
- insert(a,b,c);
- jum++;
- }
- }
- catch (IOException e)
- {
- System.err.println("Error: " + e);
- }
- System.out.println(jum);
- }
- public static void main (String[] args)
- {
- tree pohon=new tree();
- pohon.baca();
- pohon.level2();
- }
- }
Exception in thread "main" java.lang.NullPointerException
at tree.insert(tree.java:55)
at tree.baca(tree.java:106)
at tree.main(tree.java:137)