null pointer exception in a processing code.

edited June 2015 in Arduino

I have a problem with the code in the "process"

I'm using a controlled by a remote control receiver infrared sensor. The data received by the Arduino sends me to successfully process, but does not allow me to compare the variable "val" with some code that you know it will allow me to continue with the program.

The function is:

Pressing a button, for example (game = 4C73914C) a specific text should appear.

And so on.

And I make that mistake when I make that comparison.

Codigo arduino.


#include 
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup(){
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver

  while(!Serial){
   ; 
  }
  
  pinMode(11, INPUT);
  establishContact();
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
  }
}

void establishContact() {
  while (Serial.available() 

import processing.serial.*; Serial port; //Nombre del puerto serie String val; String val1;

boolean status = false;

void setup() { size(640, 480); //Creamos una ventana de 800 píxeles de anchura por 600 píxeles de altura println(Serial.list()); //Visualiza los puertos serie disponibles en la consola de abajo port = new Serial(this, Serial.list()[1], 9600); //Abre el puerto serie COM3

}

void draw() { }

void serialEvent( Serial myPort) { //put the incoming data into a String - //the '\n' is our end delimiter indicating the end of a complete packet val = myPort.readStringUntil('\n'); //make sure our data isn't empty before continuing if (val != null) { //trim whitespace and formatting characters (like carriage return) val1 = trim(val); control(); } }

void control(){ if (val1.equals("4C73914C")== true){ background(255,0,0); println ("EJERCICIO1", val1); status=!status; //El estado del color cambia port.write("A"); //Envia una "A" para que el Arduino encienda el led } else if(val1.equals("57564EEC") || val1.equals("D54CF430")){ status =status ; port.write("B"); } if(val1.equals("57564EEC")){ background(0,255,0); println("EjJERCICIO2", val1); status = !status; port.write("C"); } else if(val1.equals("4C73914C") || val1.equals("D54CF430")){ status =status ; port.write("D"); }

if(val1.equals("D54CF430")){ background(255,255,0); println("EjJERCICIO3", val1); status = !status; port.write("E"); } else if(val1.equals("4C73914C") || val1.equals("57564EEC")){ status =status ; port.write("F"); }

if(val1.equals("44C73928")){ println("Salida"); status = status; port.write("B"); port.write("D"); port.write("F"); exit();
}

if(val1.equals("9AD2DF84")){ println("Regresar"); } } </pre

Answers

  • edited May 2015

    include <IRremote.h>

    int RECV_PIN = 11; IRrecv irrecv(RECV_PIN); decode_results results;

    void setup(){ Serial.begin(9600); irrecv.enableIRIn(); // Start the receiver

    while(!Serial){ ; }

    pinMode(11, INPUT); establishContact(); }

    void loop() { if (irrecv.decode(&results)) { Serial.println(results.value, HEX); irrecv.resume(); // Receive the next value } }

    void establishContact() { while (Serial.available() <= 0) { Serial.println("0,0,0"); // envía una cadena inicial delay(300); } }

    This is a arduino code

  • edited May 2015

    import processing.serial.*;

    Serial myPort; // Create object from Serial class String val; // Data received from the serial port String codigo = "4C73914C"; // I know that the first port in the serial list on my mac // is Serial.list()[0]. // On Windows machines, this generally opens COM1. // Open whatever port is the one you're using.

    void setup() { size(800,400); background(255); String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port myPort = new Serial(this, portName, 9600); } void draw() { if ( myPort.available() > 0) { // If data is available, val = myPort.readStringUntil('\n'); // read it and store it in val

    clear(); fill(255,255,255); textSize(32); text("Codigo obtenido: " + val, 90, 90);

    if (val.equals(codigo) == true ){ println ("activated Code"); } else { println ("enabled no code"); } }

    println(val); //print it out in the console

    }

  • Answer ✓

    First thing - screenshots of code is useless because no one can run or experiment with the code. Screenshots of un-formatted code is even less useful.

    Use the Auto Format option in the PDE to fix this.

    You don't even say in which line the NPE occurred.

    Two things to cheque

    I can't see a variable declaration codigo

    Is it possible that myPort.readStringUntil(... retuns null if so then you need to test the value of val

  • See How to format code and text. Edit your messages and format your code.

Sign In or Register to comment.