Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用Java中的对象将字符串添加到列表中_Java_Arrays_Object - Fatal编程技术网

无法使用Java中的对象将字符串添加到列表中

无法使用Java中的对象将字符串添加到列表中,java,arrays,object,Java,Arrays,Object,我正在开发一个基于JSF的Web应用程序,在这个应用程序中,我从一个文件(dumpfile)中读取内容,然后使用一个逻辑对其进行解析,并使用一个对象将其添加到一个列表中,还使用该对象设置一个字符串。但我一直在犯这个错误。我不知道我错在哪里。我是初学者,有人能帮我吗 List<DumpController> FinalDumpNotes; public List<DumpController> initializeDumpNotes() throws SocketExcep

我正在开发一个基于JSF的Web应用程序,在这个应用程序中,我从一个文件(dumpfile)中读取内容,然后使用一个逻辑对其进行解析,并使用一个对象将其添加到一个列表中,还使用该对象设置一个字符串。但我一直在犯这个错误。我不知道我错在哪里。我是初学者,有人能帮我吗

List<DumpController> FinalDumpNotes;
public List<DumpController> initializeDumpNotes()
throws SocketException, IOException {
PostProcessedDump postProcessedDump = (PostProcessedDump) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("postProcessedDump");
List<DumpController> FinalNotes = new ArrayList<>();
if (postProcessedDump.getDumpNotes() == null) {
dumpNotes = new DumpNotes();
}
DumpListController dlcon = (DumpListController) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("dumpListController");
DumpInfo dumpinfo = dlcon.getSelectedDumpInfo();
String fileName = dumpinfo.getDate() + dumpinfo.getTime() + dumpinfo.getSeqNo() + dumpinfo.getType() + dumpinfo.getTape() + dumpinfo.getDescription() + ".txt";
if (checkFileExistsInWin(fileName)) {
postProcessedDump.setDumpnotescontent(getFileContentsFromWin(fileName));
String consolidateDumpnotes = getFileContentsFromWin(fileName);
String lines[];
String content = "";
lines = consolidateDumpnotes.split("\\r?\\n");
List<String> finallines = new ArrayList<>();
int k = 0;
for (int i = 0; i < lines.length; i++) {
if (!lines[i].equalsIgnoreCase("")) {
finallines.add(lines[i]);
k++;
}
}
for (int j = 0; j < finallines.size(); j++) {
if (finallines.get(j).startsWith("---------------------SAVED BY")) {
PostProcessedDump dump = new PostProcessedDump();
dump.setDumpMessage(content);
content = "";
FinalDumpNotes.add(dump);
} else {
content = content + finallines.get(j);
}
}
}
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("postProcessedDump", postProcessedDump);
return FinalDumpNotes;
}
列出最后的注意事项;
公共列表初始化为MPNotes()
抛出SocketException,IOException{
PostProcessedDump PostProcessedDump=(PostProcessedDump)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(“PostProcessedDump”);
List FinalNotes=new ArrayList();
if(postProcessedDump.getDumpNotes()==null){
dumpNotes=新的dumpNotes();
}
DumpListController dlcon=(DumpListController)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(“DumpListController”);
DumpInfo DumpInfo=dlcon.getSelectedDumpInfo();
字符串文件名=dumpinfo.getDate()+dumpinfo.getTime()+dumpinfo.getSeqNo()+dumpinfo.getType()+dumpinfo.getTape()+dumpinfo.getDescription()+“.txt”;
if(checkFileExistsInWin(文件名)){
后处理dump.setDumpnotescontent(getFileContentsFromWin(fileName));
字符串consolidateDumpnotes=getFileContentsFromWin(文件名);
字符串行[];
字符串内容=”;
行=合并的dumpnotes.split(\\r?\\n”);
List finallines=new ArrayList();
int k=0;
对于(int i=0;i
我得到以下错误:

如果要将类型为
PostProcessedDump
的实例添加到
列表中,则应更改其类型。另外,不要忘记初始化它。大概

List<PostProcessedDump> FinalDumpNotes = new ArrayList<>();

代码的问题:

List<DumpController> FinalDumpNotes;
因为您正试图将
后处理转储
对象添加到
列表
而不是
转储控制器
对象

对于初学者,您需要如下初始化列表:

List<DumpController> finalDumpNotes = new ArrayList<DumpController>();
List finalDumpNotes=new ArrayList();
请注意,我已将变量名以小写开头,这是惯例(大写通常保留给类和接口)


我将把它作为一个家庭作业留给你,来整理这个
列表的正确用法

,因为它们不是同一类型的!你的
FinalDumpNotes
DumpController
的集合,你的
dump
PostProcessedDump
。你真的拍了监视器的照片来提供屏幕截图吗?这让我有点振奋:)我用你的代码替换了我的代码,但仍然存在相同的错误。你的代码一团糟。这里给出的答案解释了您在IDE中看到的错误。
FinalDumpNotes.add(dump);
List<DumpController> finalDumpNotes = new ArrayList<DumpController>();