0

I want to introduce information about cars and keep it in a file. My problem is that when I introduce datas it show me has occurred a NullPointerException where I use "seek" to place the pointer where I want.

I have a Java Class that I use to ask the user what opcion he want to use, for the moment only to add car's data; and other where I want to write car's data in the File that I create in the same


package Tema1AccesoAleatiroFicheros;

import java.io.IOException;
import java.util.Scanner;


public class Vista {

public static void main(String[] args) throws IOException {

    Scanner sc=new Scanner(System.in);

    Controlador coche=new Controlador();

    System.out.println("Eliga opción:");
    System.out.println("1-Ingresar coche:");
    System.out.println("2-Modificar registro:");
    System.out.println("3-Salir.");
    int opcion=sc.nextInt();

    Controlador.crearFichero();

    while (opcion!=3)
    {
        switch(opcion)
        {
            case 1:
                añadir();
                break;
            case 2:
                modificar();
                break;
        }

    System.out.println("Eliga opción:");
    System.out.println("1-Ingresar coche:");
    System.out.println("2-Modificar registro:");
    System.out.println("3-Salir.");
    opcion=sc.nextInt();

    }

}

public static void añadir ()
{
    Scanner sc=new Scanner(System.in);

    String[] array=new String[7];

    System.out.println("Introduzca código:");
    String cod=sc.next();
    System.out.println("Introduzca marca:");
    String marca=sc.next();
    System.out.println("Introduzca modelo:");
    String modelo=sc.next();
    System.out.println("Introduzca matricula:");
    String matricula=sc.next();
    System.out.println("Introduzca potencia en caballos:");
    int potencia=sc.nextInt();
    System.out.println("Introduzca número de plazas:");
    int plazas=sc.nextInt();
    System.out.println("Introduzca número de puertas:");
    int puertas=sc.nextInt();

    array[0]=cod;
    array[1]=marca;
    array[2]=modelo;
    array[3]=matricula;
    array[4]=Integer.toString(potencia);
    array[5]=Integer.toString(plazas);
    array[6]=Integer.toString(puertas);

//        array[3]=new String(cod);
//        array[1]=new String(marca);
//        array[2]=new String(modelo);
//        array[3]=new String(matricula);
//        array[4]=new String(Integer.toString(potencia));
//        array[5]=new String(Integer.toString(plazas));
//        array[6]=new String(Integer.toString(puertas));

    Controlador.añadirCoche(array);
}

public static void modificar ()
{
    Scanner sc=new Scanner(System.in);

    System.out.println("Introduzca código:");
    String codigo=sc.next();

//        if(Controlador.buscarRegistro(codigo)!=-1)
    {
//            int pos=Controlador.buscarRegistro(codigo);

        System.out.println("Introduzca código:");
        String cod=sc.next();
        System.out.println("Introduzca marca:");
        String marca=sc.next();
        System.out.println("Introduzca modelo:");
        String modelo=sc.next();
        System.out.println("Introduzca matricula:");
        String matricula=sc.next();
        System.out.println("Introduzca potencia en caballos:");
        int potencia=sc.nextInt();
        System.out.println("Introduzca número de plazas:");
        int plazas=sc.nextInt();
        System.out.println("Introduzca número de puertas:");
        int puertas=sc.nextInt();

        Modelo mod=new Modelo(cod,marca,modelo,matricula,potencia,plazas,puertas);

//            Controlador.setCoche(pos, mod);
    }

}

}

$

package Tema1AccesoAleatiroFicheros;

import static Tema1AccesoAleatiroFicheros.Controlador.direccion;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Controlador {

static String direccion="C:\\Users\\Noneking2\\Desktop\\prueba.dat";
public static RandomAccessFile flujo;
private static int numRegistros=0;
private static int tamRegistro=7;

public static void crearFichero ()
{
    File comprobar=new File(direccion);
    if(!comprobar.exists())
    {
        try 
        {
            File fichero=new File(direccion);
            RandomAccessFile random=new RandomAccessFile(fichero,"rw");
//                FileOutputStream fileout=new FileOutputStream(fichero);
        } 
        catch (FileNotFoundException ex) 
        {
            Logger.getLogger(Controlador.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

public static void añadirCoche (String [] array)
{
    try 
    {
        flujo.seek(numRegistros*tamRegistro);
    }
//        catch (NullPointerException n)
//        {
//            System.out.println("NullPointerException success");
//        }
    catch (IOException ex) 
    {
        Logger.getLogger(Controlador.class.getName()).log(Level.SEVERE, null, ex);
    }

    for(int i=0;i<array.length;i++)
    {
//            try 
//            {
//                flujo.writeUTF(array[i]);
//            } 
//            catch (IOException ex) 
//            {
//                Logger.getLogger(Controlador.class.getName()).log(Level.SEVERE, null, ex);
//            }
    }
    numRegistros++;
}

}
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
Noneking
  • 11
  • 1

1 Answers1

0

The reason why you're getting a null pointer exception is that you are never assigning a value to flujo, and therefore it remains being null from when you declared it.

This

RandomAccessFile flujo;
                      ^ no value is being assigned

Is basically the same as

RandomAccessFile flujo = null;

Since it is always null, calling a method from the object throws the error.

Also, the following code will not have any effect on the program whatsoever (unless an exception is thrown), since these variables will be tidied up and deleted by the garbage collector as soon as you exit the try-catch block. You might want to initialise them outside of the block or within the class.

    try 
        {
            File fichero=new File(direccion);
            RandomAccessFile random=new RandomAccessFile(fichero,"rw");
             // FileOutputStream fileout=new FileOutputStream(fichero);
        } 

Learn about variable scope here

Radiodef
  • 37,180
  • 14
  • 90
  • 125
SamTebbs33
  • 5,507
  • 3
  • 22
  • 44