简体   繁体   中英

Java Cannot instantiate the type Module (Not Abstract Class)

I'm having issues compiling my code. eclipse gives me this error code:

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)

Here's my model code :

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();
}
}

And here's my module code:

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();
}
}

I honestly have no idea why this is happening. I've googled my error before and I only found issues that involved abstract classes.

You are importing the wrong Module - you are importing:

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

You need to import your own Module.

That's because your imported "com.sun.xml.internal.ws.api.server.Module" in your Model class. Try taking that out and importing your.package.Module instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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