FileSystem listStatus抛出NullPointerException

时间:2015-01-17 14:00:27

标签: hadoop hdfs bigdata

我正在尝试列出HDFS中存在的目录的内容。我尝试了以下代码:

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)

请告诉我。

1 个答案:

答案 0 :(得分:2)

问题在于您的代码:

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

因为你的for循环是从1开始的,所以你留下了路径[0] == null(未分配)