gpt4 book ai didi

Java无法实例化类型模块(不是抽象类)

转载 作者:行者123 更新时间:2023-12-01 13:21:29 25 4
gpt4 key购买 nike

我在编译代码时遇到问题。 eclipse 给我这个错误代码:

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

老实说,我不知道为什么会发生这种情况。我之前用谷歌搜索过我的错误,只发现了涉及抽象类的问题。

最佳答案

您正在导入错误的模块 - 您正在导入:

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

您需要导入自己的模块。

关于Java无法实例化类型模块(不是抽象类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21999892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com