TNTanuki
TNTanuki

Reputation: 59

Initializing Java variable

I am starting a process. And later I want to be able to destroy the said process then start it again.

Process myProcess;

try {
    myProcess = Runtime.getRuntime().exec(pathToMyProgram);
} catch (IOException e) {
    e.printStackTrace();
}

Then I do stuff. And later:

try {
    myProcess.destroy();
    myProcess = Runtime.getRuntime().exec(pathToMyProgram);
} catch (IOException e) {
    e.printStackTrace();
}

The problem is at the line

myProcess.destroy();

There is an error because myProcess can be uninitialized at this point. Also I can't do something like:

myProcess = new Process();

I know I could solve this by putting everything in a big try{} statement but is there another way ?

EDIT: I cannot do the null check either, the error is: the process might not be initialized.

But thanks to you I then tried to do Process myProcess = null; and now it works ! Thank you and sorry for the bad question.

I'm removing the process tag for relevance. And edited title.

Upvotes: 1

Views: 53

Answers (1)

TNTanuki
TNTanuki

Reputation: 59

Doing so solved my problem. Sorry for the newb question.

Process myProcess = null;

Upvotes: 4

Related Questions