Oi, gente! Fiz um exercício e cheguei na solução, mas estou com problemas:
O exercício consiste em determinar os 10 maiores estados brasileiros por extensão territorial. O código funciona e roda. O problema é que quando rodo o JUnity
recebo NullPointerException
. Hoje, uma pessoa me ensinou a inserir um breakpoint
na linhas exibidas pelo JUnity
e rodar no modo Debug
e o erro de fato apareceu, problema é que não sei como resolver.
O Debug
aponta para variáveis nulas dentro da classe State, quando uso a palavra reservada this
e no método compare
. Só que não sei como contornar esse problema.
Alguém pode me ajudar? Desde já, muito obrigada!
Seguem os códigos.
Obs: Cada código está em um arquivo diferente:
Código 1 - Classe Principal:
package challenge;
import challenge.SimpleReadCSV;
import challenge.State;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main{
static List<State> state = new ArrayList<State>();
static Double[] Uf = new Double[27];
static String[] name = new String[27];
public List<State> listThe10largestStatesInBrazil() {
State st = new State();
for(int i = 0; i < 10; i++) {
state.add(new State(Uf[i], name[i]));
}
Collections.sort(state, st.reversed());
return state;
}
public static void StateResults() {
System.out.printf("-------------------------------------------------\n");
System.out.printf(" Os 10 maiores estados brasileiros - (1.000 Km\u00B2)\n");
System.out.printf("-------------------------------------------------\n");
System.out.printf("| Nº | Nome do Estado | \006rea |\n");
System.out.printf("-------------------------------------------------\n");
for(int i = 0; i < 10; i++){
System.out.printf("| %02d | %-18s\t | %s\t|\n",
i+1, state.get(i).getName(),
state.get(i).getUf().toString().replace(".", ","));
}System.out.printf("-------------------------------------------------\n");
}
public static void main(String[] args) {
Main objectMain = new Main();
String doc = "EstadosBrasileirosPorOrdemAlfabética.csv";
SimpleReadCSV rdFile = new SimpleReadCSV(doc);
rdFile.reading();
Uf = rdFile.getUf();
name = rdFile.getName();
objectMain.listThe10largestStatesInBrazil();
StateResults();
}
}
Código 2 - Classe State:
package challenge;
import java.util.Comparator;
public class State implements Comparator<State>{
private Double Uf;
private String name;
State() {
}
public State(Double Uf, String name) {
this.Uf = Uf;
this.name = name;
}
public Double getUf() {
return Uf;
}
public void setUf(Double uf) {
Uf = uf;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compare(State o1, State o2) {
return Double.compare(o1.getUf(), o2.getUf()); //Essa linha é exibida no debugg com erro.
}
}
Sobre a classe State, o debug
aponta esses erros e eu não sei o que posso fazer para resolver isso:
Código 3 - Classe SimpleReadCSV:
package challenge;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class SimpleReadCSV {
private String arquivo;
private String[] column;
private String[] name = new String[27];
private Double[] Uf = new Double[27];
public SimpleReadCSV(String arquivo) {
this.arquivo = arquivo;
}
public String getArquivo() {
return arquivo;
}
public void setArquivo(String arquivo) {
this.arquivo = arquivo;
}
public String[] getColuna() {
return column;
}
public void setColumn(String[] column) {
this.column = column;
}
public String[] getName() {
return name;
}
public void setNome(String[] name) {
this.name = name;
}
public Double[] getUf() {
return Uf;
}
public void setUf(Double[] Uf) {
this.Uf = Uf;
}
public void reading() {
try {
System.setIn(new FileInputStream(new File(arquivo)));
} catch(FileNotFoundException e) {
System.out.println("Arquivo não encontrado!");
}
int i = -1;
Scanner read = new Scanner(System.in);
String line;
while(read.hasNext()) {
i++;
line = read.nextLine();
column = line.split(";");
name[i] = column[0];
Uf[i] = Double.parseDouble(column[1].replace(",", "."));
}
read.close();
}
}
Arquivo CSV:
EstadosBrasileirosPorOrdemAlfabética.csv (468 Bytes)