我在线程" main"中得到了一个"异常。显示java.lang.NullPointerException"并且无法找出原因

时间:2018-04-10 16:19:07

标签: java exception netbeans nullpointerexception

我正在做一个学校项目,并且我一直在线程"主要"显示java.lang.NullPointerException"错误的类型,我无法弄清楚原因。我已经阅读了代码,但我找不到答案。这是代码:

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);
        }  
    } }

根据NetBeans,错误位于第33行,即:

  

array.append(将String.valueOf(的Numerov));

我很感激能得到任何帮助。

1 个答案:

答案 0 :(得分:0)

PrintStream array = null;

然后你正在尝试

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

如果!new File("arraylist.txt").exists()返回false怎么办?这意味着您已经array = null 所以你要打电话

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

所以我们有类似的东西

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

这是正常的NullPointerException。 阅读this以了解有关NullPointerException

的更多信息