Help with pointer error in Java

-1

I'm doing a Priority Bank Queue project,

I did everything the way I learned, but this is giving some null pointer error that I can not understand, could anyone please help me understand where I'm going wrong?

Project link:
Java Project

class controller:

package bean;

import classes.Funcionario;
import classes.Fila;
import classes.Pessoa;


public class Bean {

 Fila filaNormal = new Fila();

 Fila filaPreferencial = new Fila();

 public boolean retirarSenha(Pessoa pessoa){

    boolean validaSenha = false;
    if(!pessoa.getNome().isEmpty() || !pessoa.getCpf().isEmpty()){
        if(pessoa.isTipoDeSenha()){
            filaPreferencial.enfileirar(pessoa);   
        }else{
            filaNormal.enfileirar(pessoa);
        }
        validaSenha = true;              
    }

    return validaSenha;
 }

 public Object retirarSenha(){
     if(filaPreferencial.temProximo()){
         return filaPreferencial.desenfileirar();
     }else{     
     return filaNormal.desenfileirar(); 
     }
 }
} // fim Bean

class that implements queue:

package classes;


public class Fila {

    public Fila() {
    }

    public Fila(Pessoa[] fila, int ponteiroInicio, int ponteiroPercorrer, int     
ponteiroFim, int total) {
        this.fila = new Pessoa[10];
        this.ponteiroInicio = 0;
        this.ponteiroPercorrer = 0;
        this.ponteiroFim = 0;
        this.total = 0;
    }

    public Pessoa fila[];
    private int ponteiroInicio;
    private int ponteiroPercorrer;
    private int ponteiroFim;
    private int total;


    public int getPonteiroInicio() {
        return ponteiroInicio;
    }

    public void setPonteiroInicio(int ponteiroInicio) {
        this.ponteiroInicio = ponteiroInicio;
    }

    public int getPonteiroPercorrer() {
        return ponteiroPercorrer;
    }

    public void setPonteiroPercorrer(int ponteiroPercorrer) {
        this.ponteiroPercorrer = ponteiroPercorrer;
    }

    public int getPonteiroFim() {
        return ponteiroFim;
    }

    public void setPonteiroFim(int ponteiroFim) {
         this.ponteiroFim = ponteiroFim;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    @Override
    public String toString() {
        return "Fila{" + "fila=" + fila + ", ponteiroInicio=" + 
ponteiroInicio + ", ponteiroPercorrer=" + ponteiroPercorrer + ",         
ponteiroFim=" + ponteiroFim + ", total=" + total + '}';
    }

    public void enfileirar(Pessoa cliente){
     if(cheia()){
         throw new RuntimeException("Fila cheia");
     }

    fila[getPonteiroFim()] = cliente;
        setTotal(total++);
    }

    public Pessoa desenfileirar(){
        if(vazia()){
            throw new RuntimeException("Fila vazia");
        }
        Pessoa cliente = fila[getPonteiroInicio()];
        setPonteiroInicio(ponteiroInicio++);
        setTotal(total--);
        return cliente;
    }

    public boolean cheia(){
    return this.total == this.fila.length;
    }

    public boolean vazia(){
    return this.total == 0;
    }

    public boolean temProximo(){
        return this.fila[getPonteiroInicio() + 1] != null;    
    }

}

class of person:

package classes;

public class Pessoa {

    public Pessoa() {
    }

    private String nome, cpf;
    private int senha;
    private boolean tipoDeSenha;

