【问题标题】:Why am I getting a NullPointerException on this object?为什么我在这个对象上得到 NullPointerException?
【发布时间】:2018-11-06 20:49:12
【问题描述】:

我有一个PolyaUrn 类型的对象,名为myUrn。我实例化它并使用一些方法来操作它。然后,我得到了名为 ballCountsmyUrnint[] 字段。我将这个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;
  }
}

注意:这些类使用第三类,WeightedSampler,以防有人想要编译。这个课程在这里:

import java.util.Random;
import java.util.stream.*;

public class WeightedSampler {

  private static Random generator = new Random(); 

  public static int sample(int[] weights) {
    int total = calculateSum(weights);
    int remains = generator.nextInt(total);
    int index = 0;

    while (index < weights.length) {
      remains -= weights[index];
      if (remains < 0) {
        break;
      }
      index++;
    }
    return index;
  }

  public static int calculateSum(int[] arr){
    return IntStream.of(arr).sum();
  }
}

【问题讨论】:

  • 下次您询问有关异常的问题时,请务必准确发布异常的完整堆栈跟踪。它准确地告诉了异常发生的位置。
  • @JB。下次不会
  • @MadPhysicist 对不起,我没有包括在内。原始异常中的行号可能有点偏离(问题中的代码不包括 cmets 等)这里是原始错误。问题实际上是遮蔽Exception in thread "main" java.lang.NullPointerExceptionat tutorial4.polya_urn.SimpleUrn.toString(SimpleUrn.java:61)at tutorial4.polya_urn.PolyaUrnProg.main(PolyaUrnProg.java:43)

标签: java arrays nullpointerexception


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多