Substring null pointer exception
in
Programming Questions
•
1 year ago
Hi All,
I'm new to processing but haven't really got the time to go through all the tutorials, so sorry if I've made a really stupid mistake somewhere ;)
I'm trying to create a program that will 'map' how far an object is from a sonar sensor. Most of the work is done on the arduino, which takes the data and turns it into an x,y value. These are printed to the serial like this (only there are 180 of them):
x value ; y value
0.00 ; 0.00 <--- That's the smallest form
508.40 ; 215.80 <--- That's the largest form
I'm trying to split them up into individual x y values and then convert them to integers so I can draw some lines, but I'm getting the "nullpointerexception" error. Here is my code so far:
- import processing.serial.*;
- Serial myPort;
- void setup()
- {
- size(600, 400);
- String portName = Serial.list()[0];
- myPort = new Serial(this, portName, 9600);
- }
- void draw()
- {
- String xy = myPort.readString();
- String x = xy.substring(0, 4);
- String y = xy.substring(7);
- println(x);
- println(y);
- }
And here is the whole error:
- RXTX fhs_lock() Error: creating lock file: /var/lock/LCK..ttyUSB0: File exists
- RXTX fhs_lock() Error: creating lock file: /var/lock/LCK..ttyUSB0: File exists
- RXTX fhs_lock() Error: creating lock file: /var/lock/LCK..ttyUSB0: File exists
- Exception in thread "Animation Thread" java.lang.NullPointerException
- at Mapping.draw(Mapping.java:37)
- at processing.core.PApplet.handleDraw(Unknown Source)
- at processing.core.PApplet.run(Unknown Source)
- at java.lang.Thread.run(Thread.java:662)
Any idea how to fix this? Maybe there's even a simpler way of doing this whole thing that my brain doesn't know about yet??
This is the Arduino code if it helps at all:
- #include <Servo.h>
- #include <math.h>
- Servo myservo;
- float xval;
- float yval;
- float rad;
- int distance = 3; //This is just to test the code, I'll add ping code later
- int deg = 0;
- void setup(){
- myservo.attach(9);
- Serial.begin(9600);
- }
- void loop(){
- while (deg <180){
- myservo.write(deg);
- rad = deg * 0.0174532925;
- xval = 300 - ((distance * cos(rad))*100);
- yval = (distance * sin(rad)) * 100;
- Serial.print(xval);
- Serial.print(" ; ");
- Serial.println(yval);
- delay(100);
- deg++;
- }
- }
Thanks!
1