我写了个程序,但是编译是出问题了,好像是在RUN里有问题,但就是不明白为什么会说我空指针
我用的是jdk1.5
下面是编译结果,再下面是源程序
D:\>java GetFil
Exception in thread "Thread-0" java.lang.NullPointerException
at GetFil.run(GetFil.java:66)
at java.lang.Thread.run(Thread.java:595)D:\>
/*
* 创建日期 2005-11-24
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
//package net.vicp.gdy;
import java.net.*;
import java.io.*;
/**
* @author gdy
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/public class GetFile implements Runnable { /** How many thread will be use when downloading this file */
private int threads=1;
/** How many Bytes will be cached per thread */
private int cacheBytes = 1024;
/** add a thread when downloading a file */
public void addThread(){
threads++;
}
/** decreaseThread */
public void decreaseThread(){
threads--;
}
/**
*
*/
URL u = null;
URLConnection uc = null;
InputStream is = null;
FileOutputStream fo = null;
public GetFile(){
}
public GetFile(String addressStr, String whereToPut) {
//this.threads = threads;
try{
URL u = new URL(addressStr);
URLConnection uc = u.openConnection();
InputStream is = uc.getInputStream();
FileOutputStream fo = new FileOutputStream(whereToPut);
}catch(FileNotFoundException fnf){
System.out.println("FileNoFound Exception");
}catch(IOException io){
System.out.println("there is a error when initial");
}
// TODO 自动生成构造函数存根
} /* (非 Javadoc)
* @see java.lang.Runnable#run()
*/ public void run() {
// TODO 自动生成方法存根
byte[] inputBuffer = new byte[cacheBytes];
try{
int byteRead = 0;
int availableByte = is.available();;
int totalByte = 0;
while(totalByte < availableByte){
byteRead = is.read(inputBuffer);
if(byteRead == 1024){
fo.write(inputBuffer);
}else if(byteRead >= 0) {
fo.write(inputBuffer,0,byteRead);
break;
}else break;
}
}catch(IOException ioe){
System.out.println("error in run");
}
} public static void main(String[] args) {
GetFile gf = new GetFile("http://127.0.0.1/iisstart.asp","d:\\a.asp");
Thread t = new Thread(gf);
t.start();
}
}
我用的是jdk1.5
下面是编译结果,再下面是源程序
D:\>java GetFil
Exception in thread "Thread-0" java.lang.NullPointerException
at GetFil.run(GetFil.java:66)
at java.lang.Thread.run(Thread.java:595)D:\>
/*
* 创建日期 2005-11-24
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
//package net.vicp.gdy;
import java.net.*;
import java.io.*;
/**
* @author gdy
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/public class GetFile implements Runnable { /** How many thread will be use when downloading this file */
private int threads=1;
/** How many Bytes will be cached per thread */
private int cacheBytes = 1024;
/** add a thread when downloading a file */
public void addThread(){
threads++;
}
/** decreaseThread */
public void decreaseThread(){
threads--;
}
/**
*
*/
URL u = null;
URLConnection uc = null;
InputStream is = null;
FileOutputStream fo = null;
public GetFile(){
}
public GetFile(String addressStr, String whereToPut) {
//this.threads = threads;
try{
URL u = new URL(addressStr);
URLConnection uc = u.openConnection();
InputStream is = uc.getInputStream();
FileOutputStream fo = new FileOutputStream(whereToPut);
}catch(FileNotFoundException fnf){
System.out.println("FileNoFound Exception");
}catch(IOException io){
System.out.println("there is a error when initial");
}
// TODO 自动生成构造函数存根
} /* (非 Javadoc)
* @see java.lang.Runnable#run()
*/ public void run() {
// TODO 自动生成方法存根
byte[] inputBuffer = new byte[cacheBytes];
try{
int byteRead = 0;
int availableByte = is.available();;
int totalByte = 0;
while(totalByte < availableByte){
byteRead = is.read(inputBuffer);
if(byteRead == 1024){
fo.write(inputBuffer);
}else if(byteRead >= 0) {
fo.write(inputBuffer,0,byteRead);
break;
}else break;
}
}catch(IOException ioe){
System.out.println("error in run");
}
} public static void main(String[] args) {
GetFile gf = new GetFile("http://127.0.0.1/iisstart.asp","d:\\a.asp");
Thread t = new Thread(gf);
t.start();
}
}
int byteRead = 0;
int availableByte = is.available();;
int totalByte = 0;
while(totalByte < availableByte){
byteRead = is.read(inputBuffer);
if(byteRead == 1024){
fo.write(inputBuffer);
}else if(byteRead >= 0) {
fo.write(inputBuffer,0,byteRead);
break;
}elsebreak;}
}catch(IOException ioe){
System.out.println("error in run");
}
InputStream is = uc.getInputStream();
FileOutputStream fo = new FileOutputStream(whereToPut);应为is = uc.getInputStream();
fo = new FileOutputStream(whereToPut);