2015-01-17 46 views
0

我想列出HDFS内存在的目录的内容。我尝试了下面的代码:FileSystem listStatus引发NullPointerException

public static void main(String[] args) throws IOException { 
    String uri = args[1]; 
    Configuration conf = new Configuration(); 
    FileSystem fs = FileSystem.get(URI.create(uri),conf); 

    for(int i=0;i<args.length;i++){ 
     System.out.println("Args: " + args[i]); 
    } 

    Path[] paths = new Path[args.length]; 
    for(int i=1; i<args.length;i++){ 
     paths[i] = new Path(args[i]); 
     System.out.println(paths[i]); 
    } 

    FileStatus[] status = fs.listStatus(paths); 
    Path[] listedPaths = FileUtil.stat2Paths(status); 
    for(Path p : listedPaths){ 
     System.out.println(p); 
    } 
} 

但我得到一个异常:

Exception in thread "main" java.lang.NullPointerException 
    at org.apache.hadoop.fs.FileSystem.fixRelativePart(FileSystem.java:2198) 
    at org.apache.hadoop.hdfs.DistributedFileSystem.listStatus(DistributedFileSystem.java:697) 
    at org.apache.hadoop.fs.FileSystem.listStatus(FileSystem.java:1482) 
    at org.apache.hadoop.fs.FileSystem.listStatus(FileSystem.java:1559) 
    at org.apache.hadoop.fs.FileSystem.listStatus(FileSystem.java:1539) 
    at com.demo.ListStatusDemo.main(ListStatusDemo.java:29) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.apache.hadoop.util.RunJar.main(RunJar.java:212) 

请让我知道这一点。

回答

2

的问题是在你这里代码:

for(int i=1; i<args.length;i++){ 
    paths[i] = new Path(args[i]); 
.... 

,因为你的循环为基础1,你离开路径[0] == NULL(即未分配)

+0

我改变了指数'路径[i-1] =新路径(args [i]);'但仍然得到相同的异常。请指教。 – user182944

+0

@ user182944如果你做了'paths [i-1]',那么'paths [paths.length-1]'的路径将为空,因此是例外。 –

+0

是的,只要{Path [] paths = new Path [args.length-1];} – yurgis

相关问题