Topic: 找不到类异常的问题

  Print this page

1.找不到类异常的问题 Copy to clipboard
Posted by: eamon100
Posted on: 2005-07-05 16:53

写了一段代码:
package com.Hello;

class Student{
private String name;
Student()
{
   name="Student";
}
void setName(String newName)
{
   name=newName;
}
String getName()
{
   return name;
}
}

public class Hello {
  public static void main(String[] args) {
Class c=null;
    try {
      c = Class.forName("Student");
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
    String name="";
    try {
      name = ((Student)(c.newInstance())).getName();
      System.out.println("name: "+name);
    } catch (InstantiationException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    }
  }
}
明明定义了Student类,编译是却除了如下的异常:
java.lang.ClassNotFoundException: Student
  at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
  at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
  at java.lang.Class.forName0(Native Method)
  at java.lang.Class.forName(Class.java:164)
  at com.Hello.Hello.main(Hello.java:36)
Exception in thread "main" java.lang.NullPointerException
  at com.Hello.Hello.main(Hello.java:44)
请高手指点,谢谢!

2.Re:找不到类异常的问题 [Re: eamon100] Copy to clipboard
Posted by: why
Posted on: 2005-07-05 21:35

try {
c = Class.forName("com.Hello.Student");

3.Re:找不到类异常的问题 [Re: eamon100] Copy to clipboard
Posted by: eamon100
Posted on: 2005-07-05 21:41

问题解决了
Thanks!!


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923