2014-02-24 27 views
0

我在编译我的代码时遇到了问题。日食给了我这个错误代码:Java无法实例化类型Module(Not Abstract Class)

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Cannot instantiate the type Module 
Cannot instantiate the type Module 

at Model.AddModule(Model.java:214) 
at Model.loadFromTextFiles(Model.java:129) 
at Model.menu(Model.java:98) 
at Run.main(Run.java:7) 

这里是我的模型代码:

import java.beans.XMLDecoder; 
import java.beans.XMLEncoder; 
import java.io.*; 
import java.util.*; 

import com.sun.xml.internal.ws.api.server.Module; 

public class Model implements java.io.Serializable { 

private Student[] StudentList = new Student[0]; 
private Module[] ModuleList = new Module[0]; 

public void runTests() throws FileNotFoundException { 
    Scanner scan=new Scanner (System.in); 
    System.out.println("Loading From text files"); 
    scan.nextLine(); 
    loadFromTextFiles(); 
    printReport(); 
    System.out.println("Saving serializable"); 
    scan.nextLine(); 
    saveSer(); 
    System.out.println("Creating a new module(CS12330)"); 
    scan.nextLine(); 
    String[] temp=new String[0]; 
    AddModule("CS12330",temp); 
    printReport(); 
    System.out.println("Loading from serialixed file"); 
    scan.nextLine(); 
    loadSer(); 
    printReport(); 
    System.out.println("Saving using XML"); 
    scan.nextLine(); 
    saveXML(); 
    System.out.println("Creating a new module(CS15560)"); 
    scan.nextLine(); 
    AddModule("CS15560",temp); 
    printReport(); 
    System.out.println("Loading from XML file"); 
    scan.nextLine(); 
    loadXML(); 
    printReport(); 
} 

public void menu() throws FileNotFoundException { 
    while (true) { 
     System.out.println("Menu"); 
     System.out.println("1-Run Tests"); 
     System.out.println("2-Add Student"); 
     System.out.println("4-Add Module"); 
     System.out.println("5-Add A Student To Module"); 
     System.out.println("6-Save (Text File)"); 
     System.out.println("7-Save (Serialization)"); 
     System.out.println("8-Save (XML)"); 
     System.out.println("9-Load (Text File)"); 
     System.out.println("10-Load (Serialization)"); 
     System.out.println("11-Load (XML)"); 

     Scanner scan=new Scanner (System.in); 
     String response=scan.nextLine(); 
     if (response.equals("1")){ 
      runTests(); 
     } else if (response.equals("2")) { 
      System.out.print("Enter UID: "); 
      String UID=scan.nextLine(); 
      System.out.print("Enter Surname: "); 
      String surname=scan.nextLine(); 
      System.out.print("Enter First Name: "); 
      String firstname=scan.nextLine(); 
      System.out.print("Course Code: "); 
      String courseCode=scan.nextLine(); 
      AddStudent(UID,surname,firstname,courseCode); 
     } else if (response.equals("4")) { 
      System.out.print("Enter Module Code: "); 
      String moduleCode=scan.nextLine(); 
      String[] temp=new String[0]; 
      AddModule(moduleCode,temp); 
     } else if (response.equals("5")) { 
      System.out.print("Enter Module Code: "); 
      String moduleCode=scan.nextLine(); 
      Module m=findAModule(moduleCode); 
      scan.nextLine(); 
      if(m!=null){ 
       System.out.print("Enter UID: "); 
       String UID=scan.nextLine(); 
       Student s=findAStudent(UID); 
       if (s!=null) { 
        //m.addThisStudent(s); 
       }else System.out.println("Student Not Found"); 
      }else System.out.println("Module Not Found"); 

     } else if (response.equals("6")) { 
      saveToTextFiles(); 
     } else if (response.equals("7")) { 
      saveSer(); 
     } else if (response.equals("8")) { 
      saveXML(); 
     } else if (response.equals("9")) { 
      loadFromTextFiles(); 
     } else if (response.equals("10")) { 
      loadSer(); 
     } else if (response.equals("11")) { 
      loadXML(); 
     } 
    } 
} 

public void loadFromTextFiles() throws FileNotFoundException { 
    Scanner infile=new Scanner(new InputStreamReader(new FileInputStream("students.txt"))); 
    int num=infile.nextInt();infile.nextLine(); 
    for (int i=0;i<num;i++) { 
     String u=infile.nextLine(); 
     String sn=infile.nextLine(); 
     String fn=infile.nextLine(); 
     String c=infile.nextLine(); 
     AddStudent(u,sn,fn,c); 
    } 
    infile.close(); 

     infile=new Scanner(new InputStreamReader(new FileInputStream("modules.txt"))); 
     num=infile.nextInt();infile.nextLine(); 
     for (int i=0;i<num;i++) { 
      String c=infile.nextLine(); 
      int numOfStudents=infile.nextInt();infile.nextLine(); 
      String[] students = new String[numOfStudents]; 
      for (int j=0;j<numOfStudents;j++) { 
       students[j] = infile.nextLine(); 
      } 

      AddModule(c,students); 
     } 
     infile.close(); 
    } 
public void saveToTextFiles() { 
    try { 
     PrintWriter outfile = new PrintWriter(new OutputStreamWriter (new FileOutputStream("Students.txt"))); 
     outfile.println(StudentList.length); 
     for(int i=0;i<StudentList.length;i++) { 
      outfile.println(StudentList[i].getUID()); 
      outfile.println(StudentList[i].getSName()); 
      outfile.println(StudentList[i].getFName()); 
      outfile.println(StudentList[i].getDegree()); 
     } 
     outfile.close(); 

     outfile = new PrintWriter(new OutputStreamWriter (new FileOutputStream("Modules.txt"))); 
     outfile.println(ModuleList.length); 
     for(int i=0;i<ModuleList.length;i++) { 
      outfile.println(ModuleList[i].getCode()); 
      outfile.println(ModuleList[i].getStudents().length); 
      for (int j=0;j<(ModuleList[i]).getStudents().length;j++) { 
       outfile.println(ModuleList[i].getStudents()[j]); 
      } 
     } 
     outfile.close(); 
    } 
    catch(IOException e) { 
    } 
} 

public void loadSer() { 
    Model m =null; 
    try{ 
     FileInputStream fileIn = new FileInputStream("Model.ser"); 
     ObjectInputStream in = new ObjectInputStream(fileIn); 
     m=(Model) in.readObject(); 
     in.close(); 
     fileIn.close(); 
    }catch (Exception e){} 
    if (m!=null) { 
     setStudentList(m.getStudentList()); 
     setModuleList(m.getModuleList()); 
    } 
} 

public void saveSer() { 
try { 
    FileOutputStream fileOut = new FileOutputStream("Model.ser"); 
    ObjectOutputStream out = new ObjectOutputStream(fileOut); 
    out.writeObject(this); 
    out.close(); 
    fileOut.close(); 
} catch(IOException e) {} 
} 

public void loadXML() { 
    try { 
     Model m = null; 
     XMLDecoder decoder = new XMLDecoder (new BufferedInputStream (new FileInputStream("model.xml"))); 
     m = (Model) decoder.readObject(); 
     decoder.close(); 
     setStudentList(m.getStudentList()); 
     setModuleList(m.getModuleList()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

public void saveXML() { 
    try { 
     Model m = null; 
     XMLEncoder encoder = new XMLEncoder (new BufferedOutputStream (new FileOutputStream("model.xml"))); 
     encoder.writeObject(this); 
     encoder.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private void AddModule(String c, String[] students) { 
    int length = ModuleList.length; 
    Module NewArray[]=new Module[length+1]; 
    for (int i=0;i<length+1;i++) { 
     if (i<length) { 
      NewArray[i]=new Module(ModuleList[i]); 
     } 
    } 
    NewArray[length]=new Module(c, students); 
    ModuleList = NewArray.clone(); 


} 
private void AddStudent(String u, String sn, String fn, String c) { 
    int length = StudentList.length; 
    Student NewArray[]=new Student[StudentList.length+1]; 
    for (int i=0;i<StudentList.length+1;i++) { 
     if (i<length) { 
      NewArray[i]=new Student(StudentList[i]); 
     } 
    } 
    NewArray[length]=new Student(u,sn,fn,c); 
    StudentList = NewArray.clone(); 

} 
public void printReport() { 
    for (int i= 0;i<ModuleList.length;i++) { 
     System.out.println(ModuleList[i].toString(this)); 
    } 
} 
public Student findAStudent(String UID) { 
    for(int i=0;i<StudentList.length;i++) { 
     if (StudentList[i].getUID().compareTo(UID)==0) { 
      return StudentList[i]; 
     } 
    } 
    return null; 
} 
public Module findAModule(String moduleCode) { 
    for(int i=0;i<ModuleList.length;i++) { 
     if (ModuleList[i].getCode().compareTo(moduleCode)==0) { 
      return ModuleList[i]; 
     } 
    } 
    return null; 

} 

public Module[] getModuleList() { 
    return ModuleList; 
} 

public Student[] getStudentList() { 
    return StudentList; 
} 

public void setModuleList(Module[] m) { 
    ModuleList=m.clone(); 
} 

public void setStudentList(Student[] s) { 
    StudentList=s.clone(); 
} 
} 

这是我的模块代码:

public class Module{ 

private String Code; 
private String[] students = new String[0]; 

public Module(){}; 

public Module(String c, String[] s) { 
    Code=c; 
    students=s; 
} 
public Module(Module module) { 
    Code=module.getCode(); 
    students=module.getStudents(); 
} 
public String[] getStudents() { 
    return students; 
} 
public String getCode() { 
    return Code; 
} 
public void addThisStudent(Student s) { 
    AddStudent(s.getUID()); 
} 

public String toString(Model mod) { 
    String studentString =""; 
    if (students.length == 0) { 
     studentString = "\n No Students"; 
    }else { 
     for (int i=0;i<students.length;i++) { 
      Student s = mod.findAStudent(students[i]); 
      if (s!=null) { 
       studentString += "\n "+s.toString(); 
      } 
     } 
    } 
    return Code + studentString; 
} 
private void AddStudent(String UIDToAdd) { 
    int length = students.length; 
    String NewArray[]=new String[students.length+1]; 
    for (int i=0;i<length;i++) { 
     NewArray[i] = students[i]; 
    } 
    NewArray[length] = UIDToAdd; 
    students = NewArray.clone(); 
} 
} 

老实说,我不知道为什么会这样。我之前搜索了我的错误,只发现涉及抽象类的问题。

+0

重新编译你的'Module'类。 –

+0

它们在同一个包中吗? –

+0

单击窗口 - 显示视图并选择显示名为“问题”的视图。保持此视图始终处于打开状态,并且如果在该视图中仍然列出编译错误,请不要尝试执行您的程序。阅读错误信息,理解它们的含义,并修复编译错误。一旦没有任何错误,那么只有你可以开始你的程序。另外,学习并尊重Java命名约定。变量和方法以小写字母开头。 –

回答

1

要导入的错误的模块 - 要导入:

import com.sun.xml.internal.ws.api.server.Module; 

您需要导入自己的模块。

0

这是因为您的Model类中导入了“com.sun.xml.internal.ws.api.server.Module”。尝试取出并导入your.package.Module。