Ver Mensaje Individual
  #8 (permalink)  
Antiguo 14/12/2011, 14:34
raislin
 
Fecha de Ingreso: mayo-2008
Mensajes: 124
Antigüedad: 16 años
Puntos: 0
Respuesta: Ficheros en java

Hola , me da un error de NullPointerException y no se xq
public class Vehiculos {
private int Codigo;
private int Capacidad;
private String Matricula;
private String FechaAdquisicion;
private Repostajes Vehiculo;
private int VecesRepostado;
public Vehiculos(){
this.FechaAdquisicion ="" ;
this.Capacidad=0;
this.Codigo=0;
this.Matricula="";
this.VecesRepostado=0;
}
public Vehiculos(int cod,int cap,String mat,String fch){
this.FechaAdquisicion = fch;
this.Capacidad=cap;
this.Codigo=cod;
this.Matricula=mat;
}
public void setCodigo(int cod){
this.Codigo=cod;
}
public void setCapacidad(int cap){
this.Capacidad=cap;
}
public void setMatricula(String mat){
this.Matricula=mat;
}
public void setFechaAdquisicion(String fch){

this.FechaAdquisicion = fch;
}
public int getCodigo(){
return Codigo;
}
public int getCapacidad(){
return Capacidad;

}
public String getMatricula(){
return Matricula;
}
public String getFechaAdquisicion(){
return FechaAdquisicion;
}
public void setCombustible(int comb){
this.Vehiculo.setCombustible(comb);
}
public void setKilometraje(int km){
this.Vehiculo.setKilometraje(km);
}
public void setImporte(double imp){
this.Vehiculo.setImporte(imp);
}
public int getCombustible(){
return Vehiculo.getCombustible();
}
public int getKilometraje(){
return Vehiculo.getKilometraje();
}
public double getImporte(){
return Vehiculo.getImporte();
}
public void setVecesRepostado(int veces){
this.VecesRepostado=veces;
}
public int getVecesRepostado(){
return VecesRepostado;
}
public int IncrementoVecesRepostado(){
return VecesRepostado++;
}
}

