NullPointerExeption error in array

edited May 2016 in Arduino

I continue to get a "NullPointerExeption" error on an array (defined on line 12) that takes data from a Serial port (data from two potentiometers) , and splits it. The array appears to be initialized correctly. Does anybody know what may be causing this? I have included the code (which is very long) below. Thank you for any help.

import processing.serial.*;

// initialize all variables
Serial myPort;  // Create object from Serial class
public String input;  // Data received from the serial port
PFont font;
int xPosB= 1;         // horizontal position of the graph
int xPosT= 1;
boolean allOn = false;
boolean brakeOn = false;
boolean throttleOn = false;
String arrdata[] =  input.split("/n", 2);

// remove non-numerical characters to create graphable string
String graphableB[] = arrdata[0].split("Brake:", 2);
String graphableT[] = arrdata[1].split("Throttle:", 2);
//convert to integer for graphing
int inByteT = int(graphableT[2]);
int inByteB = int(graphableB[2]);


void setup() {
  size(925, 800);

  //initialize text
  font = createFont("Agency FB", 16, true); //create font
  textFont(font, 36);

  //initialize data acquisition
  //String data = Integer.toString(input); 
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);

  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[0], 9600);

  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');

  // set inital background:
  background(#717070);

  //P5 Setup
  // create a toggle and change the default look to a (on/off) switch look

  import controlP5.*;

  ControlP5 cp0;
  ControlP5 cp1;
  ControlP5 cp2;

  smooth();
  cp0 = new ControlP5(this);
  cp1 = new ControlP5(this);
  cp2 = new ControlP5(this);

  cp0.addToggle("allOn")
    .setPosition(40, 250)
    .setSize(50, 20)
    .setValue(true)
    .setMode(ControlP5.SWITCH)
    .setCaptionLabel("ALL")
    ;

  cp1.addToggle("brakeOn")
    .setPosition(40, 190)
    .setSize(50, 20)
    .setValue(true)
    .setMode(ControlP5.SWITCH)
    .setCaptionLabel("BRAKE")
    ;

  cp2.addToggle("throttleOn")
    .setPosition(40, 130)
    .setSize(50, 20)
    .setValue(true)
    .setMode(ControlP5.SWITCH)
    .setCaptionLabel("THROTTLE")
    ;
}

void draw(){

  //initialize text print
  background(255);
  textFont(font, 16);
  fill(#FF0324);

  if ( myPort.available() > 0) {  // If data is available,
    input = myPort.readString();    // read it and store it in data
    text(arrdata[0], 400, 400);
    text(arrdata[1], 500, 500);
  } else {
    text("NO DATA", 350, 350);
  }

  graphDrawBrake(allOn, brakeOn);
  graphDrawThrottle(allOn, throttleOn);
}


void graphDrawBrake (boolean allOn, boolean brakeOn) {

  while (allOn == true) {
    while (brakeOn == true) {
      // draw the line:
      stroke(#FF0000);
      line(xPosB, height, xPosB, height - inByteB);

      // at the edge of the screen, go back to the beginning:
      if (xPosB >= 400) {
        xPosB = 0;
        background(#717070);
      } else {
        // increment the horizontal position:
        xPosB++;
      }
    }
  }
}

void graphDrawThrottle (boolean allOn, boolean throttleOn) {

  while (allOn == true) {
    while (throttleOn == true) {
      // draw the line:
      stroke(#50D85A);
      line(xPosT, height, xPosT, height - inByteT);

      // at the edge of the screen, go back to the beginning:
      if (xPosT < 400 || xPosT >= 800) {
        xPosT = 400;
        background(#717070);
      } else {
        // increment the horizontal position:
        xPosT++;
      }
    }
  }
}

Answers

  • edited May 2016
    • Java initializes all reference type variables w/ null if we don't do it.
    • So @ String input;, Serial myPort;, PFont font;, all of those 3 variables starts as null.
    • Hence @ line #12, input.split("/n", 2);, you try to invoke method split() over null, b/c variable input is still null! :-\"
  • You can try

    if (input!=null)

    before line 12

  • edited May 2016

    @Chrisir, line #12 is still within sketch's field declaration section! :-\"

  • Ok, so in draw

    if.....

      split
    

    before setup() only The declaration

  • @Chrisir the input should not be null in the first place it should take data from the serial port

  • @Chrisir got that error fixed, now I am getting "Error insert '.class' to complete expression"

  • Show entire Code

    Line Number of error?

Sign In or Register to comment.