加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 返回null的Class.forName

发布时间:2020-12-15 04:56:09 所属栏目:Java 来源:网络整理
导读:我看过一篇关于Class.forName是否可以返回null here的帖子,每个人似乎认为它不能(或不会).但是,它使用以下代码为我返回null: public void init() { File binDriectory = new File("./bin/dft"); String[] files = binDriectory.list(); for (String file :
我看过一篇关于Class.forName是否可以返回null here的帖子,每个人似乎认为它不能(或不会).但是,它使用以下代码为我返回null:

public void init() {
    File binDriectory = new File("./bin/dft");
    String[] files = binDriectory.list();
    for (String file : files) {
      if (file.endsWith(".class")) {
        if (file.indexOf("DataReader") > 0) {
          //strip off the ".class"
          String className = file.substring(0,file.indexOf(".class"));

          try {
            //load the class
            Class readerclass = Class.forName("dft." + className);
            //get the file extension of the file type this class deals with

            /* NullPointerException thrown here in call to getMthod() */

            Method getExtensionMethod = readerClass.getMethod("getFileExtension",null);
            String extension = (String) getExtensionMethod.invoke(readerClass.newInstance(),null);
            //add the extension and class to the class map
            classMap.put(extension,readerClass);
            //add the extension to the list of reader file extensions
            readerExtensions.add(extension);
          }
          catch (ClassNotFoundException e) {
            System.err.println("**WARNING: class not found: dft." + className);
            continue;
          }
          catch (NoSuchMethodException e) {
            System.err.println("**WARNING: class dft." + className + " does "
                               + "not contain a getFileExtension() method.");
            continue;
          }
          catch (InstantiationException e) {
            System.err.println("**WARNING: could not create an instance of "
                               + "class dft." + className);
            continue;
          }
          /* with Java 7,these next two catch blocks can be combined (and they
             should) */
          catch (InvocationTargetException e) {
            System.err.println("**WARNING: could not invoke getFileExtension()"
                               + " method from class dft." + className);
            continue;
          }
          catch (IllegalAccessException e) {
            System.err.println("**WARNING: could not invoke getFileExtension()"
                + " method from class dft." + className);
            continue;
          }

          System.out.println(className);
        }
        else if (file.indexOf("DataWriter") > 0) {
          System.out.println(file);
        } 
      }
    }
  }

不抛出ClassNotFoundException,但forName()的结果为null.文档没有说明返回null.

有没有人知道为什么会这样?我在另一个不使用包名的项目中测试了forName()的调用(上面的代码在名为“dft”的包中),并且一个工作正常.我认为这与它有关.类路径也很好 – .class文件在… bin / dft中,类路径包含… / bin.我甚至尝试在类路径中显式添加… / bin / dft目录以防万一,它仍然返回null.

解决方法

您正在分配forName返回的值

readerclass(小写C)

但是从中调用getMethod

readerClass(大写C)

这可能是一个未初始化的领域.

Java区分大小写.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读