    public Pessoa(String nome, String cpf, int senha, boolean tipoDeSenha) {
        this.nome = nome;
        this.cpf = cpf;
        this.senha = senha;
        this.tipoDeSenha = tipoDeSenha;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getCpf() {
        return cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public int getSenha() {
        return senha;
    }

    public void setSenha(int senha) {
        this.senha = senha;
    }

    public boolean isTipoDeSenha() {
        return tipoDeSenha;
    }

    public void setTipoDeSenha(boolean tipoDeSenha) {
        this.tipoDeSenha = tipoDeSenha;
    }

    @Override
    public String toString() {
        return "Pessoa{" + "nome=" + nome + ", cpf=" + cpf + ", senha=" +     
senha + ", tipoDeSenha=" + tipoDeSenha + '}';
    }
}

Action in a Jframe where the client fills in the data, the object to be queued is created:

private void btnRetirarSenhaActionPerformed(java.awt.event.ActionEvent evt) {                                                

    Pessoa pessoa = new Pessoa();
    String nomeCliente = txtNome.getText();
    String cpf = txtCPF.getText();
    boolean preferencial = jCheckPreferencial.isSelected();
    int senha = 0; // substituit por método criador de senha; 
    pessoa.setNome(nomeCliente);
    pessoa.setCpf(cpf);
    pessoa.setTipoDeSenha(preferencial);
    pessoa.setSenha(senha);
    Bean b = new Bean();
    boolean validarSenha = b.retirarSenha(pessoa);

    if(validarSenha == false){
       lblErroSenha.setText("Necessário preencher todos os campos "
                                        + "indicados com o: *");
   }
} 

Action in jFrame where an object is de-framed and shown on the screen:

private void btnChamarSenhaActionPerformed(java.awt.event.ActionEvent evt) {                                               
    Bean b = new Bean();
    lblDados.setText(b.retirarSenha().toString());

}

Error when clicked Create object button:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at classes.Fila.cheia(Fila.java:89)
at classes.Fila.enfileirar(Fila.java:70)
at bean.Bean.retirarSenha(Bean.java:46)
at view.TelaCliente.btnRetirarSenhaActionPerformed(TelaCliente.java:119)
at view.TelaCliente.access$000(TelaCliente.java:15)
at view.TelaCliente$1.actionPerformed(TelaCliente.java:50)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Error when I click on the button that should dequeue and show the deprecated object:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at classes.Fila.temProximo(Fila.java:97)
    at bean.Bean.retirarSenha(Bean.java:55)
    at     view.TelaFuncionario.btnChamarSenhaActionPerformed(TelaFuncionario.java:99)
    at view.TelaFuncionario.access$000(TelaFuncionario.java:13)
    at view.TelaFuncionario$1.actionPerformed(TelaFuncionario.java:43)
    at     javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at     javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at     javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:40    2)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at     javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.    java:252)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at     java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege    (ProtectionDomain.java:80)
    at     java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege    (ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at     java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege    (ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at     java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:    201)
    at     java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116    )
    at     java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:    105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    
asked by anonymous 27.05.2018 / 00:50

1 answer

-1

The problem is that you have two constructors in the Queue class:

public Fila() {
}

public Fila(Pessoa[] fila, int ponteiroInicio, int ponteiroPercorrer, int ponteiroFim, int total) {
    this.fila = new Pessoa[10];
    this.ponteiroInicio = 0;
    this.ponteiroPercorrer = 0;
    this.ponteiroFim = 0;
    this.total = 0;
}

Note that only the second constructor initializes the variable this.fila .

In class Bean , you invoke only the first constructor - which does not initialize the variable this.fila :

Fila filaNormal = new Fila();
Fila filaPreferencial = new Fila();

It happens that when you invoke methods temProximo , cheia or vazia , you try to access the this.fila variable. It turns out that it has not yet been initialized (due to the constructor used). This ends up causing NullPointerException :

public boolean temProximo(){
    return this.fila[getPonteiroInicio() + 1] != null;    
}

One solution to your problem is to correctly implement the two constructors:

public Fila() {
    this.fila = new Pessoa[10];
    this.ponteiroInicio = 0;
    this.ponteiroPercorrer = 0;
    this.ponteiroFim = 0;
    this.total = 0;
}

public Fila(Pessoa[] fila, int ponteiroInicio, int ponteiroPercorrer, int ponteiroFim, int total) {
    this.fila = fila;
    this.ponteiroInicio = ponteiroInicio;
    this.ponteiroPercorrer = ponteiroPercorrer;
    this.ponteiroFim = ponteiroFim;
    this.total = total;
}
    
28.05.2018 / 14:36