我正在获取NullPointerException并查找错误并阅读有关该错误的信息,但似乎仍无法解决问题。我的主要方法采用文件类型和字符串的列表,并将其传递给Parse方法以将Excel文件转换为txt文件。我是Java的新手,将不胜感激。谢谢!
Exception in thread "main" java.lang.NullPointerException
at transform.Parse.Transform(Parse.java:54)
at transform.Input.main(Input.java:62)
这是我的代码。我在显示异常的地方添加了两条注释:
package transform;
public class Input {
private static final Object Face_value = null;
public static void main( String args[] ) throws Exception {
String str = " ";
Scanner sc = new Scanner(System.in);
ArrayList<String> ExcelList = new ArrayList<String>();
System.out.println("Print .xlsx file names");
while (sc.hasNextLine() && !(str = sc.nextLine()).equals("")) {
ExcelList.add(str);
}
for(int i=0; i<ExcelList.size(); i++) {
System.out.println(ExcelList.get(i));
}
String str1 = " ";
Scanner sc1 = new Scanner(System.in);
ArrayList<String> txtList = new ArrayList<String>();
System.out.println("Print desired .txt file names");
while(sc1.hasNextLine() && !(str1 = sc1.nextLine()).equals("")) {
txtList.add(str1);
}
if(txtList.size() != ExcelList.size()) {
System.out.println("The number of Excel files listed does not match the number of .txt files listed");
}
for(int i=0; i < txtList.size(); i++) {
System.out.println(txtList.get(i));
}
//convert ArrayList of type String to type File
for(int i=0; i < ExcelList.size(); i++) {
File file = new File(ExcelList.get(i));
Parse.Transform(file, txtList.get(i));
}
///////////////////////////////////////////////////////////////////
//ERROR ON THE LINE ABOVE(line 62) -- Parse.Transform(file, txtList.get(i));
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
System.out.println("\nDone");
}
}
package transform;
public class Parse{
public static void Transform (File ExcelFile, String TxtFile) {
Writer writer = null;
try {
//read excel file and get the first sheet
FileInputStream fis = new FileInputStream(ExcelFile);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
// set the name of the .txt file
File file = new File(TxtFile);
writer = new BufferedWriter(new FileWriter(file));
Iterator rows = sheet.rowIterator();
writer.write("@SecurityDeal\r\n");
while( rows.hasNext() ) {
XSSFRow row = (XSSFRow) rows.next();
if(row.getRowNum() == 0){
continue;
}
Iterator cells = row.cellIterator();
XSSFCell Face_Value = row.getCell(10);
XSSFCell Rate = row.getCell(8);
XSSFCell DealDate = row.getCell(6);
XSSFCell MaturityDate = row.getCell(7);
XSSFCell CReference = row.getCell(5);
XSSFCell Comments = row.getCell(4);
XSSFCell Counterparty = row.getCell(3);
//////////////////////ERROR ON BELOW LINE(Line 54) ///////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
String Cp = Counterparty.toString();
//convert date to correct format
Date s1 = MaturityDate.getDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat("M/dd/yyyy");
String MD = sdf.format(s1);
Date d1 = DealDate.getDateCellValue();
SimpleDateFormat sdf1 = new SimpleDateFormat("M/dd/yyyy");
String dd = sdf1.format(d1);
//convert cells to String
String Comments_String = Comments.toString();
String Face_Value_String = Face_Value.toString();
//convert strings to double
double Number_Comments = Double.parseDouble(Comments_String);
double Number_Face_Value = Double.parseDouble(Face_Value_String);
//get rid of exponential format
String Comments1 = String.format("%.0f", Number_Comments);
String Face_Value1 = String.format("%.0f", Number_Face_Value);
//Mapping for counterparty
if(Cp.equals("CITI")){
Cp = "US-Citibank, US";
}
else if(Cp.equals("BAML")){
Cp = "US-Bank Of America Merrill Lynch";
}
else if (Cp.equals("MIZUHO")) {
Cp = "US-Mizuho Ltd";
}
else if(Cp.equals("WELLS")) {
Cp = "US-Wells Fargo";
}
writer.write("$NEW\r\n" + "Instrument=US-Commercial Paper\r\n" + "BorrowInvest=BORROWING\r\n"
+ "Counterparty=" + Cp + "\r\n" + "Entity=US-GMF\r\n" + "Currency=USD\r\n" + "FaceValue=" + Face_Value1 + "\r\n"
+ "BaseRate=" + Rate + "\r\n" + "DealDate=" + dd + "\r\n" + "MaturityDate=" + MD + "\r\n" + "Dealer=GM_Financi\r\n" +
"CounterpartyReference=" + CReference + "\r\n" + "Comments=" + Comments1 + "\r\n$INSERT\r\n");
System.out.println("\r\n");
}
System.out.println("Successfully Created File");
} catch ( IOException ex ) {
ex.printStackTrace();
System.out.println("Check formatting of file names. Make sure they end in .txt or .xlsx");
} finally {
try
{if (writer != null) {writer.close();}}
catch (IOException e) {e.printStackTrace();}
}
}
}