Weka in Java "Class index is negative" error
<p>I am trying weka library in java. I connect sql server and pull my data. And split test and train. I set class index for test and train. Here is my code's part;</p>

<pre><code>        Instances data = new Instances(datafile);
        data.setClassIndex(data.numAttributes() - 1);

        int trainSize = (int) Math.round(data.numInstances() * 0.8);
        int testSize = data.numInstances() - trainSize;
        Instances train = new Instances(data, 0, trainSize);
        Instances test = new Instances(data, trainSize, testSize);

        train.setClassIndex(train.numAttributes() - 1);
        test.setClassIndex(test.numAttributes() - 1);

        Classifier[] models = 
        { 
           new J48(), 
           new PART(), 
           new DecisionTable(),
           new DecisionStump(),
           new NaiveBayes(),
           new IBk()
        };

    // Run for each model
       for (int j = 0; j &lt; models.length; j++) 
       {
            FastVector predictions = new FastVector();

            Evaluation validation = classify(models[j], train, test);
            predictions.appendElements(validation.predictions());

        }
}
</code></pre>

<p>And i got error on this line;</p>

<pre><code>Evaluation validation = classify(models[j], train, test);,
</code></pre>

<p>I search this error in internet, users says write code like this;</p>

<pre><code>train.setClassIndex(train.numAttributes() - 1);
test.setClassIndex(test.numAttributes() - 1);
</code></pre>

<p>But i got same error again. Full error like this;</p>

<pre><code>Exception in thread "main" weka.core.UnassignedClassException: Class index is negative (not set)!
    at weka.core.Instances.numClasses(Instances.java:915)
    at weka.classifiers.evaluation.Evaluation.&lt;init&gt;(Evaluation.java:461)
    at weka.classifiers.evaluation.Evaluation.&lt;init&gt;(Evaluation.java:441)
    at weka.classifiers.Evaluation.&lt;init&gt;(Evaluation.java:211)
    at weka1.Weka1.classify(Weka1.java:84)
    at weka1.Weka1.main(Weka1.java:193)
</code></pre>