繁体   English   中英

未找到类创建对象数组时出现异常

[英]Class not found Exception when creating an object array

所以,我写了这些课:

public class FicherosDeCiudadanos {

   public static int numCiudadanos (File f) {
       try{

           Scanner texto=new Scanner(f);
           int contador=0;
           while(texto.hasNextLine()){
               contador++;
               texto.nextLine();
           }
           texto.close();
           return contador;
       }
   }

   public static  Ciudadano[] leerFichero (File f) {
      try{
          Scanner texto=new Scanner(f);
          //This next line throws classNotFound when debbugging but only 
          //when evaluating Ciudadano[]
          Ciudadano[] tablaCiudadano = new Ciudadano[numCiudadanos(f)];
          ....    
          //TO-DO
   }
}

我验证它有效,而且确实如此,方法leerFichero()完美地完成了这项工作。 它创建了Ciudadano的对象数组,其代码为TO-DO。 但是,在另一个包中,我调用方法leerFichero()并且它到达那里,找出numCiudadanos()来设置数组的长度,但是当它到达Ciudadano[]它抛出ClassNotFoundException ,程序停止。 当我从它自己的包中使用它时它仍然有效。

我猜这是堆栈

Thread [main] (Suspended)   
owns: Object  (id=36)   
owns: Object  (id=37)   
ClassNotFoundException(Throwable).<init>(String, Throwable) line: 286   
ClassNotFoundException(Exception).<init>(String, Throwable) line: not available 
ClassNotFoundException(ReflectiveOperationException).<init>(String, Throwable) line: not available  
ClassNotFoundException.<init>(String) line: not available   
URLClassLoader$1.run() line: not available  
URLClassLoader$1.run() line: not available  
AccessController.doPrivileged(PrivilegedExceptionAction<T>, AccessControlContext) line: not available [native method]   
Launcher$ExtClassLoader(URLClassLoader).findClass(String) line: not available   
Launcher$ExtClassLoader(ClassLoader).loadClass(String, boolean) line: not available 
Launcher$AppClassLoader(ClassLoader).loadClass(String, boolean) line: not available 
Launcher$AppClassLoader.loadClass(String, boolean) line: not available  
Launcher$AppClassLoader(ClassLoader).loadClass(String) line: not available  
FicherosDeCiudadanos.leerFichero(File) line: 54 
Operaciones.nombreCliente(int) line: 80 
Operaciones.listadoCompras() line: 37   
TrabajoProg1.ejecutarOperacion(int) line: 40    
TrabajoProg1.main(String[]) line: 22    

这可能是构建路径问题。 您需要右键单击外部包并转到

构建路径>配置构建路径

然后导航到

项目

然后,单击Add...并添加包含FicherosDeCiudadanos的项目。

确保在您的调用类的顶部有一行:

import package1.FicherosDeCiudadanos

其中package1是包含FicherosDeCiudadanos的包

有时调试器与某些类不同步。

尝试清理/构建项目,甚至重新启动Eclipse(或其他IDE)。 然后再次尝试调试。

所以,最后这个错误没有任何意义,之后又出现了另一个错误,使得它似乎停止了这个错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM