如何使用ArrayList和循环修复NullPointerException

时间:2019-07-11 12:01:22

标签: java nullpointerexception

我编译了文件,没有任何问题,但是当我尝试运行文件时,出现了以下3个类的NullPointerException

我尝试重做所有循环并检查是否存在间隙。 首先:

  public int findRoot(Complex root) {
  for(int i=0 ; i < roots.size() ; i++){
  roots.add(root);
    if(root.add(roots.get(i).minus()).abs() <= Newton.TOL){
      return i;
    }
    if(root.add(roots.get(i).minus()).abs() >= Newton.TOL){
      return -1;
    }
    else{
      return roots.size();
    }
    }
  return roots.size();
}}

第二:

public void createFractal(boolean colorIterations) {
  this.colorIterations = colorIterations;
  //colorIteration instance in  colorPixel method
    for(int i = 0; i<NUMPIXELS ; i++){
    for(int j = 0 ;j<NUMPIXELS ; j++){
      iterator.iterate(pixelToComplex(i,j));
      int rootColor = findRoot(iterator.getRoot());
      int numIter = iterator.getNumIterations();
      colorPixel(i, j, rootColor, numIter);
    }
  }
}

第三:

public static void main(String[] args) {
    // Here is some example code which generates the two images seen in
    // figure 1 of the formulation.
    Complex[] coeff = new Complex[] { new Complex(-1.0,0.0), new Complex(),
                                      new Complex(), new Complex(1.0,0.0) };
    Polynomial p    = new Polynomial(coeff);
    NewtonFractal f = new NewtonFractal(p, new Complex(-1.0,1.0), 2.0);

    f.createFractal(false);
    f.saveFractal("fractal-light.png");
    f.createFractal(true);
    f.saveFractal("fractal-dark.png");
}

0 个答案:

没有答案