使用Weka在Android中将CSV转换为ARFF时出错

时间:2015-09-30 16:38:21

标签: java android csv weka

我正在唱weka网站https://weka.wikispaces.com/Converting+CSV+to+ARFF上的代码和说明
但由于某种原因,它给了我错误。请注意,我检查以确保文件存在且可读。


CSVLoader.java的代码
https://github.com/rjmarsan/Weka-for-Android/blob/925a77b566d6e55a775911fbf317234d11ec8024/src/weka/core/converters/CSVLoader.java


Android代码

                    String input = Environment.getExternalStorageDirectory().getAbsolutePath()+"/homework1/user_patterns.csv";
                    String output = Environment.getExternalStorageDirectory().getAbsolutePath()+"/homework1/user_patterns.arff";
                    Instances data = null;

                    try {
                        File inputFile = new File(input);
                        System.out.println(inputFile.exists());
                        System.out.println(inputFile.canRead());

                        // load CSV
                        CSVLoader loader = new CSVLoader();
                        loader.setSource(inputFile);
                        data = loader.getDataSet();

                        // save ARFF
                        ArffSaver saver = new ArffSaver();
                        saver.setInstances(data);
                        saver.setFile(new File(output));
                        saver.writeBatch();
                    } catch (IOException err) {
                        System.out.println(err);
                    }

堆栈跟踪

09-30 15:23:03.810  22869-22869/com.weka W/dalvikvm﹕ Exception Ljava/lang/NullPointerException; thrown while initializing Lweka/core/converters/ConverterUtils;
09-30 15:23:03.810  22869-22869/com.weka W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4d12b20)
09-30 15:23:03.814  22869-22869/com.weka E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.weka, PID: 22869
    java.lang.ExceptionInInitializerError
            at weka.core.converters.CSVLoader.readHeader(CSVLoader.java:882)
            at weka.core.converters.CSVLoader.readStructure(CSVLoader.java:534)
            at weka.core.converters.CSVLoader.getStructure(CSVLoader.java:519)
            at weka.core.converters.CSVLoader.getDataSet(CSVLoader.java:552)
            at com.weka.MainMenu$1.onClick(MainMenu.java:105)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at weka.core.converters.ConverterUtils.<clinit>(ConverterUtils.java:741)
            at weka.core.converters.CSVLoader.readHeader(CSVLoader.java:882)
            at weka.core.converters.CSVLoader.readStructure(CSVLoader.java:534)
            at weka.core.converters.CSVLoader.getStructure(CSVLoader.java:519)
            at weka.core.converters.CSVLoader.getDataSet(CSVLoader.java:552)
            at com.weka.MainMenu$1.onClick(MainMenu.java:105)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

请注意,MainMenu.java:105引用data = loader.getDataSet();

2 个答案:

答案 0 :(得分:0)

尝试将代码更改为

// load CSV
    CSVLoader loader = new CSVLoader();
    if(null != inputFile){
        loader.setSource(inputFile);
        data = loader.getDataSet();
    }else{
        Log.e("ERROR","error the input file is null");
    }

尝试并告诉我你是否在日志中遇到错误并且没有崩溃

答案 1 :(得分:0)

The source code you linked only contains a comment in weka.core.converters.ConverterUtils.java line 741, so I'm guessing that what you are actually using is the jar from here: https://www.pervasive.jku.at/Teaching/lvaInfo.php?key=346&do=uebungen

The problem there is that in the static class initializer, almost everything is commented out, leaving only line 741 that will fail because the static field m_FileLoaders is still null when m_FileLoaders.size() is called:

/** all available loaders (extension &lt;-&gt; classname). */
protected static Hashtable<String,String> m_FileLoaders;
[...]

// determine all loaders/savers
static {
  Vector classnames;

  try {
    // generate properties 
    // Note: does NOT work with RMI, hence m_FileLoadersCore/m_FileSaversCore
    /* GenericPropertiesCreator creator = new GenericPropertiesCreator();
       creator.execute(false);
       Properties props = creator.getOutputProperties();
       // init
       m_FileLoaders    = new Hashtable<String,String>();
       [...]
    */
    }
    catch (Exception e) {
      // ignore
    }
    finally {
      // loaders
      if (m_FileLoaders.size() == 0) {
      /*classnames = GenericObjectEditor.getClassnames(AbstractFileLoader.class.getName());
      [...]
      */
      }
    }
  }

All in all, my best guess is that the jar is broken, so either you fix it yourself or get a non-broken one.

(Btw: Another question with the same problem.)