| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 382 人关注过本帖
标题:求教 求教 ,txt读取 ,然后存入树中. 为什么呢?为什么呢?求解啊~高手们 ...
只看楼主 加入收藏
ericstan
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-11-20
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
求教 求教 ,txt读取 ,然后存入树中. 为什么呢?为什么呢?求解啊~高手们。头疼。
我想将读取一个.txt 中的记录,例如
6274  John         Johnson         315.56
2843  Mark            Wilson          7217.23
4892  Dana           Albright     31462.56

将每一行记录,生成一个 Employee 类的 object .
然后将它存入 Binary Search Tree 中...一条一条加入树中。
但是在存入树中时,抛出了一个 NullPointer 异常
实在不解,为什么?

程序代码:
public class Lab5
{
   public static void main (String[] args) throws IOException
   {
      Company company = new Company();
      company.menu("1 - Employees.txt");
    }
}

public class Company
{
    public void menu(String line)throws IOException
    {
        String delim = "- \r\t";
        String file = "";
        int id = 0;
        String lastName="" , firstName= "";
        double salary = 0.0;
        TreeBag bsTree = null;
        
        StringTokenizer tokenizer  = new StringTokenizer(line,delim);
        int command = Integer.parseInt(tokenizer.nextToken());
        
        switch(command)
        {            
            case 1:  //创建一个树,然后将.txt中的记录存入。
                while(tokenizer.hasMoreTokens())
                {
                    file     = tokenizer.nextToken();
                }
                BufferedReader fileIn = new BufferedReader(new FileReader(file));
                String record = fileIn.readLine();
                while(record != null )
                {
                    StringTokenizer tokenizer2 = new StringTokenizer(record);                
                    while(tokenizer2.hasMoreTokens())
                    {
                        id        = Integer.parseInt(tokenizer2.nextToken());
                        lastName  = tokenizer2.nextToken();
                        firstName = tokenizer2.nextToken();
                        salary    = Double.parseDouble(tokenizer2.nextToken());    
                        Employee employee = new Employee(id,lastName,firstName,salary);    
                        //System.out.println( employee.tostring());
                        bsTree.add(employee);//这里抛出nullpointer 异常
                    }
                    record = fileIn.readLine();             
                }
                break;

                                ......
                            }//end switch
                        }//end main
}//end class 



public class TreeBag
{
        private BTNode root ;
        
        public TreeBag()
        {
            root = null;
        }

        public void add(Comparable element)
        {
            addNode(element,root);
        }
        
        public BTNode addNode(Comparable element , BTNode p)
        {
            if(p == null)
                p = new BTNode(element, null ,null);
            
            else if((p.getData())<0)
                p.setLeft(addNode(element,p.getLeft()));
            
            else if((p.getData())>0)
                p.setRight(addNode(element,p.getRight()));
            
            else 
                p.setData(element);
                
            return p;
        }
}


除此之外,当然有 个class  BTNode {...}  , 就不列不来了。
搜索更多相关主题的帖子: 记录 
2011-05-20 15:14
ericstan
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-11-20
收藏
得分:0 
找到错误了.....晕!
2011-05-20 15:38
w123012306
Rank: 9Rank: 9Rank: 9
来 自:湖南
等 级:蜘蛛侠
威 望:4
帖 子:307
专家分:1180
注 册:2010-4-22
收藏
得分:14 
仔细!
我也经常犯错误!

楼上,楼下的一定要幸福开心哦!
2011-05-20 18:22
ericstan
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-11-20
收藏
得分:0 
是啊。有错误才会进步啊。
2011-05-25 10:55
快速回复:求教 求教 ,txt读取 ,然后存入树中. 为什么呢?为什么呢?求解啊~ ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018398 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved