NullPointerException with arduino library

edited February 2014 in Arduino

Hello, I am using the arduino library (http://playground.arduino.cc/Interfacing/Processing#.UwffIYXW9PY) and when running this line of code (in void setup()): arduino.pinMode(driveMotorPWM1, Arduino.OUTPUT); it gives me a NullPointerException. I did call the variable in this line and the wiki doc on this error code is gibberish to me as a newbie with Processing and Java. If this helps at all, here is all the code. It is not finished so there are some loose ends:

       import processing.serial.*;
        import cc.arduino.*;

        Serial port;
        Arduino arduino;

        int driveMotorPWM1 = 11; //1st drive motor PWM for driver board
        int driveMotorI1 = 12; //I = Current
        int driveMotorDir1 = 13; //Dir = Direction
        int driveMotorPWM2 = 10;
        int driveMotorI2 = 9;
        int driveMotorDir2 = 8;
        int conveyorPWM = 5; //Conveyor controlled by motor driver board as well, so has same inputs
        int conveyorI = 6;
        int conveyorDir = 7;
        int servo = 4;
        int servoTime = 0;
        int upGUI = 0;
        int downGUI = 0;
        int rightGUI = 0;
        int leftGUI = 0;


        void setup() {
          background(10, 245, 227);
          servoTime = millis();
          size(700,500);
          rect(30, 20, 55, 55, 7); 
          println("Available serial ports:");
          println(Serial.list());
          port = new Serial(this, "COM3", 9600);  
          arduino.pinMode(driveMotorPWM1, Arduino.OUTPUT);
          arduino.pinMode(driveMotorI1, Arduino.OUTPUT);
          arduino.pinMode(driveMotorDir1, Arduino.OUTPUT);
          arduino.pinMode(driveMotorPWM2, Arduino.OUTPUT);
          arduino.pinMode(driveMotorI2, Arduino.OUTPUT);
          arduino.pinMode(driveMotorDir2, Arduino.OUTPUT);
          arduino.pinMode(conveyorPWM, Arduino.OUTPUT);
          arduino.pinMode(conveyorI, Arduino.OUTPUT);
          arduino.pinMode(conveyorDir, Arduino.OUTPUT);
          arduino.pinMode(servo, Arduino.SERVO);

        }

        void keyPressed() {
          if (key == CODED) {
              if (keyCode == UP) { //forward
                arduino.digitalWrite(driveMotorI1 & driveMotorDir1, Arduino.HIGH);
                arduino.analogWrite(driveMotorPWM1, 255);
                arduino.digitalWrite(driveMotorI2 & driveMotorDir2, Arduino.HIGH);
                arduino.analogWrite(driveMotorPWM2, 255);
                upGUI = 1;
            } else if (keyCode == DOWN) { //backward
                arduino.digitalWrite(driveMotorI1, Arduino.HIGH);
                arduino.digitalWrite(driveMotorDir1, Arduino.LOW);
                arduino.analogWrite(driveMotorPWM1, 255);
                arduino.digitalWrite(driveMotorI2, Arduino.HIGH);
                arduino.digitalWrite(driveMotorDir2, Arduino.LOW);
                arduino.analogWrite(driveMotorPWM2, 255);
                downGUI = 1;  
            } else if (keyCode == RIGHT) { //rotate right
                arduino.digitalWrite(driveMotorI1 & driveMotorDir1, Arduino.HIGH);
                arduino.analogWrite(driveMotorPWM1, 255);
                arduino.digitalWrite(driveMotorI2, Arduino.HIGH);
                arduino.digitalWrite(driveMotorDir2, Arduino.LOW);
                arduino.analogWrite(driveMotorPWM2, 255);  
                rightGUI = 1;
            } else if (keyCode == LEFT) { //rotate left
                arduino.digitalWrite(driveMotorI1, Arduino.HIGH);
                arduino.digitalWrite(driveMotorDir1, Arduino.LOW);
                arduino.analogWrite(driveMotorPWM1, 255);
                arduino.digitalWrite(driveMotorI2 & driveMotorDir2, Arduino.HIGH);
                arduino.analogWrite(driveMotorPWM2, 255);  
                leftGUI = 1;
          } 
         }
        }

        void draw() {

        }

Answers

  • Did a little more research on the error code. Could it be that the arduino is just not hooked up to any outputs as I run it?

  • edited March 2014 Answer ✓

    Can you also post the error code. Also try this code for the serial port

    String portName = Serial.list()[0];
    myPort = new Serial(this, portName, 9600);
    
  • Thanks, but it was just that the arduino was not hooked up to any outputs DebianAddict

Sign In or Register to comment.