1

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)
user3577397
  • 453
  • 3
  • 12
  • 27
  • 1
    I don't understand that to be honest. I followed the guide I linked above. I just want to convert this file to arff. – user3577397 Feb 26 '15 at 20:06
  • From which directory do you execute this program? When you execute `ls Users/myname/Desktop/bball.csv` from that directory, what is printed? – JB Nizet Feb 26 '15 at 20:08
  • does Users/myname/Desktop/bball.csv exist? it is a relative path.. – Victor Feb 26 '15 at 20:08
  • Clearly whatever you linked to is throwing a null pointer exception. In their `getStructure()` method, something is acessing a `StreamTokenizer` that is null. Your program is failing because it is calling this method. – NoseKnowsAll Feb 26 '15 at 20:09
  • I replaced my actual real name, with "myname" for privacy purposes. But when I click on the file on my Mac's desktop, that seems to be the path. – user3577397 Feb 26 '15 at 20:09
  • That doesn't answer my question. When you execute `ls Users/myname/Desktop/bball.csv` (with myname replaced by your real name), what is printed? Absolute paths start with a `/`. Yours is a *relative* path. – JB Nizet Feb 26 '15 at 20:10
  • 1
    @user3577397 - Open up Terminal. Go to the folder that your program is written in (cd/ls are the only 2 commands you'll need). Then type `ls Users/myname/Desktop/bball.csv`. What does it print? – NoseKnowsAll Feb 26 '15 at 20:11
  • Nothing is printed. I wanted it to save an arff file to my desktop, but nothing happens when I execute this program in Java. Java just prints out the error I linked above. – user3577397 Feb 26 '15 at 20:12
  • Oh thanks Nose, I didn't know that's what he meant. – user3577397 Feb 26 '15 at 20:13
  • READ what I ask. I'm not asking you to execute your program. I'm asking you to execute `ls Users/myname/Desktop/bball.csv`. – JB Nizet Feb 26 '15 at 20:13
  • Oh... it says no such file or directory :/ – user3577397 Feb 26 '15 at 20:13
  • So, what do you deduce? – JB Nizet Feb 26 '15 at 20:14
  • @JBNizet -- I honestly didn't understand you, I'm sorry. I did read what you wrote, I genuinely didn't understand. I apologize – user3577397 Feb 26 '15 at 20:15
  • It's not the correct directory – user3577397 Feb 26 '15 at 20:15
  • The file is not there.. – Victor Feb 26 '15 at 20:16
  • No file -> no data -> no loader.getDataSet() – Victor Feb 26 '15 at 20:16
  • Exactly. So change the string file location to the actual file location: relative to your program's directory, of course. Or just move your file into the same directory and ignore everything else. – NoseKnowsAll Feb 26 '15 at 20:16
  • Or use absolut paths – Victor Feb 26 '15 at 20:17
  • @user3577397 no need to apologize. I understand that. I apologize for being too harsh, sometimes. – JB Nizet Feb 26 '15 at 20:18
  • Consider the functions I posted on my answer when manipulating files. – Victor Feb 26 '15 at 20:18
  • Actually, I believe it is the right directory, I just didn't put a forward slash when I tested the ls. I just put "ls /Users/myname/Desktop/bball.csv" and it printed that on the next line – user3577397 Feb 26 '15 at 20:21
  • 1
    this / turns into a absolute path, like a said before. it is a big difference. – Victor Feb 26 '15 at 20:22
  • put on you java code too – Victor Feb 26 '15 at 20:23
  • Indeed - the absolute path gave different results. I'm getting a different error now tho. "---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)" I can post that error above – user3577397 Feb 26 '15 at 20:24
  • If you're in the directory `/foo/bar`, and use the relative path `Users/myname/baz.csv`, the absolute path of the file is `/foo/bar/Users/myname/baz.csv`, because a path which doesn't start with a `/`is a path that is **relative** to the current directory. If you use the absolute path `/Users/myname/baz.csv`, then this path will point to the file `/Users/myname/baz.csv`, whatever the directory you're in, because it's an **absolute** path.. – JB Nizet Feb 26 '15 at 20:25
  • @user3577397 that is a whole different problem now. The fact was that before your program was not using the correct path, and not being able to continue. now you have other problem, which is on the program itself. I think you could investigate that and create other question if you dont find a solution – Victor Feb 26 '15 at 20:29
  • ps: jdbc.idbDriver is installed? – Victor Feb 26 '15 at 20:30
  • 1
    and please, accept the answer so people can see that this question was solved and that was the solution in case more people have the same problem. – Victor Feb 26 '15 at 20:32
  • @victor - Thanks. I will accept the answer, and probably create a new question later on. I'm not really sure what that is (jdbc.idbDriver). Is there something wrong with my CSV file? The way I wrote it? I just wrote it in a standard text file and changed the extension to csv. – user3577397 Feb 26 '15 at 20:35
  • 1
    CVS are basically text files well formatted..it does not seems to be wrong. jdbc.idbDriver is a class your program is trying to load but not finding it. The classes you are using probably depend on it to do their job. Try checking the library manual for this. This problem is usually simple to solve, normally you find the class, download it and copy/past to a place your JVM can find..the library installation manual should tell you this information. – Victor Feb 26 '15 at 20:40
  • hmm.. According to this http://stackoverflow.com/questions/15343145/trying-to-add-database-driver-jdbc-rmijdbc-rjdriver-error-not-in-classpath I might not need to access this database, so the guy recommended to comment it out? I might try that and see what happens. – user3577397 Feb 26 '15 at 20:43

1 Answers1

1

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

Victor
  • 3,520
  • 3
  • 38
  • 58