I need to pass buffer object which has generated values to construcor of reading class to print real time generated numbers.
Which is the best way to do it?
When I pass generated value I get the number but I need to pass values that are beeinig generated and writed during the thread execution?

public class Reader implements Runnable {
        // private int randomN;
        public Reader() {
            // this.randomN = randomN;
        }

and part of the the buffer class

    private class Buffer extends JTextField
    {
        int n;
        boolean valueSet = false;

        synchronized int get() {

            if (!valueSet)
                try {
                    wait();
                } catch (InterruptedException e) {
                    System.out.println("InterruptedException caught");
                }
            //System.out.println("Got: " + n);
            valueSet = false;
            notify();
            return n;
        }

from the assigment:

A class that is a subclass of JTextField that will be passed into the constructors of the two Threads mentioned next.
The access to this object will need to be coordinated by either explicit locking or by synchronizing.

Edited 4 Years Ago by mehnihma: editing

Just pass the Buffer object like any other parameter

 public Reader(Buffer b) {
     ...
     // use b to access the Buffer's methods and variables


 Buffer myBuffer = new Buffer();
 Reader myReader = new Reader(myBuffer);

But still I have problems geting it in the class
I get Null pointer

Buffer myBuffer = new Buffer();

    t1 = new Thread(new Writer());

Thread read = new Thread(new Reader(myBuffer));





public class Reader implements Runnable {
        // private int randomN;

        Buffer b;
        public Reader(Buffer b) {
            // this.randomN = randomN;
            //this.b = b;
        }

        public void run() {

            // create a loop and write

            // create a loop and write
            int targetNum = Integer.parseInt(tTarget.getText()); // parse to
                                                                    // integer
            // Random rand = new Random(); // random nuber generator
            while (true) {


                lock.lock();
                // System.out.println(x);

                if (b.get() == targetNum) {
                    break; // found the number

                }

Edited 4 Years Ago by mehnihma: 2

Please post the full text of the error message. It shows which line the error occured on.
Or if you know the line number, look at that line and find what variable has the null value and then backtrack in the code to see why that variable does not have a valid value.

Why is line 17 commented out? This would give b a value.

Edited 4 Years Ago by NormR1

If I do it like this

   public class Reader implements Runnable {
        // private int randomN;

        Buffer b;

        public Reader(Buffer b) {
            // this.randomN = randomN;
            this.b = b;
        }

        public void run() {

            // create a loop and write

            // create a loop and write
            int targetNum = Integer.parseInt(tTarget.getText()); // parse to
                                                                    // integer
            // Random rand = new Random(); // random nuber generator
            while (true) {

                lock.lock();
                // System.out.println(x);

                if (b.get() == targetNum) { // see if is the same number in te
                                            // buffer
                    // release buttons
                    btnStart.setEnabled(true);
                    btnReset.setEnabled(true);
                    break; // found the number

I get nul pointer again on if (b.get() == targetNum) ?

Exception in thread "Thread-4" java.lang.NullPointerException
at TargetFinder$Reader.run(TargetFinder.java:244)
at java.lang.Thread.run(Thread.java:680)

The code calling threads

Buffer myBuffer = new Buffer();

writerT = new Thread(new Writer(myBuffer));

read = new Thread(new Reader(myBuffer));

Edited 4 Years Ago by mehnihma: 2

And how to print buffered number in JTextArea?
I get this

TargetFinder$Buffer[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=com.apple.laf.AquaTextFieldBorder@da0225b,flags=288,maximumSize=,minimumSize=,preferredSize=,caretColor=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0],disabledTextColor=javax.swing.plaf.ColorUIResource[r=128,g=128,b=128],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=com.apple.laf.AquaImageFactory$SystemColorProxy[r=0,g=0,b=0],selectionColor=com.apple.laf.AquaImageFactory$SystemColorProxy[r=169,g=201,b=255],columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]

It is not, I pasted wrong code,
I got it not wrok, but now it just stops,
It is working without this buffer, I do not get how to read that number that class Read has read from the writter and print in in JTextField

Please post the right code that shows what your problem is and add comments to the code that describes your problem.

I need to start a thread that reads and writes

Random rand = new Random(); // random nuber generator
                        // check if the target number is smaller then max number
                        if (maxTarget < maxNum) {
                            // x = rand.nextInt(maxNum + 1); // set maximum
                            // number

                            // area.append(x+" ");
                            // thread for writing random numbers
                            // lock buttons
                            btnStart.setEnabled(false);
                            btnReset.setEnabled(false);

                            Buffer myBuffer = new Buffer();

                            writerT = new Thread(new Writer());

                            read = new Thread(new Reader());

                            // Reader myReader = new Reader(myBuffer);
                            read.start();
                            writerT.start();

And pass it to read and write classes

  public class Writer implements Runnable {
        private int x;
        private Buffer b;

        public Writer() {
            //this.b = b;
            //this.x = x;
        }

        public void run() {

            // create a loop and write
            maxTarget = Integer.parseInt(tTarget.getText()); // parse to integer
            Random rand = new Random(); // random nuber generator
            while (true) {

            lock.lock();

            x = rand.nextInt(maxNum + 1); // set maximum number

            // System.out.println(x);
            myBuffer.get(x); // send to buffer
            area.append(x + " ");

            lock.unlock();
            // notify();
            try {
                Thread.sleep(1);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }


            }

        }
    }

it work without buffer, but when I want to pass buffer I get problems

There is one more class read() which reads data that write class writes

  public class Reader implements Runnable {
        // private int randomN;

        Buffer b;

        public Reader() {
            // this.randomN = randomN;
             //this.b = b;
        }

        public void run() {

            // create a loop and write

            // create a loop and write
            int targetNum = Integer.parseInt(tTarget.getText()); // parse to
                                                                    // integer
            // Random rand = new Random(); // random nuber generator
            while (true) {

                //area.setText(b+"");
                lock.lock();
                // System.out.println(x);

                if (myBuffer.get() == targetNum) { // see if is the same number in te
                                            // buffer
                    // release buttons
                    btnStart.setEnabled(true);
                    btnReset.setEnabled(true);
                    break; // found the number

                }

                lock.unlock();
                // notify();
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e1) {

                    e1.printStackTrace();
                }

            }

        }
    }

All classes are inner classes
Now I have problems adding what writer has written and send it to buffer read it and print it in JTexArea for every number till the end,
If I add buffer as a variable I get only one value but I need every value from writter class, how can I do that

Edited 4 Years Ago by mehnihma: ed

I need every value from writter class, how can I do that

Can you post a small complete program that compiles and executes and shows the problem?

This question has already been answered. Start a new discussion instead.