Search code examples
javacsvnullpointerexceptionwekaarff

"Exception in thread "main" java.lang.NullPointerException" - CSV to Arff


I'm trying to convert a CSV file to Arff format following this guide. (link below)

This is for practice purposes, I just want to know how to convert this properly before building a bigger dataset.

CSV file

Name,points,rebounds,steals,assists
Lebron James,17,10,1,7
Lebron James,29,5,2,8
Lebron James,30,4,0,8
Lebron James,24,4,1,3
Lebron James,25,1,0,11
Lebron James,22,4,2,9
Lebron James,21,3,2,2
Lebron James,27,7,1,14
Lebron James,41,5,0,5
Lebron James,35,2,2,2
Lebron James,18,4,3,4

Java file - Working off this site tutorial Weka Turorial

import weka.core.Instances;
import weka.core.converters.CSVLoader;
import weka.core.converters.ArffSaver;

import java.io.File;

public class arffBBALL{
    public static void main(String[] args) throws Exception{



        //load CSV
        CSVLoader loader = new CSVLoader();
        loader.setSource(new File("Users/myname/Desktop/bball.csv"));
        Instances data = loader.getDataSet();


        //save ARFF
        ArffSaver saver = new ArffSaver();
        saver.setInstances(data); // set the dataset we want to convert
        // save as ARFF
        saver.setFile(new File("Users/myname/Desktop/nbball.arff"));
        saver.writeBatch();
    }
}

This is the error I'm getting in Java

Exception in thread "main" java.lang.NullPointerException
    at java.io.StreamTokenizer.<init>(StreamTokenizer.java:248)
    at weka.core.converters.CSVLoader.getStructure(CSVLoader.java:593)
    at weka.core.converters.CSVLoader.getDataSet(CSVLoader.java:628)
    at arffBBALL.main(arffBBALL.java:15)

I loaded the Weka.jar file into the referenced Library, but I can't seem to convert the CSV file to arff.

I have the CSV file on my Desktop on my Mac.

Any help would be appreciated. Thanks

EDIT: Latest errors after adding absolute path:

---Registering Weka Editors---
Trying to add database driver (JDBC): jdbc.idbDriver - Error, not in CLASSPATH?
Exception in thread "main" java.io.IOException: wrong number of values. Read 2, expected 1, read Token[EOL], line 4
    at weka.core.converters.ConverterUtils.errms(ConverterUtils.java:912)
    at weka.core.converters.CSVLoader.getInstance(CSVLoader.java:824)
    at weka.core.converters.CSVLoader.getDataSet(CSVLoader.java:646)
    at arffBBALL.main(arffBBALL.java:15)

Solution

  • test with this function to see if the problem is the file.. http://docs.oracle.com/javase/7/docs/api/java/io/File.html#exists() http://docs.oracle.com/javase/7/docs/api/java/io/File.html#canRead()

    It seems you have a file problem. Try checking the paths..