向reportGroup.getReportList()添加对象类时获取java空指针异常
我已将值分配给报表信息类的成员,并尝试添加到报表组类。正在获取java空指针异常。尽管我在一开始就已初始化报表信息 执行此操作时出现错误-reportGroup.getReportList().add(reportInfo) 我有两个java类向reportGroup.getReportList()添加对象类时获取java空指针异常,java,Java,我已将值分配给报表信息类的成员,并尝试添加到报表组类。正在获取java空指针异常。尽管我在一开始就已初始化报表信息 执行此操作时出现错误-reportGroup.getReportList().add(reportInfo) 我有两个java类 ReportGroup.java String GroupName; List<ReportInfo>reportList; public String getReportGroupName() { re
ReportGroup.java
String GroupName;
List<ReportInfo>reportList;
public String getReportGroupName() {
return ReportGroupName;
}
public void setReportGroupName(String reportGroupName) {
ReportGroupName = reportGroupName;
}
public List<ReportInfo> getReportList() {
return reportList;
}
public void setReportList(List<ReportInfo> reportList) {
this.reportList = reportList;
}
}
ReportInfo.java
String ReportName;
String ReportId;
String ReportPath;
//Getter and Setter
ReportGroup.java
字符串组名;
列表报告列表;
公共字符串getReportGroupName(){
返回ReportGroupName;
}
public void setReportGroupName(字符串reportGroupName){
ReportGroupName=ReportGroupName;
}
公共列表getReportList(){
返回报告列表;
}
公共无效集合报告列表(列表报告列表){
this.reportList=reportList;
}
}
ReportInfo.java
字符串报告名;
字符串ReportId;
字符串报告路径;
//接二连三
主要类别:
public class test {
List<ReportInfo> reportInfo = new ArrayList<ReportInfo>();
public static void main(String[] args) throws IOException {
String orgId="346";
String orgName="Ralph Lauren Corporation";
String reportPath= "/"+orgId +"_"+orgName.replaceAll(" ","_");
String reportFolderPath = "C:/Lokesh/Dev Tools/apache-tomcat-6.0.37/webapps/ROOT/Reports";
List<String> filesArray = null;
filesArray = new ArrayList<String>();
String filetowrite="C:/Users/lbandhu/Desktop/test.html";
FileWriter fw=new FileWriter(filetowrite);
ReportGroup reportGroup=new ReportGroup();
reportGroup.setReportGroupName(orgName); //Organization Name such as Ralph Lauren Corporation
fw.write("<html><head><title>"+ reportGroup.ReportGroupName+"</title><h1>Utilization Report</h1><span>Please contact your account manager for a listing of utilization report definitions or with any questions on your organization’s reports. </span><br></head><body><table>");
//fw.write("<br><h>"+orgName+"</h><br>");
File folder = new File(reportFolderPath+reportPath);
List<String> reportNames = listFolder(folder);
for (String rname : reportNames) {
ReportInfo reportInfo=new ReportInfo();
reportInfo.setReportName(rname);
folder = new File(reportFolderPath+reportPath+"/"+reportInfo.ReportName);
System.out.println(folder);
fw.write("<br><tr> "+reportInfo.ReportName +" Utilization Reports</tr><br>"); //Report Name such as Monthly,Quarterly and Yearly
Iterator<String> iterator=listFilesForFolder(folder, filesArray).iterator();
writeReportPath( fw,iterator, reportFolderPath,reportInfo);
fw.write("<br>");
reportGroup.getReportList().add(reportInfo);
}
filesArray.clear();
fw.close();
}
公共类测试{
List reportInfo=new ArrayList();
公共静态void main(字符串[]args)引发IOException{
字符串orgId=“346”;
字符串orgName=“拉尔夫劳伦公司”;
字符串reportPath=“/”+orgId+“”+orgName.replaceAll(“,”);
String reportFolderPath=“C:/Lokesh/Dev Tools/apache-tomcat-6.0.37/webapps/ROOT/Reports”;
List filesArray=null;
filesArray=newarraylist();
字符串filetowrite=“C:/Users/lbandhu/Desktop/test.html”;
FileWriter fw=新的FileWriter(filetowrite);
ReportGroup ReportGroup=新的ReportGroup();
reportGroup.setReportGroupName(orgName);//组织名称,如拉尔夫·劳伦公司
fw.write(“+reportGroup.ReportGroupName+”利用率报告请与您的客户经理联系,以获取利用率报告定义列表或对您组织的报告有任何疑问。
”;
//fw.写(“
”+orgName+“
”);
文件夹=新文件(reportFolderPath+reportPath);
List reportNames=listFolder(文件夹);
for(字符串rname:reportNames){
ReportInfo ReportInfo=新的ReportInfo();
reportInfo.setReportName(rname);
文件夹=新文件(reportFolderPath+reportPath+“/”+reportInfo.ReportName);
System.out.println(文件夹);
fw.write(“
”+reportInfo.ReportName+“利用率报告
”);//报告名称,如月度、季度和年度
Iterator Iterator=listFilesForFolder(文件夹,filesArray).Iterator();
WriterReportPath(fw、迭代器、reportFolderPath、reportInfo);
fw.写(“
”);
reportGroup.getReportList().add(reportInfo);
}
filesArray.clear();
fw.close();
}
您需要在构造函数中初始化列表:
public ReportGroup(){
reportList = new ArrayList<ReportInfo>();
}
class ReportGroup {
final List<ReportInfo> reportInfo;
ReportGroup () {
reportInfo = new ArrayList<ReportInfo>;
}
}
公共报告组(){
reportList=新的ArrayList();
}
或
公共报告组(字符串组名){
this.GroupName=GroupName;
reportList=新的ArrayList();
}
您需要在构造函数中初始化列表:
public ReportGroup(){
reportList = new ArrayList<ReportInfo>();
}
class ReportGroup {
final List<ReportInfo> reportInfo;
ReportGroup () {
reportInfo = new ArrayList<ReportInfo>;
}
}
公共报告组(){
reportList=新的ArrayList();
}
或
公共报告组(字符串组名){
this.GroupName=GroupName;
reportList=新的ArrayList();
}
您正在初始化
测试
类实例的reportInfo
字段,但不是报告组
的reportInfo
字段。它看起来不像您实际实例化了测试
类,并且该字段对静态(即类)不可见方法。即使它是可见的(例如,如果它本身是静态的),您也会使用局部变量reportInfo
对其进行阴影处理
当您在for
循环的第一行执行ReportInfo ReportInfo=new ReportInfo()
时,您正在创建一个新的ReportGroup
,其中包含一个未初始化的新ReportInfo
成员
您的ReportGroup
应该如下所示:
class ReportGroup {
final List<ReportInfo> reportInfo = new ArrayList<>();
}
类报告组{
最终列表reportInfo=新的ArrayList();
}
或者,您可以在构造函数中进行初始化:
public ReportGroup(){
reportList = new ArrayList<ReportInfo>();
}
class ReportGroup {
final List<ReportInfo> reportInfo;
ReportGroup () {
reportInfo = new ArrayList<ReportInfo>;
}
}
类报告组{
最终清单报告信息;
报告组(){
reportInfo=新的ArrayList;
}
}
您正在初始化测试
类实例的reportInfo
字段,但不是报告组
的reportInfo
字段。它看起来不像您实际实例化了测试
类,并且该字段对静态(即类)不可见方法。即使它是可见的(例如,如果它本身是静态的),您也会使用局部变量reportInfo
对其进行阴影处理
当您在for
循环的第一行执行ReportInfo ReportInfo=new ReportInfo()
时,您正在创建一个新的ReportGroup
,其中包含一个未初始化的新ReportInfo
成员
您的ReportGroup
应该如下所示:
class ReportGroup {
final List<ReportInfo> reportInfo = new ArrayList<>();
}
类报告组{
最终列表reportInfo=新的ArrayList();
}
或者,您可以在构造函数中进行初始化:
public ReportGroup(){
reportList = new ArrayList<ReportInfo>();
}
class ReportGroup {
final List<ReportInfo> reportInfo;
ReportGroup () {
reportInfo = new ArrayList<ReportInfo>;
}
}
类报告组{
最终清单报告信息;
报告组(){
reportInfo=新的ArrayList;
}
}
reportList接缝未初始化为'ListreportList=new ArrayList();'它在最顶端初始化!!我看不到!您正在主方法开始之前初始化ReportGroupRight。List reportInfo=new ArrayList();reportInfo和not reportListreportList接缝未初始化为'ListreportList=new ArrayList()“它是在最顶端初始化的!!我看不到!您正在主方法启动之前初始化ReportGroups。List reportInfo=new ArrayList();reportInfo和not reportlist与上面的不同,可以使用我在最顶层已经使用的任何一种方式进行初始化。在当前代码中,将“List reportInfo;”替换为“List reportInfo=new ArrayList();”,然后再次测试please@Thanks里亚德,