0

I have an error nullPointerException at the line if (stringReceive.contains(tabStock[j])){ but I don't understand why there is an error with the index j of the array tabstock.

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   //   ");
}
Vince
  • 1
  • please provide a *minimal* example of the problem. you have posted quite a lot of code that is unrelated to your problem. – umläute Mar 17 '16 at 21:01
  • also, i don't see how this is related to Pure Data; while Pd might be the sender of the OSC-message, it is entirely up to the receiving application to correctly handle the message (or discard any invalid message). incidentally, what is the value of `stringReceive`? – umläute Mar 17 '16 at 21:02

1 Answers1

0

Either tabStock, tabStock[j] or stringReceive is null.

Print out their values before that line to check which one it is.

Once you know which one it is, trace back through your code to figure out why it's null.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Thanks for your answer. I made a print of `tabStock`, `tabStock[j]` and `stringReceive`. `tabStock` prints something weird that I don't understand `[Ljava.lang.String;@1bc135b`, `tabStock[j]` prints `1` so it's good and `stringReceive` prints `null`. I found it's null because the compiler doesn't execute the `oscEvent(OscMessage theOscMessage)` function so it can't affect `theOscMessage.addrPattern();` to `stringReceive`. Do you know Pure Data ? In fact `theOscMessage.addrPattern();` is a value that I retrieve from Pure Data. – Vince Mar 15 '16 at 18:19
  • @Vince Nope, I've never worked with that library, sorry. The `println(tabStock)` line is printing the array's **hashcode**, since `println()` isn't smart enough to know how to print an array. You can use the [printArray()](https://www.processing.org/reference/printArray_.html) function instead. – Kevin Workman Mar 15 '16 at 18:31
  • Pure Data isn't a library, it's a software but ``oscP5`` is a library. I tried the ``printArray()`` function and it prints the array but now I'm stuck. So I will try to ask my question to someone who knows Pure Data and ``oscP5``. Thanks anyway ! – Vince Mar 15 '16 at 19:03