user3406408
user3406408

Reputation: 11

error while converting csv to arff using weka jar

i want to convert an csv file to arff file and i am using weka jar to do the operations.however its throwing me an nullpointerexception. Here is the code

    /*
     * 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);

            }

        }

here is the stack trace it throws me an aerror while saving the arff back... help me where i am going wrong? It reads the data from the CSV as i am able to display that data

    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)

Upvotes: 0

Views: 748

Answers (1)

yogesh
yogesh

Reputation: 463

Add one line to your saver object.

saver.setDestination(new File("F:\\abc.arff"));

It will help you to create file.

Upvotes: 1

Related Questions