2014-04-04 2 views
0

새로운 작은 질문 : 지금 코드를 테스트하려고하지만 오류가 발생합니다.Java, Objects, 생성자 : 메서드가 형식에 대해 정의되지 않았습니다.

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 : DTNode 내 모든 전화에서

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); 
    } 
} 

() 나는 오류를 받고 있어요. 내가 잘못 가고있는 어떤 단서? 인스턴스 배열을 사용하여 의사 결정 트리를 만드는 재귀를 시도하고 있습니다. 여기에 오류가 있습니다 :

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 키워드를 사용하여 생성자를 호출하려면 : 당신은 대신 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

당신은 호출 할 수 없습니다 생성자 (constructor)는 일반적인 메서드라고 부릅니다.

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

이봐, 작은 다른 문제를 도와 줄 수 있니? testTree 클래스를 만들고 인스턴스를 만든 다음 트리를 만든 다음 노드의 일부를 인쇄하여 호출이 작동하는지 확인하려고합니다. 학급을 질문 상단에 추가하십시오. 나는 오류를 얻는다. 아픈 것도 덧붙인다. – user3251142

관련 문제