-1

I'm doing a school project, and I keep getting a "Exception in thread "main" java.lang.NullPointerException" type of error and I can't figure out why. I've read the code but I can't find the answer. This is the code:

package trabajopractico_1;

import java.util.ArrayList; import java.io.*; import java.util.logging.Level; import java.util.logging.Logger; import java.util.Scanner;

public class code {

    public void metodo(){
        ArrayList<Integer> metodoVolatil = new ArrayList();
        ArrayList<Integer> metodoNoVolatil = new ArrayList();
        Scanner teclado = new Scanner(System.in);
        int numeroV;
        int numeroNV;
        int resultadosV;
        int resultadosNV;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintStream array = null;
        PrintStream resultadosPS = null;
        PrintStream errorPS = null;
        try{

            if (!new File("arraylist.txt").exists()) {
                array = new PrintStream(new File("arraylist.txt"));
            }

            for (int i = 0; i < 5; i++) {
                System.out.println("Ingrese 5 numeros para almacenar en el ArrayList.txt: ");
                numeroV = Integer.valueOf(br.readLine());
                metodoVolatil.add(numeroV);
                array.append(String.valueOf(numeroV));
                array.println();
            }

            for (int i = 0; i < 5; i++) {
                System.out.println("Ingrese 5 numeros para almacenar en el ArrayList: ");
                numeroNV = teclado.nextInt();
                metodoNoVolatil.add(numeroNV);
            }

            resultadosPS = new PrintStream(new File("resultados.txt")); 
            errorPS = new PrintStream(new File("error.txt"));

            for (int i = 0; i < metodoVolatil.size(); i++) {
                resultadosV = metodoVolatil.get(i) / 3;
                System.out.println("Los resultados de la division de los valores en el ArrayList son: ");
                System.out.println(resultadosV);
            }           

            for (int i = 0; i < metodoNoVolatil.size(); i++) {
                resultadosNV = metodoNoVolatil.get(i) / 3;
                resultadosPS.append(String.valueOf(resultadosNV));
                resultadosPS.println();
            }  
        }

        catch(ArithmeticException e){
            System.err.println("Ah ocurrido un error en la division");            
        } 

        catch (FileNotFoundException e) {
            Logger.getLogger(code.class.getName()).log(Level.SEVERE, null, e);  
        }

        catch (IOException e) {
            Logger.getLogger(code.class.getName()).log(Level.SEVERE, null, e);
        }  
    } }

Acording to NetBeans, the error is located on line 33, which is this:

array.append(String.valueOf(numeroV));

I would be so grateful to receive any help.

1 Answers1

0
PrintStream array = null;

then you are trying

if (!new File("arraylist.txt").exists()) {
    array = new PrintStream(new File("arraylist.txt"));
}

what if !new File("arraylist.txt").exists() returns false? This mean that you've array = null so then you are calling

array.append(String.valueOf(numeroV));

so we have something like this

PrintStream array = null;
array.append(something);

this is normal that you have NullPointerException. read this to know more about NullPointerException