-2

For the code below, the classifyInstance() line gives an error:

Exception in thread "main" java.lang.NullPointerException
    at weka.classifiers.functions.LinearRegression.classifyInstance(LinearRegression.java:272)
    at LR.main(LR.java:45)

I tried to debug but no success. How can I use my saved model to predict the class attribute of my test file? The problem is based on the regression.

for (int i = 0; i < unlabeled.numInstances(); i++) {
    double clsLabel = cls.classifyInstance(unlabeled.instance(i));
    labeled.instance(i).setClassValue(clsLabel);
    System.out.println(clsLabel + " -> " + unlabeled.classAttribute().value((int) clsLabel));
}

This is the actual code:

public class LR{
    public static void main(String[] args) throws Exception
    {
        BufferedReader datafile = new BufferedReader(new FileReader("C:\\dataset.arff"));
        Instances data = new Instances(datafile);

        data.setClassIndex(data.numAttributes()-1);    //setting class attribute
        datafile.close();

        LinearRegression lr = new LinearRegression();  //build model
        int folds=10;

        Evaluation eval = new Evaluation(data);
        eval.crossValidateModel(lr, data, folds, new Random(1));
        System.out.println(eval.toSummaryString()); 

        //save the model
        weka.core.SerializationHelper.write("C:\\lr.model", lr);

        //load the model
        Classifier cls = (Classifier)weka.core.SerializationHelper.read("C:\\lr.model");


        Instances unlabeled = new Instances(new BufferedReader(new FileReader("C:\\testfile.arff")));
        // set class attribute
        unlabeled.setClassIndex(unlabeled.numAttributes() - 1);
        // create copy
        Instances labeled = new Instances(unlabeled);
        double clsLabel;
        // label instances
        for (int i = 0; i < unlabeled.numInstances(); i++) 
        {
            clsLabel = cls.classifyInstance(unlabeled.instance(i));
            labeled.instance(i).setClassValue(clsLabel);
            System.out.println(clsLabel + " -> " + unlabeled.classAttribute().value((int) clsLabel));
        }
        // save labeled data
        BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\final.arff"));
        writer.write(labeled.toString());
        writer.newLine();
        writer.flush();
        writer.close();
    }
}   
Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
user5538704
  • 91
  • 1
  • 12
  • 4
    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 Nov 10 '15 at 18:43
  • I already saw that post but I am not able to find what could be the possible null values here – user5538704 Nov 10 '15 at 18:45
  • You passed `null` to `cls.classifyInstance()`. Check the value of `unlabeled.instance(i)` – Bogdan Kobylynskyi Nov 10 '15 at 18:48
  • Well, you could start by looking at line 272. We can't help you because you didn't tell us. – Seelenvirtuose Nov 10 '15 at 18:49
  • There is no line number 272 in my code! – user5538704 Nov 10 '15 at 18:50
  • 1
    Oh, well! I though the first mentioned class in the stacktrace was yours. Sorry. Your class seems to be `LR`. And the error occurs in line 45 of this class. Look there! – Seelenvirtuose Nov 10 '15 at 18:53
  • line 45 is the classifyInstance() line. I am not able to understand how is null value being passed in there? and how can I use my saved model for prediction? – user5538704 Nov 10 '15 at 18:57
  • Did you check to ensure that `cls` is non-null? – FredK Nov 10 '15 at 19:21
  • Take a look at [this](http://grepcode.com/file/repo1.maven.org/maven2/nz.ac.waikato.cms.weka/weka-dev/3.7.5/weka/classifiers/functions/LinearRegression.java#LinearRegression.classifyInstance%28weka.core.Instance%29), line 272. Probably when you are creating your `cls` from your file there's something wrong and `m_checksTurnedOff` gets value null. Maybe it would be good that you post your `lr.model`. – lrnzcig Nov 10 '15 at 20:28
  • 1
    @lrnzcig m_checksTurnedOff **cannot** have the value `null` because it is a primitive `booelan`. Unfortunately, we have no clue on what version @user553874 used, he failed to include the most basic debugging information - version number, and code where the problem occurs. Or, for a null pointer exception: **which variable was null**... – Has QUIT--Anony-Mousse Nov 10 '15 at 22:10
  • I am new to Weka. so sorry for not providing all the information. the version of weka is 3.6 and that of netbeans is 8.0 the problem occurs in the classifyInstance() line. i am not able to identify how null value is being passed in there. i tried all methods but to no success. can you please code that part for me ? – user5538704 Nov 11 '15 at 05:06
  • @Anony-Mousse Of course you are right, sorry for my mistake. Your answer also seems to be right, just by looking at the code, although not accepted yet. – lrnzcig Nov 11 '15 at 07:16

1 Answers1

1

Did you train your classifier?

Looks to me like you are trying to classify, without having trained your classifier.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
  • +1 on this, if you are doing evaluation only there is no need to train the classifier as the `crossValidateModel` does the training, however, you are saving and loading a classifier. The classifier must be trained with the `buildClassifier` method prior to saving. – SJB Nov 11 '15 at 02:09
  • why should the classifier be trained before saving it? doesn't the crossValidateMethod already trains it? I am new to Weka so if you could explain me in layman's language and help me with the code? – user5538704 Nov 11 '15 at 05:09
  • crossValidate *simulates* training 10 classifiers, tests them, and discards them. It's an evaluation method. – Has QUIT--Anony-Mousse Nov 11 '15 at 06:46