0

Here's the error I'm getting:
I know it's telling me that a[0].setAttribute(0); is wrong, but I don't know why it's wrong. How should I be filling up the Instance array with values?

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)

This is the class I'm using to test my constructors.

package DecisionTree;

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

The instance class:

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
  • 1
    There is nothing (other than a null) in a[0], since you didn't put anything there. Commented Apr 4, 2014 at 1:18

3 Answers 3

3

You're creating an array of reference type, of objects, but are trying to use it before filling it with valid objects. When created it's filled with nothing but nulls. So fill it, and then use it.

e.g.,

Instance[] a = new Instance[5];
for (int i == 0; i < a.length; i++) {
   // first create your instance
   a[i] = new Instance(); // don't know if this is a valid constructor

   // and only *then* can you use it
   a[i].setAttribute(i);
   a[i].setLabel(i % 2 == 0);
}

Consider an array of references like an empty parking lot. You can't drive any cars in the lot until you first put cars into the lot. Similarly, you can't use any if the items in a reference array until you first initialize those items.

Sign up to request clarification or add additional context in comments.

2 Comments

That makes sense. And so can I do a[i] = new Instance(i, i%2==0);
@user3251142: yep, if Instance has that constructor, for sure I'd use it.
2

You have not yet put any objects in your array. Instance[] a = new Instance[5];

//creates an empty array of size 5, with space enough in each for one pbject of type instance. So you now have to put 5 Instances in there.

Instance a1 = new Instance();
a[0] = a1;
Instance a2 = new Instance();
..
Instance a5 = new Instance();

or,

for(int i =0; i <5; i++){
a[i] = new Instance;
}

or some variation of this.

Comments

1

When you declare an array of objects all elements inside it will be by default null. You need to create Instance instances and insert into it on the array before use the array elements

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.