ExitValue from Runtime Process not returning

B

Binaebi A.

I'm currently writing a java program that interfaces with a Perl
script through a Linux command shell. I am able to interact with the
script, but with each action I get a Null Exception Error which does
not prevent the program from working. How is it possible that an error
can be thrown and yet not stop the program from working?

I am testing to make sure the process is completed before the program
exits, and I'm wondering if maybe the exit value is not returning as
it should. I know the error is somewhere at the end of my code,
because everything in the method executes perfectly. I get correct
return values, but when I try to send the exit value to the screen, no
value shows up. I have looked online at other examples of code, and
mine seems to match theirs...so I'm wondering how it's possible that
my code has decided to ignore the exit value and (I think) thereby
cause a null pointer exception.

I have included my code in the hopes someone will have an idea how to
help me correct this odd error.

*******************************************************************************

ArrayList getIdFromUser () {
ArrayList pbsIdArray = new ArrayList();
try {
//send command line to new shell
Process proc = Runtime.getRuntime().exec(cmd);
BufferedReader in = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
BufferedReader error = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));

//read input, test substring for pbs id
String line;
while(!(line = in.readLine()).equals(null)) {
//System.out.println(line);
if (test values) {
//if true, nothing should happen
}
else {
//if false, add pbs to array list
pbsId = line;
pbsIdArray.add(pbsId);
}
}

//read error results
String eLine;
while(!(eLine = error.readLine()).equals(null))
pbsIdArray.add(eLine);

//wait for the process to end, then close process
try{ proc.waitFor(); }
catch(InterruptedException ie) {
pbsIdArray.add("ERROR: Process interrupted: " + ie.toString());
}

System.err.println("Exit: " + proc.exitValue());

//close streams
in.close();
error.close();

} catch (IOException e) {
pbsIdArray.add("ERROR: File not found.");
}
catch (NullPointerException n) {
pbsIdArray.add("ERROR: Null Pointer Exception.");
/*this is the error I get even though it doesn't throw me
from the method before its internal workings are
complete*/
}

return pbsIdArray;
}
 
M

Michael Borgwardt

Binaebi said:
I have included my code in the hopes someone will have an idea how to
help me correct this odd error.

Nothing odd about it.

The main point about Java exceptions is that they can tell you almost
exactly where the problem occurs via the stack trace. Don't just
return a message that an NPE occurred, print out the stack trace
with the Exception.printStackTrace() method. It will tell you the
line in which the exception was thrown, which usually leaves only
one or two potential culprits. Then you just have to find out why
the variable in question had a null value when you expected it
to point to an object. In most cases this is quite obvious once
you look for it specifically.
 
G

Gordon Beaton

I'm currently writing a java program that interfaces with a Perl
script through a Linux command shell. I am able to interact with the
script, but with each action I get a Null Exception Error which does
not prevent the program from working. How is it possible that an error
can be thrown and yet not stop the program from working?


The error occurs after the child process has already
completed, when readLine() returns null at EOF. You attempt
to call null.equals(), which causes the exception.

To compare a reference with null, use one of the identity
operators == or !=, not equals().

Change the following line:
while(!(line = in.readLine()).equals(null)) {

to:

while ((line = in.readLine()) != null)

and this:
while(!(eLine = error.readLine()).equals(null))

to:

while ((eline = error.readLine()) != null)

/gordon
 
L

Liz

Gordon Beaton said:
The error occurs after the child process has already
completed, when readLine() returns null at EOF. You attempt
to call null.equals(), which causes the exception.

To compare a reference with null, use one of the identity
operators == or !=, not equals().

Change the following line:


to:

while ((line = in.readLine()) != null)

I use this myself, but when I run "jikes +P <filename>"
to find errors it says that this is too complicated.
(compound statement)
What would you recommend that is not?
 
G

Gordon Beaton

I use this myself, but when I run "jikes +P <filename>"
to find errors it says that this is too complicated.
(compound statement)
What would you recommend that is not?

I think the example is clear and concise, and that jikes is being too
picky.

This seems to be a common alternative:

line = in.readLine();

while (line != null) {
// do some stuff with line
[...]

line = in.readLine();
}

....but I find it unnecessarily verbose, and the logic isn't quite as
clear when code is repeated outside and inside the loop. This is
especially true when the missing stuff, which could be long, gets
filled in.

There are surely other ways to do the same thing, but my example is a
common idiom that most programmers will recognize immediately.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top