Estou a tentar ler o arquivo .txt, selecionar a coluna “costumerid” e contar quantos tipos diferentes existem, porém o programa apresenta-me este erro: ” Exception in thread “main” java.lang.NullPointerException at com.nayana.exercicio1.methods.Methods.customerId(Methods.java:95) at com.nayana.exercicio1.Exercicio1.main(Exercicio1.java:18) ” Alguém consegue me ajudar?
Código Methods:
package com.nayana.exercicio1.methods; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Date; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.HashSet; public class Methods { private BufferedReader file; private HashSet<String> customer; private int lines; private String[] columns; public String line; private String string; //INPUT PARA O ARQUIVOO A SER LIDO public BufferedReader getFile() { return file; } public void setFile(BufferedReader file) throws FileNotFoundException { this.file = file; } //CONTAR NÚMERO DE LINHAS EXISTENTES public int getLines() { return lines; } public String getLine() { return line; } public void setLines(int lines) throws IOException { this.lines = lines; } public String[] getColumns() { return columns; } public void setColumns(String[] columns) { this.columns = columns; columns = ((String)line).split(";"); } public void setLine(String line) throws IOException { this.line = file.readLine(); } public void contarLinhas() throws Exception { while((line = file.readLine()) !=null) { setColumns(columns); lines++;} } //CONTAR QUANTOS COSTUMERID DIFERENTES EXISTEM public HashSet<String> getCustomer() { return customer; } public void setCustomer(HashSet<String> customer) throws IOException { this.customer = customer; customer = new HashSet<String>(); } public void customerId() throws Exception { while((line = file.readLine()) !=null) { setColumns(columns); HashSet<String> customer = new HashSet<String>(); customer.add(columns[4]); customer.size(); } } //MOSTRAR NA TELA public void status(){ System.out.println("\nO número total de linhas é: " + getLines()); System.out.println("\nO número total de CustomerId é: " + getCustomer()); } }
Código Main:
package com.nayana.exercicio1; import java.io.BufferedReader; import java.io.FileReader; import com.nayana.exercicio1.methods.Methods; public class Exercicio1 { public static void main(String[] args) throws Exception { Methods exercicio = new Methods(); exercicio.setFile(new BufferedReader(new FileReader("C:\Users\nayan\Downloads\orders_04_20_07.txt"))); //exercicio.contarLinhas(); exercicio.customerId(); exercicio.status(); } }