A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王卿 黑马帝   /  2011-7-19 10:18  /  1957 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

为什么这里会出错???


“Exception in thread "main" java.lang.NullPointerException





import java.io.*;
import java.util.*;

class CopyFile
{
       String newFileDir; //新的路径
       String srcFileDir;    //原来的目录
       String desFileDir;   //新的目录
       File srcFile;
      
       CopyFile( String srcFileDir,String desFileDir )
       {
              this.srcFileDir=srcFileDir;
              this.desFileDir=desFileDir;
              srcFile=new File(srcFileDir);
       }
      
      
      
       public void start(  ) throws Exception
       {     
              getList( srcFile );
              System.out.println( "d" );
       }
      
      
       private void getList( File f ) throws Exception
       {
              
              File fileList[]=f.listFiles();
              for( int i=0; i<fileList.length; i++ )
              {
                     if( fileList.isDirectory() )
                     {
                            copyDirectory( fileList );
                            getList( fileList ) ;                    
                     }
                     else
                     {
                            copyFile( fileList );
                           
                     }
              }
       }
      
       private void copyDirectory( File f )
       {
              File desDirectory=new File( replacePath(f));
              if( !(desDirectory.exists()))
              {
                     desDirectory.mkdir();
              }
              //System.out.println( desDirectory.getAbsolutePath() );
       }
      
       private void copyFile( File f ) throws Exception
       {
              FileOutputStream fos=
                     new FileOutputStream( f );
              File newFile=new File( replacePath(f ));
              newFile.createNewFile();
              FileInputStream fis=
                     new FileInputStream( newFile );
              byte buf[]=new byte[1024];
              int num=0;
              while( (num=fis.read(buf))!=-1 )
              {
                     fos.write(buf,0,num);
              }
              fos.close();
              fis.close();
              //System.out.println( replacePath(f ) );
              
       }
      
       private String replacePath( File f )
       {
              String s=f.getAbsolutePath();
              System.out.println( s );
              s=s.replace( srcFileDir,desFileDir );
              System.out.println( desFileDir );
              System.out.println( srcFileDir );
              System.out.println( s );
              System.out.println( " " );
              return s;
       }

}

class Demo
{
       public static void main(String args[]) throws Exception
       {
              new CopyFile( "E:\\zjs\\day22","D:\\zjs").start();
              
       }
      
      
}
[ 本帖最后由 gdky005 于 2011-07-19  10:47 编辑 ]

评分

参与人数 1技术分 +1 收起 理由
admin + 1 大家都来帮你看看

查看全部评分

3 个回复

正序浏览
黑马网友  发表于 2011-7-19 12:08:32
板凳
[code=java]for( int i=0; i<fileList.length; i++ )
              {
                     if( fileList.isDirectory() )
                     {
                            copyDirectory( fileList );
                            getList( fileList ) ;                     
                     }
                     else
                     {
                            copyFile( fileList );
                           
                     }
              } [/code][code=java]                            copyDirectory( fileList );
                            getList( fileList ) ;   [/code]里面fileList是数组的引用  你没确定传的是确定的路径
改下fileList[i]试试
回复 使用道具 举报
黑马网友  发表于 2011-7-19 10:48:41
藤椅
应该怎么解决呢?
回复 使用道具 举报
黑马网友  发表于 2011-7-19 10:43:18
沙发
getList()中if语句中把数组当成变量实用了;
listFiles()方法,当传进来的不是一个目录时,返回值为null;应该先检查是否为目录;

评分

参与人数 1技术分 +1 收起 理由
admin + 1 参与必须给分

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马