简体   繁体   中英

Java, Objects, Constructors: Getting a NullPointerException error

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

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.

eg,

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM