filesystem liststatus引发nullpointerexception

lxkprmvk  于 2021-06-04  发布在  Hadoop
关注(0)|答案(1)|浏览(366)

我试图列出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)

请告诉我这件事。

xxe27gdn

xxe27gdn1#

问题在您的代码中:

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

因为for循环是基于1的,所以将路径[0]==null(即未赋值)

相关问题