处理 - 将变量作为参数传递 - 获取NullPointerException

时间:2015-03-09 15:15:07

标签: nullpointerexception processing

我用构造函数编写了代码,该代码将变量传递给Lsystem。

这应该允许我创建许多“树”,每次都搞乱参数。

但是,我收到了一行

的NullPointerException
for (int i=0; i < m_state.length(); i++) {

我知道我需要使用它。在某个地方(尽我所能阅读),但我对如何使我的代码工作感到困惑。

如果有人可以告诉我正确的代码应该是什么样子,那我就非常感激。

提前谢谢。

class Tree {

  // member variables
  int    m_lineLength;       // turtle line length
  int    m_x;                // initial x position
  int    m_y;                // initial y position
  float  m_branchAngle;      // turtle rotation at branch
  float  m_initOrientation;  // initial orientation
  String m_state;            // initial state
  float  m_scaleFactor;      // branch scale factor
  String m_F_rule;           // F-rule substitution
  String m_H_rule;           // H-rule substitution
  String m_f_rule;           // f-rule substitution
  int    m_numIterations;    // number of times to substitute

  // constructor
  // (d = line length, x & y = start position of drawing)
  Tree(int d, int x, int y, float m_branchAngle, float m_initOrientation, String m_state, float m_scaleFactor, String m_F_rule, String m_H_rule, String m_f_rule, int m_numIterations) {
    m_lineLength = d;
    m_x = x;
    m_y = y; 
    m_branchAngle = (25.7/180.0)*PI;
    m_initOrientation = -HALF_PI;
    m_scaleFactor = 1;
    m_state = "F";
    m_F_rule = "F[+F]F[-F]F";
    m_H_rule = "";
    m_f_rule = "";
    m_numIterations = 5;

    // Perform L rounds of substitutions on the initial state
    for (int k=0; k < m_numIterations; k++) {
      m_state = substitute(m_state);
    }
  }

  void draw() {
    pushMatrix();
    pushStyle();

    stroke(0);
    translate(m_x, m_y);        // initial position
    rotate(m_initOrientation);  // initial rotation

    // now walk along the state string, executing the
    // corresponding turtle command for each character
    for (int i=0; i < m_state.length(); i++) {
      turtle(m_state.charAt(i));
    }

    popStyle();
    popMatrix();
  }

  // Turtle command definitions for each character in our alphabet
  void turtle(char c) {
    switch(c) {
    case 'F': // drop through to next case
    case 'H':
      line(0, 0, m_lineLength, 0);
      translate(m_lineLength, 0);
      break;
    case 'f':
      translate(m_lineLength, 0);
      break;
    case 's':
      scale(m_scaleFactor);
      break;
    case '-':
      rotate(m_branchAngle);
      break;
    case '+':
      rotate(-m_branchAngle);
      break;
    case '[':
      pushMatrix();
      break;
    case ']':
      popMatrix();
      break;
    default:
      println("Bad character: " + c);
      exit();
    }
  }

  // apply substitution rules to string s and return the resulting string
  String substitute(String s) {
    String newState = new String();
    for (int j=0; j < s.length(); j++) {
      switch (s.charAt(j)) {
      case 'F':
        newState += m_F_rule;
        break;
      case 'H':
        newState += m_F_rule;
        break;
      case 'f':
        newState += m_f_rule;
        break;
      default:
        newState += s.charAt(j);
      }
    }
    return newState;
  }

}

Tree tree;

void setup() {
  int SZ = 512;  // screen size

  int d = 2;
  int x = SZ/2;
  int y = SZ;

  float m_branchAngle = (25.7/180.0)*PI;
  float m_initOrientation = -HALF_PI; 
  String m_state = "F";
  float m_scaleFactor = 1;
  String m_F_rule = "F[+F]F[-F]F";
  String m_H_rule = ""; 
  String m_f_rule = "";
  int m_numIterations = 5;

  size(SZ,SZ);
  background(255);
  noLoop();

  tree = new Tree(d, x, y, m_branchAngle, m_initOrientation, m_state, m_scaleFactor, m_F_rule, m_H_rule, m_f_rule, m_numIterations);  
}

void draw() {
  tree.draw();
}

1 个答案:

答案 0 :(得分:2)

让我们看一下Tree构造函数的简化版本:

String m_state;

Tree(String m_state) {
    m_state = "F";
    for (int k=0; k < m_numIterations; k++) {
      m_state = substitute(m_state);
    }
}

请注意,您有两个名为m_state的变量:一个类级实例变量,以及构造函数中的一个参数。这意味着构造函数级变量隐藏类级变量。你在构造函数中使用m_state变量所做的任何事情都是指构造函数级变量,而不是类级变量。

然后你使用你从未实际初始化的类级变量。这就是造成NullPointerException的原因。

解决问题的一种方法是使用 this 关键字专门引用类级变量:

String m_state;

Tree(String m_state) {
    this.m_state = "F";
    for (int k=0; k < m_numIterations; k++) {
      this.m_state = substitute(this.m_state);
    }
}

但是,如果你没有使用构造函数级别的m_state变量,你为什么还要烦恼呢?所以你可以完全消除这个参数,如果你只是将它设置为等于&#34; F&#34;反正。