Java – returns a null class forName

I read an article about class Whether forname can return null here posts, everyone seems to think it can't (or won't) However, it returns null for me using the following code:

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 is not thrown, but the result of forname() is null The document has no description and returns null

Does anyone know why? I tested the call to forname () in another project that does not use the package name (the above code is in the package named "DFT"), and one works normally I think it has something to do with it Classpath is also good – The class file is in... Bin / DFT, and the class path contains... / bin I even tried to explicitly add the... / bin / DFT directory to my classpath in case it still returned null

Solution

You are assigning the value returned by forname

Readerclass (lowercase C)

But call getmethod from it.

Readerclass (capital C)

This may be an uninitialized domain

Java is case sensitive

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>