The attached code generates a NullPointerException which I do not
understand, as none of the relevant objects appear to be null. Any
explanation or work-around would be appreciated. I am using JDK 1.0.2 on a
Debain 2.0 box, libc5. I need to stick with 1.0.2 for this project so
that 3.0 browsers can run the code.
Here is the error message produced by the driver program:
bash-2.00$ java ReaderTest
0
19928
0
java.io.PipedOutputStream@404e8698
java.lang.NullPointerException
at java.io.PipedOutputStream.write(PipedOutputStream.java)
at java.io.OutputStream.write(OutputStream.java)
at seismo.Reader.run(Reader.java:38)
Thanks,
Richard Hall
Network Services
University of Tennessee
import seismo.Reader;
import java.net.MalformedURLException;
public class ReaderTest {
public static void main(String[] args) {
try {
Reader r = new Reader("http://pobox.ns.utk.edu/~hall/java/seismo/data/test");
}
catch(MalformedURLException e) { System.out.println("Learn to type"); }
}
}
package seismo;
import java.io.*;
import java.net.*;
public class Reader extends Thread implements SeismoFeed {
private PipedOutputStream output;
private InputStream stream;
private int compress_factor = 1;
private boolean compressed = false;
private boolean simple = false;
private int total;
private int MAX = 576000;
public Reader(URL u) {
try {
stream = u.openStream();
output = new PipedOutputStream();
start();
}
catch(IOException e) { System.err.println("Reader unable to find data"); }
}
public Reader(String s) throws MalformedURLException { this(new URL(s)); }
public void run() {
while(true) {
byte[] b = null;
int bite = 0;
try {
while((bite = stream.available()) > 0) {
stream.read((b = new byte[bite]));
System.out.println(b[0]);
System.out.println(b.length);
System.out.println(b[b.length - 1]);
System.out.println(output.toString());
output.write(b);
total += bite;
}
}
catch(IOException e) {
System.err.println("IOException while reading from stream. Aborting");
die();
}
if(total >= MAX) die();
yield();
}
}
public void die() {
try {
stream.close();
output.close();
}
catch(IOException e) {}
stream = null;
output = null;
stop();
}
public PipedOutputStream getOutput() { return output; }
public void setCompression(int cf) {
compressed = true;
compress_factor = cf;
}
public void noCompression() { compressed = false; }
}