Errors when converting CSV to ARFF in Android using Weka

南笙酒味 提交于 2019-12-24 11:46:51

问题


I'm sing the code and instructions found on the weka site https://weka.wikispaces.com/Converting+CSV+to+ARFF
But for some reason its giving me errors. Notice I check to make sure the file exists and is readable.


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


Android Code

                    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);
                    }

Stack Trace

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)

Note that MainMenu.java:105 refers to data = loader.getDataSet();


回答1:


try changing your code to

// 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");
    }

try that and tell me if you get an error in the log cat and no Crash




回答2:


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.)



来源:https://stackoverflow.com/questions/32871404/errors-when-converting-csv-to-arff-in-android-using-weka

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!