I'm currently working on a pipe system in Java where many processors can write in it and read from it. The code compiles without any errors but when I try to run it, it throws me the following exception: Exception in thread "main" java.lang.NullPointerException at Processor.main(Processor.java:42)
And here is my code so far:
class Pipe {
private float[] buffer;
private boolean buffer_blocked;
public Pipe() {
this.buffer = new float[10];
this.buffer_blocked = false;
}
public synchronized void send(int chnl, float value) {
while (buffer_blocked) {
try {
wait();
} catch(InterruptedException e) {}
}
buffer_blocked = true;
buffer[chnl] = value;
buffer_blocked = false;
notifyAll();
}
public synchronized float receive(int chnl) {
while (buffer_blocked) {
try {
wait();
} catch(InterruptedException e) {}
}
return buffer[chnl];
}
}
public class Processor {
private static Pipe pipe;
public Processor() {
// empty constructor
}
public static void main(String[] args) {
Processor P1 = new Processor();
Processor P2 = new Processor();
P1.pipe.send(0, 6.1f); // write
float number = P2.pipe.receive(0); // read;
System.out.println(number); // should print 6.1
}
}
I read something about that the array must not contain null pointers when it comes to setting values but I didn't use null pointers. I appreciate any kind of help, thanks in advance!