为什么在此对象上收到NullPointerException?

时间:2018-11-06 20:49:13

标签: java arrays nullpointerexception

我有一个类型为PolyaUrn的对象,名为myUrn。我实例化它并使用一些方法来操纵它。然后,我得到名为int[]的{​​{1}}的{​​{1}}字段。我将此myUrn传递给构造函数,以创建名为ballCounts的类型int[]的新对象。但是,当我尝试在SimpleUrn上调用方法时,会得到su。我正在使用su关键字来创建类型为NullPointerException的对象,并指向变量new。我不明白为什么我不能使用解引用运算符SimpleUrn

来引用su

以下代码:

su

.类:

import java.util.Arrays;

class PolyaUrnProg {

  public static void main(String[] args) {
    PolyaUrn myUrn = new PolyaUrn();
    myUrn.draw();
    myUrn.draw();
    System.out.println(myUrn);
    System.out.println();

    int numDraws = 100; // we will make another 98 draws from the urn

    for (int i = 0 ; i < numDraws-2 ; i++) {
      myUrn.draw();
    }
    System.out.println("...and after 98 more draws:\n");
    System.out.println(myUrn);

    System.out.println('\n');
    System.out.println("Let's construct a SimpleUrn from original PolyaUrn.");

    // initialising new int array with the same size as myUrn
    int[] a = new int[myUrn.getArray().length];

    // copying myUrn into 'a'
    a = Arrays.copyOf(myUrn.getArray(), myUrn.getArray().length);

    // passing 'a' to constructor in SimpleUrn to initialize it
    SimpleUrn su = new SimpleUrn(a);

    //ERROR - NullPointException
    su.toString();
  }
}

PolyaUrn类:

import java.util.Arrays;

class PolyaUrn {

  private int[] ballCounts;
  private int lastDraw;

  public PolyaUrn() { // Sets up PolyaUrn by rolling it

    ballCounts = new int[2]; // initially two possibilities
    ballCounts[0] = 1;
    ballCounts[1] = 1;

    // last draw is not a meaningful value until a ball is drawn.
    lastDraw = -1;
  }

  public int[] getArray() {
    return ballCounts;
  }

  public int getLastDraw() {
    return lastDraw;
  }

  public int[] updateUrn(int b) {

    if(b == 0) {

      int[] newUrn = Arrays.copyOf(ballCounts, ballCounts.length+1);
      newUrn[newUrn.length-1] = 1;
      ballCounts = newUrn;

    } else {

      ballCounts[b] = ballCounts[b]+1;
    }

    return ballCounts;
  }

  public void draw() {
    int ballDrawn = WeightedSampler.sample(ballCounts);
    updateUrn(ballDrawn);
    lastDraw = ballDrawn;
  }

  public String toString() {
    String s = "Polya Urn\n=========\n\n";
    s += "Ball-counts are as follows:\n\n";
    s += " Value\t| Count\n";
    s += "----------------\n";
    for (int ballValue = 0; ballValue < ballCounts.length ; ballValue++) {
      s += "  " + ballValue + "\t|  " + ballCounts[ballValue] + "\n";
    }
    return s;
  }
}

注意:这些类使用第三类SimpleUrn,以防有人要编译。此类在这里:

import java.util.Arrays;

class SimpleUrn {

  private int[] ballCounts;
  private int lastDraw;

  public SimpleUrn(int[] ballArray) { // Sets up PolyaUrn by rolling it

    int[] ballCounts = ballArray;

    // last draw is not a meaningful value until a ball is drawn.
    lastDraw = -1;
  }

  public int getLastDraw() {
    return lastDraw;
  }

  public int[] updateUrn(int b) {

    ballCounts[b] = ballCounts[b]-1;
    return ballCounts;
  }

  public void draw() {

    int ballDrawn = WeightedSampler.sample(ballCounts);
    updateUrn(ballDrawn);
    lastDraw = ballDrawn;
  }

  public String toString() {
    String s = "Polya Urn\n=========\n\n";
    s += "Ball-counts are as follows:\n\n";
    s += " Value\t| Count\n";
    s += "----------------\n";
    for (int ballValue = 0; ballValue < ballCounts.length ; ballValue++) {
      s += "  " + ballValue + "\t|  " + ballCounts[ballValue] + "\n";
    }
    return s;
  }
}

1 个答案:

答案 0 :(得分:4)

您在ballCounts构造函数中 shadingwing SimpleUrn。改变这个

public SimpleUrn(int[] ballArray) { // Sets up PolyaUrn by rolling it
    int[] ballCounts = ballArray;

    // last draw is not a meaningful value until a ball is drawn.
    lastDraw = -1;
}

收件人

public SimpleUrn(int[] ballArray) { // Sets up PolyaUrn by rolling it
    this.ballCounts = ballArray;

    // last draw is not a meaningful value until a ball is drawn.
    lastDraw = -1;
}