0

When I try using weka API to binning it always got NullPointerException unless writing in the Debug comment block. Can anyone explain this for me? I'm new in JAVA and already read the docs but still can't figure it out.

This is from my test pj:

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

        Scanner _input = new Scanner(System.in);
        String path = "";

        System.out.print("Input data path: ");
        path = _input.nextLine();

        /*Init data source*/
        DataSource source = new DataSource(path);
        Instances data = source.getDataSet();

        /*if (data.classIndex() == -1)
               data.setClassIndex(data.numAttributes() - 1);*/

        /*Debug section*/ //THIS SECTION WORKS FINE

        /*Discretize _dis = new Discretize();
        _dis.setInputFormat(data);
        _dis.setBins(10);
        data = Filter.useFilter(data, _dis);
        System.out.print(data.firstInstance());
        System.exit(2);*/


                    System.out.println("Select sub function: \n"
                                + "1. Equal-width\n"
                                + "2. Equal-depth\n");
                    System.out.print("Input selection: ");
                    int select = _input.nextInt();


                    Discretize _dis = new Discretize();
                    _dis.setInputFormat(data);
                    _dis.setAttributeIndices("first-last");


                    if (select == 1) {
                        _dis.setBins(_input.nextInt());
                    }
                    else {
                        _dis.setDesiredWeightOfInstancesPerInterval(_input.nextDouble());
                        _dis.setUseEqualFrequency(true);
                    }

                    data = Filter.useFilter(data, _dis); //NULL POINTER EXCEPTION HERE
                    System.out.print(data.firstInstance());

    }
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – thegauravmahawar Sep 29 '15 at 08:01

1 Answers1

0

A NullPointerException occurs whenever you are trying to access some object method or field but the object is null or has not been initialized. In your specific case, I think you are getting the NullPointerException in the line after that you commented. Simply your instruction:

data = Filter,useFilter(data, _dis);

is returning empty data (null) and the next line:

System.out.print(data.firstInstance());

is trying to print data's (or null) firstInstance and here is where the exceptions occur.

To fix this, just add a control before like this:

data = Filter.useFilter(data, _dis);
if(data!=null)System.out.print(data.firstInstance());
else System.out.println("Empty data.");
Aurasphere
  • 3,841
  • 12
  • 44
  • 71
  • Thanks for your concern. But I wonder why in the debug section it work fine but in main section it got NPE, I want that var data to work but don't know how to fix this – 加藤秀明 Sep 29 '15 at 14:56
  • Try comment out this line: _dis.setAttributeIndices("first-last"); Also what value do you have on input? Does it work with the same in the debug block? – Aurasphere Sep 29 '15 at 15:01
  • It work with the same. _dis.setAttributIndices is just a config of the filter so cmt out that line doesn't matter, I have already tried that but still won't work. – 加藤秀明 Sep 30 '15 at 12:08
  • I think that the problem is that your filter is too much restrictive and that's why you are getting null data back. So my suggestion would be to try comment out one by one every filter option so you can figure out which one (may even be more than one) is preventing you from getting any data back. – Aurasphere Sep 30 '15 at 12:14