【问题标题】:Processing nullPointerException处理 nullPointerException
【发布时间】:2016-03-14 11:06:41
【问题描述】:

我在if (stringReceive.contains(tabStock[j])){ 行有一个错误nullPointerException,但我不明白为什么数组tabstock 的索引j 有错误。

import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myRemoteLocation;

String[] tabStock = new String[6];
String[] tabReceive = new String[6];
String stringReceive;
String oldStringReceive;
int cptEssai = 0;
int cpt = 0;

void setup() {
size(400,400);
frameRate(25);
/* start oscP5, listening for incoming messages at port 12000 */
oscP5 = new OscP5(this,12000);

myRemoteLocation = new NetAddress("127.0.0.1",12001);

tabStock[0] = "1";
tabStock[1] = "2";
tabStock[2] = "3";
tabStock[3] = "4";
tabStock[4] = "5";
tabStock[5] = "6";
//stringReceive = "A3E8F6";
tabReceive[0] = "1";
tabReceive[1] = "2";
tabReceive[2] = "3";
tabReceive[3] = "4";
tabReceive[4] = "3";
tabReceive[5] = "6";


}


void draw() {
   background(0);
   compare();

}

void mousePressed() {

    OscMessage myMessage = new OscMessage("/test");

    myMessage.add(123); /* add an int to the osc message */

  /* send the message */
oscP5.send(myMessage, myRemoteLocation);
}


/* incoming osc message are forwarded to the oscEvent method. */
void oscEvent(OscMessage theOscMessage) {
/* print the address pattern and the typetag of the received OscMessage */
print("### received an osc message.");
print(" addrpattern: "+theOscMessage.addrPattern());
println(" typetag: "+theOscMessage.typetag());
stringReceive = theOscMessage.addrPattern();
println(stringReceive);
if (oldStringReceive != stringReceive){
    oldStringReceive = stringReceive;
}
}

void compare() {
    println(stringReceive);
    println("compare()");
    boolean failed=false;
    int j = 0;
    while (cptEssai < 3 && !failed){ 
    for (int i= 0; i < tabStock.length; i++){

    while(j<6){
         if (stringReceive.contains(tabStock[j])){
         print("tag existe et bien placé   //   ");
         print("allumage de la lampe qui correspond à ce tag   //   ");
         j++;
    }
         else {
              print("tag n'existe pas ou mal placé   //   ");
              print("extinction de toutes les lampes   //   ");
              oldStringReceive = stringReceive;
         if (oldStringReceive != stringReceive){
             cptEssai++;
             failed = true;
             break;
         }
         else
         {
            if(j>0){
            j--;
         }
         }

    }
   }
  }
  }
  print("GAME OVER ! Désactivation de l'épreuve   //   ");
}

【问题讨论】:

  • 请提供问题的最小示例。您发布了很多与您的问题无关的代码。
  • 另外,我不明白这与纯数据有什么关系;虽然 Pd 可能是 OSC 消息的发送者,但正确处理消息(或丢弃任何无效消息)完全取决于接收应用程序。顺便问一下stringReceive的值是多少?

标签: nullpointerexception processing puredata


【解决方案1】:

tabStocktabStock[j]stringReceive 中的任何一个都是 null

在该行之前打印出它们的值以检查它是哪一个。

一旦你知道它是哪一个,追溯你的代码以找出为什么它是空的。

【讨论】:

  • 感谢您的回答。我打印了tabStocktabStock[j]stringReceivetabStock 打印出一些我不理解的奇怪东西 [Ljava.lang.String;@1bc135btabStock[j] 打印 1 所以很好,stringReceive 打印 null。我发现它是空的,因为编译器不执行oscEvent(OscMessage theOscMessage) 函数所以它不会影响theOscMessage.addrPattern();stringReceive。你知道纯数据吗?事实上,theOscMessage.addrPattern(); 是我从 Pure Data 中检索到的值。
  • @Vince 不,我从未使用过那个库,抱歉。 println(tabStock) 行正在打印数组的 hashcode,因为 println() 不够聪明,不知道如何打印数组。您可以改用printArray() 函数。
  • Pure Data 不是一个库,它是一个软件,但oscP5 是一个库。我尝试了printArray() 函数,它打印了数组,但现在我被卡住了。因此,我将尝试向了解 Pure Data 和 oscP5 的人提出我的问题。还是谢谢!
猜你喜欢
  • 2013-12-21
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-07
相关资源
最近更新 更多