jxgn
jxgn

Reputation: 801

Thread Start Issue

I am developing an Android app using Bluetooth. I want to have the write and the read running in two separate threads.

Write Thread:

class Write extends Thread {

    public void run(){

        while(Bluetooth.threadStateWrite){
            if(LLTestAppActivity.DEBUG){
                Log.d("BLUETOOTH_WRITE", "Write Thread Running!");
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Read Thread

class Read extends Thread {

public void run(){

    while(Bluetooth.threadStateRead){
        if(LLTestAppActivity.DEBUG){
            Log.d("BLUETOOTH_READ", "Read Thread Running!");
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }       
    }
}
}

I call these two threads from the below Bluetooth class:

public class Bluetooth {

Write write;
Read read;

//Constructor
public Bluetooth() {    
    write.start();
    read.start();

}

The Write and Read classes are inside the Bluetooth class.

So when I try to instantiate the Bluetooth class I get NullPointer exception at the constructors. Can anyone guide me how to do this? Thanks in advance.

Upvotes: 0

Views: 42

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

you will need to initialize write and read using class Constructor before calling start method as:

public Bluetooth() {    
    write=new Write();  //create Write class Object
    write.start();
    read=new Read();    //create Read class Object
    read.start();

}

Upvotes: 2

Related Questions