Java POO, problems running the program

Asked

Viewed 13 times

0

Exception in thread "main" java.lang.NullPointerException
    at Estudante.Estudantes.<init>(Estudantes.java:17)
    at Estudante.ArrayEstudante.lerFichcriarArray(ArrayEstudante.java:35)
    at Estudante.GerirEstudantes.main(GerirEstudantes.java:10)
C:\Users\Nhatsa\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 9 seconds)
//CLASSE EXECUTAVEL

package Estudante;

import java.io.*;

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

            ArrayEstudante est = new ArrayEstudante();
            est.lerFichcriarArray();   //erro3(GerirEstudantes.java:10)
            System.out.println("LISTA DE ESTUDANTES\n+++++++++++++--------------------+++++++++++++++++++");
            System.out.println(est);
            /*
            System.out.println("Turma ordenada por ALtura\n*************+++++++++++++++++++++++******************");
            System.out.println(est);
            */

        }
}
//LEITuRA DE ARRAYFICHEIRO

package Estudante;


import java.io.*;
import java.util.StringTokenizer;

    public class ArrayEstudante{
        Validacoes valida = new Validacoes();
        private Estudantes[] turma;
        private int cont;

    public ArrayEstudante(){
        turma = new Estudantes[29];
       // cont = 0;

    }
    public void lerFichcriarArray()throws IOException{
        StringTokenizer umaCadeia;
        String umaLinha = "", nom;
        char sex;
        float alt;
        String nomFich = "turma.txt";
        int qde;

        try{
            FileReader fr = new FileReader(nomFich);
            BufferedReader fichIn = new BufferedReader(fr);
            umaLinha = fichIn.readLine();
            while(umaLinha != null){
                umaCadeia = new StringTokenizer(umaLinha,";");
                nom = umaCadeia.nextToken();
                sex = umaCadeia.nextToken().toUpperCase().charAt(0);
                alt = Float.parseFloat(umaCadeia.nextToken());
                turma[29] = new Estudantes();  //erro2(ArrayEstudante.java:35)
                umaLinha = fichIn.readLine();

            }
            fichIn.close();
        }catch(FileNotFoundException fn){
            System.out.println("Ficheiro nao Encontrado! Verifique a sua existencia.");
        }catch(NumberFormatException | IOException nn){
            System.out.println(nn.getMessage());
        }

    }
/*
    public Validacoes getValida() {
        return valida;
    }

    public Estudantes[] getTurma() {
        return turma;
    }

    public int getCont() {
        return cont;
    }
*/    
//    @Override;
   /* public String toString(){
        System.out.pritln("nome\tSexo\tAltura\n"+nome+"\t"+sexo+"\t"+altura);
    }*/

   /* public ArrayEstudante(Estudantes[] turm, int cont) {
        this.turma = turm;
        this.cont = cont;
    }*/
    public float maiorAlt(){
        float aux = 0;
        for(int a = 1; a <= 29; a++){
            for(int b = 1; b <= 29; b++){
                if(turma[a].getAltura() > turma[b].getAltura()) {
                    aux = turma[a].getAltura();
                    turma[a] = turma[b];
                    aux = turma[b].getAltura();
                }
            }        
        }
        return aux;
    }

}
//CLASSE ESTUDANTE/PRICIPAL
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Estudante;

import java.io.*;

    public class Estudantes {
        private Validacoes vd;
        private String nome;
        private char sexo;
        private float altura;

    public Estudantes()throws IOException{
        nome = vd.validarString((short)3, (short)25, "nome: "); // erro1(Estudantes.java:17)
        sexo = vd.validarChar("Informe o sexo do estudante: ");
        altura = vd.validarFloat((float)1.0, (float)2.0, "Informe a Altura: ");
    }
   /* public float maiorAlt(){

    }*/

    public String getNome() {
        return nome;
    }

    public char getSexo() {
        return sexo;
    }

    public float getAltura() {
        return altura;
    }
    public String toString(){
        return nome + "\t" + sexo + "\t" + altura;
    }    

}
  • At the beginning of its Estudantes, did not miss a vd = new Validacoes(); or something similar? Anyway, at the beginning of this builder, vd is null, soon, make a vd.validarString(...) will give NullPointerException.

  • Thanks a lot, the tip worked was missing {Vd = new Validations();}

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.