2014-04-04 100 views
0

新的小问题:试图现在测试我的代码,但我得到一个错误。Java,对象,构造函数:该方法未定义类型

java.lang.NullPointerException 
    at DecisionTree.TestTree.main(TestTree.java:6) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272) 

TestTree:

package DecisionTree; 

public class TestTree { 
    public static void main(String[] args) { 
    Instance[] a = new Instance[5]; 
    a[0].attribute = 0; 
    a[1].attribute = 1; 
    a[2].attribute = 2; 
    a[3].attribute = 3; 
    a[4].attribute = 4; 
    a[0].label = true; 
    a[1].label = false; 
    a[2].label = true; 
    a[3].label = false; 
    a[4].label = true; 
    DecisionTree work = new DecisionTree(a); 
    System.out.println(work.root.cutoff); 
    } 
} 

我在给DTNode所有调用()我得到的错误。任何线索我要去哪里错了?我试图通过实例数组来创建一个决策树。下面是错误的:

File: DecisionTree\DecisionTree.java [line: 8] 
Error: The method DTNode(DecisionTree.Instance[], int, int) is undefined for the type DecisionTree.DecisionTree 
File: DecisionTree\DTNode.java [line: 45] 
Error: The method DTNode(DecisionTree.Instance[], int, int) is undefined for the type DecisionTree.DTNode 
File: DecisionTree\DTNode.java [line: 46] 
Error: The method DTNode(DecisionTree.Instance[], int, int) is undefined for the type DecisionTree.DTNode 

DecisionTree.java:

package DecisionTree; 

public class DecisionTree { 

    DTNode root; 

    public DecisionTree(Instance[] instance) { 
    root = DTNode.DTNode(instance, 0, instance.length); 

    } 

} 

DTNode.java:

package DecisionTree; 

public class DTNode { 

    Instance[] instances; 
    double cutoff; 
    DTNode left, right; 

    public DTNode (Instance[] instance, int l, int r) { 
     this.instances = instance; 
     int i; 
     int j = 0; 
     int k = 0; 
     int getIndex; 
     double[] cutoff = new double[instance.length/2]; 
     double[] entropy = new double[instance.length/2]; 
     int[] split = new int[instance.length/2+1]; 
     double smallestEntropy; 

     for(i=0; i<instance.length-1; i++) { 
     if(instance[i].label != instance[i+1].label) { 
      cutoff[j] = (instance[i].attribute + instance[i+1].attribute)/2; 
      split[j] = (2*i+1)/2; 
      j++; 
     } 
     } 

     if (j == 0) { 
     this.left = null; 
     this.right = null; 
     } 

     for(k=0; k<j; k++) { 
     entropy[k] = calcEntropy(instance, l, cutoff[k], r); 
     } 

     for(k=0; k<j; k++) { 
     if (entropy[k] < smallestEntropy) { 
      smallestEntropy = entropy[k]; 
      getIndex = k; 
     } 
     } 

     this.cutoff = cutoff[getIndex]; 
     this.left = DTNode(instance, l, split[getIndex]); 
     this.right = DTNode(instance, split[getIndex]+1, r); 
    } 

    public double calcEntropy(Instance[] inst, int a, double b, int c) { 
     int i; 
     double leftSideCounter = 0; 
     double rightSideCounter = 0; 
     double leftTrue = 0; 
     double rightTrue = 0; 
     double leftSideEntropy = 0; 
     double rightSideEntropy = 0; 
     double entropy; 

     for(i=a; i<=c; i++){ 
     if(inst[i].attribute < b) { 
      leftSideCounter++; 
     } 
     else { 
      rightSideCounter++; 
     } 
     } 

     for(i=0; i<leftSideCounter; i++) { 
     if(inst[i].label = true) { 
      leftTrue++; 
     } 
     } 

     for(i=0; i<rightSideCounter; i++) { 
     if(inst[i].label = true) { 
      rightTrue++; 
     } 
     } 

     leftSideEntropy = -leftTrue/leftSideCounter*Math.log(leftTrue/leftSideCounter)/Math.log(2)-(leftSideCounter-leftTrue)/leftSideCounter*Math.log((leftSideCounter-leftTrue)/leftSideCounter)/Math.log(2); 
     rightSideEntropy = -rightTrue/rightSideCounter*Math.log(rightTrue/rightSideCounter)/Math.log(2)-(rightSideCounter-rightTrue)/rightSideCounter*Math.log((rightSideCounter-rightTrue)/rightSideCounter)/Math.log(2); 
     entropy = leftSideEntropy*leftSideCounter/(leftSideCounter+rightSideCounter)+rightSideEntropy*rightSideCounter/(leftSideCounter+rightSideCounter);   
     return entropy; 
    } 


} 

Instance.java:

package DecisionTree; 

public class Instance { 

    double attribute; 
    boolean label; 

    public Instance(double a, boolean c) { 
attribute = a; 
label = c; 
    } 

    public double getAttribute() { 
return attribute; 
    } 

    public void setAttribute(double a) { 
attribute = a; 
    } 

    public boolean getLabel() { 
return label; 
    } 

    public void setLabel(boolean c) { 
label = c; 
    } 
} 

回答

1

要调用构造函数,使用new关键字。目前,你怎么称呼它有两种方式:

root = DTNode.DTNode(instance, 0, instance.length); 
this.left = DTNode(instance, l, split[getIndex]); 
// and again like this for right. 

目前这些被解释为具有以下签名的方法:

DTNode DTNode(Instance[], int, int); 
// Note: when you call it as DTNode.DTNode(...) then it would have to be a static method 

由于存在有这个签名没有一种方法,它可以让你看到的错误。相反,打电话给这样的构造:

root = new DTNode(instance, 0, instance.length); 
this.left = new DTNode(instance, l, split[getIndex]); 
// and again like this for right. 
+0

嘿,你认为你可以帮助我一个小的其他问题?我做了一个testTree类,我试图构建实例,然后树,然后打印出我的节点的一部分,以查看我的调用是否工作。生病添加到问题的顶部的类。我得到一个错误,生病添加太 – user3251142

+0

@ user3251142只是问一个新的问题 – clcto

2

你不能打电话一个构造函数就像你调用普通方法一样。您必须使用new操盘手:

root = new DTNode(instance, 0, instance.length); 
+0

嘿,你认为你可以帮我一个小的其他问题吗?我做了一个testTree类,我试图构建实例,然后树,然后打印出我的节点的一部分,以查看我的调用是否工作。生病添加到问题的顶部的类。我得到一个错误,生病添加 – user3251142