我想將csv文件轉換爲arff文件,並且我正在使用weka jar來執行操作,但它向我投擲nullpointerexception。下面是代碼使用weka jar將csv轉換爲arff時出錯
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Model;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.core.converters.CSVLoader;
/**
*
* @author sanketh
*/
public class testClass {
public void createArff(String Filename) {
try {
String path = Filename.substring(0, Filename.length() - 3);
System.out.println("new path:" + path);
String[] args = new String[2];
args[0] = path + "csv";
args[1] = path + "arff";
// load CSV
CSVLoader loader = new CSVLoader();
loader.setSource(new File(args[0]));
Instances data = loader.getDataSet();
System.out.println(data);
// save ARFF
ArffSaver saver = new ArffSaver();
saver.setInstances(data);
//saver.setFile(new File(args[1]));
saver.setFile(new File("h:\\abc.arff"));
saver.writeBatch();
} catch (IOException ex) {
//System.out.println("Please try with some other test data!");
// attribMap.resultLog="Please try with some other test data!";
Logger.getLogger(GenerateFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) throws Exception {
String nepath="H:\\file.csv";
testClass t1=new testClass();
t1.createArff(nepath);
}
}
這裏是堆棧跟蹤 它拋出我一個AERROR同時節省了ARFF回來... 幫助我在哪裏,我錯了? 因爲我能夠顯示數據
Exception in thread "main" java.lang.NullPointerException
at java.io.Writer.<init>(Unknown Source)
at java.io.PrintWriter.<init>(Unknown Source)
at java.io.PrintWriter.<init>(Unknown Source)
at weka.core.converters.ArffSaver.writeBatch(ArffSaver.java:187)
at Model.testClass.createArff(testClass.java:39)
at Model.testClass.main(testClass.java:65)
哪一行是39?另外,如果可以的話,我會逐步進入調試模式並查看這些值。看起來像第39行的值爲空。 –
@ChrisBolton第39行hea引用「saver.writeBatch();」同時是一個內置的weka jar方法 – user3406408