ez4nick
ez4nick

Reputation: 10199

Trying to pass instance of an object to another class

I have a Status object in my DrawStatus class that I would like to pass to my FileStatus Class. Currently my code is as follows given below:

public class DrawStatus extends JPanel{
    Status status = new Status();


    public DrawStatus(){

    }

    public Status getStatusObject(){
        return status;
    }
}

and this is the FileStatus class:

public class FileStatus {
DrawStatus status;
Status s;

public FileStatus(){
    s=status.getStatusObject();
}

public void writeToFile(){
    String file_text = s.getPedestrianStatusText() + "     " + s.getGatesStatusText() + "     " + s.getDrawBridgePositionText();
    System.out.println("\n" + file_text);
}
}

and the problem is that on the line

s=status.getStatusObject();

I get a NPE, and I am not sure why the object would be null. Any advice on how to fix this issue is appreciated.

Upvotes: 0

Views: 48

Answers (2)

RKC
RKC

Reputation: 1874

Is this reference initialized DrawStatus status before using that?

DrawStatus status=new DrawStatus();

Upvotes: 0

Salah
Salah

Reputation: 8657

You haven't initialized the DrawStatus variable in FileStatus class.

public class FileStatus {
DrawStatus status;
Status s;

So try to initialize it by doing something like:

public class FileStatus {
DrawStatus status = new DrawStatus();
Status s;

Upvotes: 3

Related Questions