0

I am trying to test some data mining algorithms from smile project (https://github.com/haifengl/smile). The testing process is simple (I have included into existing Eclipse project Maven repositories of Smile project), but with the following code I catch a NPE (Null pointer exception) with InputStream , the file is just heavy csv file necessary to be read (included in the same project folder)

package com.algorithms;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;

import smile.data.AttributeDataset;
import smile.data.NominalAttribute;
import smile.data.parser.DelimitedTextParser;

public class DenclueTester {
    public void doTestDenclue() throws IOException, ParseException
    {
        DelimitedTextParser parser = new DelimitedTextParser();
        parser.setResponseIndex(new NominalAttribute("class"), 0);
        InputStream in =  this.getClass().getResourceAsStream("USCensus1990_data1.csv");
        AttributeDataset data = parser.parse("US Census data", in);
        double[][] x = data.toArray(new double[data.size()][]);
        int[] y = data.toArray(new int[data.size()]);

    }
    public DenclueTester() {}   //constructor
}

The following code is executed in main :

public class Dtest
{
    public static void main(String[] args) throws IOException, ParseException 
    {
        DenclueTester dt = new DenclueTester();
        dt.doTestDenclue();
    }
}

Stack trace:

Exception in thread "main" java.lang.NullPointerException
    at java.io.Reader.<init>(Unknown Source)
    at java.io.InputStreamReader.<init>(Unknown Source)
    at smile.data.parser.DelimitedTextParser.parse(DelimitedTextParser.java:234)
    at com.algorithms.DenclueTester.doTestDenclue(DenclueTester.java:18)
    at com.algorithms.Dtest.main(Dtest.java:26)

Could anyone help me with that?

rusty_cool
  • 27
  • 4
  • Probably can't locate the resource so try giving it the absolute path to the file's location like "/documents/resources/uscensus1990_data1.csv" or wherever it is located. Should also surround with try/catch clause. – smoggers Jun 09 '15 at 10:04
  • 2
    Possible duplicate http://stackoverflow.com/questions/16570523/getresourceasstream-returns-null – dhke Jun 09 '15 at 10:04
  • I have already tried it, even including absolute path to the file doestn't help – rusty_cool Jun 09 '15 at 10:29
  • What is the package of your class? Where is the csv file located in your Maven project? Your code expects it to find it in the same package as the class. And since it's a Maven project, it must be stored under src/main/resources/the/package. Also, post the exception stack trace. – JB Nizet Jun 09 '15 at 10:43
  • @JBNizet package name is com.algorithms. I create an object of this class in main program and just call this method. Here is the stack trace: Exception in thread "main" java.lang.NullPointerException at java.io.Reader.(Unknown Source) at java.io.InputStreamReader.(Unknown Source) at smile.data.parser.DelimitedTextParser.parse(DelimitedTextParser.java:234) at com.algorithms.DenclueTester.doTestDenclue(DenclueTester.java:18) at com.algorithms.Dtest.main(Dtest.java:26) – rusty_cool Jun 09 '15 at 10:48
  • Edit the question. And answer all my questions. – JB Nizet Jun 09 '15 at 10:49
  • Don't do that. The classes folder will be deleted as soon as you clean your project. Read my previous comment. – JB Nizet Jun 09 '15 at 11:39

1 Answers1

1

Solved issue by placing the csv file into /classes/package_name folder. Thanks

rusty_cool
  • 27
  • 4