public class Main {
public static void GenerarFicheros(){
FileWriter fichero = null;
PrintWriter pw = null;
try{
fichero = new FileWriter("c:/VEHICULOS.txt");
pw = new PrintWriter(fichero);
//Inicializamos los objetos
Vehiculos vehi_1 = new Vehiculos(1,1000,"1234A","02/05/2007");
Vehiculos vehi_2 = new Vehiculos(2,500,"1234B","02/04/2004");
Vehiculos vehi_3 = new Vehiculos(3,1500,"1234C","12/03/2006");
Vehiculos vehi_4 = new Vehiculos(4,2000,"1234D","02/01/2003");
Vehiculos vehi_5 = new Vehiculos(5,2500,"1234F","02/04/2006");
//Introducimos los objetos en el archivo VEHICULOS.txt
pw.println(vehi_1.getCodigo()+" "+vehi_1.getCapacidad()+" "+vehi_1.getMatricula()+" "+vehi_1.getFechaAdquisicion());
pw.println(vehi_2.getCodigo()+" "+vehi_2.getCapacidad()+" "+vehi_2.getMatricula()+" "+vehi_2.getFechaAdquisicion());
pw.println(vehi_3.getCodigo()+" "+vehi_3.getCapacidad()+" "+vehi_3.getMatricula()+" "+vehi_3.getFechaAdquisicion());
pw.println(vehi_4.getCodigo()+" "+vehi_4.getCapacidad()+" "+vehi_4.getMatricula()+" "+vehi_4.getFechaAdquisicion());
pw.println(vehi_5.getCodigo()+" "+vehi_5.getCapacidad()+" "+vehi_5.getMatricula()+" "+vehi_5.getFechaAdquisicion());
pw.close();
fichero = new FileWriter("c:/REPOSTAJES.txt");
pw= new PrintWriter(fichero);
Repostajes repos_1 = new Repostajes(1,250,500,10,"02/05/2011");
Repostajes repos_2 = new Repostajes(2,250,500,10,"09/06/2010");
Repostajes repos_3 = new Repostajes(3,250,500,10,"12/07/2011");
Repostajes repos_4 = new Repostajes(4,250,500,10,"15/08/2011");
Repostajes repos_5 = new Repostajes(4,250,500,10,"22/09/2009");
Repostajes repos_6 = new Repostajes(5,250,500,10,"11/10/2008");
Repostajes repos_7 = new Repostajes(5,250,500,10,"02/12/2010");
Repostajes repos_8 = new Repostajes(5,250,500,10,"08/01/2011");
pw.println(repos_1.getCodigo()+" "+repos_1.getKilometraje()+" "+repos_1.getImporte()+" "+ repos_1.getCombustible()+" "+ repos_1.getFechaRepostaje());
pw.println(repos_2.getCodigo()+" "+repos_2.getKilometraje()+" "+repos_2.getImporte()+" "+ repos_2.getCombustible()+" "+ repos_2.getFechaRepostaje());
pw.println(repos_3.getCodigo()+" "+repos_3.getKilometraje()+" "+repos_3.getImporte()+" "+ repos_3.getCombustible()+" "+ repos_3.getFechaRepostaje());
pw.println(repos_4.getCodigo()+" "+repos_4.getKilometraje()+" "+repos_4.getImporte()+" "+ repos_4.getCombustible()+" "+ repos_4.getFechaRepostaje());
pw.println(repos_5.getCodigo()+" "+repos_5.getKilometraje()+" "+repos_5.getImporte()+" "+ repos_5.getCombustible()+" "+ repos_5.getFechaRepostaje());
pw.println(repos_6.getCodigo()+" "+repos_6.getKilometraje()+" "+repos_6.getImporte()+" "+ repos_6.getCombustible()+" "+ repos_6.getFechaRepostaje());
pw.println(repos_7.getCodigo()+" "+repos_7.getKilometraje()+" "+repos_7.getImporte()+" "+ repos_7.getCombustible()+" "+ repos_7.getFechaRepostaje());
pw.println(repos_8.getCodigo()+" "+repos_8.getKilometraje()+" "+repos_8.getImporte()+" "+ repos_8.getCombustible()+" "+ repos_8.getFechaRepostaje());
pw.close();

}catch(Exception e){
e.printStackTrace();
}finally{
try{
if (null != fichero)
fichero.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
public static void main(String[] args) {
GenerarFicheros();
FileReader fr1 = null;
FileReader fr2 = null;
BufferedReader bf1 = null;
BufferedReader bf2 = null;
FileWriter anadirFichero = null;
PrintWriter pw = null;
try{
fr1= new FileReader("c:/VEHICULOS.txt");
bf1= new BufferedReader(fr1);

String Registro1;
String Registro2;
String[] vVehiculos;
String[] vRepostajes;

System.out.println("Dime el nombre del fichero");
Scanner sc = new Scanner(System.in);
String nombreFichero = sc.nextLine();
File NuevoArchivo = new File("c:/"+nombreFichero+".txt");

while((Registro1=bf1.readLine())!=null){
vVehiculos = Registro1.split(" ");
fr2= new FileReader("c:/REPOSTAJES.txt");
bf2= new BufferedReader(fr2);
Vehiculos vehiculo = new Vehiculos();
vehiculo.setMatricula(vVehiculos[2]);
while((Registro2=bf2.readLine())!=null){
vRepostajes = Registro2.split(" ");
if(vVehiculos[0].equals(vRepostajes[0])){
int combustible = Integer.parseInt(vRepostajes[3]);
int distancia = Integer.parseInt(vRepostajes[1]);
double importe = Double.parseDouble(vRepostajes[2]);
vehiculo.setCombustible(vehiculo.getCombustible()+ combustible);
// vehiculo.setKilometraje(vehiculo.getKilometraje()+ distancia);
//vehiculo.setImporte(vehiculo.getImporte() + importe );
//vehiculo.setVecesRepostado(vehiculo.IncrementoVece sRepostado());

}
vRepostajes=null;
vVehiculos=null;
}
anadirFichero = new FileWriter(NuevoArchivo,true);
pw = new PrintWriter(anadirFichero);
pw.println(" Matricula:"+vehiculo.getMatricula()+" @");
pw.println(" Numero Repostajes:"+vehiculo.getVecesRepostado());
pw.println("@ Combustible consumido:"+vehiculo.getCombustible()+" @");
pw.println("@ Distancia recorrida:"+vehiculo.getKilometraje()+" @");
// pw.println("@ Importe facturado:"+vehiculo.getImporte()+" @");

pw.close();

}
bf1.close();
bf2.close();
fr1.close();
fr2.close();
}catch(Exception e){
e.printStackTrace();
}
}

}
Donde señalo en rojo es dnd salta el error.