Help with Java Pointer Error

Asked

Viewed 118 times

-1

I’m doing a Bank Queue project with priority,

I did everything the way I learned, but this giving some null pointer error that I’m not able to understand, could someone please help me understand where I’m going wrong?

Project link:
Java Project

controller class:

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 the 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 the data, is created the object that will be lined up:

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 unrolled 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 cling to the button that should unroll and show the unranked 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)
  • 1

    and please provide a [mcve] so that we can execute the code and help you solve the problem.

  • Failed to provide an executable example, otherwise it is difficult to find the problem.

  • I put the errors, as for the referenced topic, I think I put everything necessary to be able to evaluate the application and nothing irrelevant, but I am very early so I apologize from now on if I have unnecessary code.

  • 1

    nullpointer almost always needs to test code flow. How to analyze code flow without running? Ai complica ne. You need to make life easier for those who want to help you too, plus a code involving 3 different classes.

  • If I share the project on google drive is enough for that? Excuse the amount of things not done.

  • Henrique, Voce can access the link I mentioned in the first comment, it has guidelines on how to create a simple example that can be tested.

  • I read it, but for me everything I put is necessary to verify the error.

  • How do you verify a flow error in a code that isn’t even testable? I’m trying to guide you because I want to help you, but if you don’t want to work hard to be helped, it’s hard.

  • I’m struggling, I’m just not really understanding what’s missing. I read the whole topic, but I don’t know how to classify what is testable or not, I know the basics of POO just for now.

  • I put the project on google drive and shared the link, that’s what was missing @Articuno ?

  • Try creating a new project and paste all this code and see if it will run. It will not, that you need to do, remove dependencies and provide only the testable relevant code, where it is possible to execute and simulate this queue problem.

  • @Sincerely and honestly I have no idea how to do, could please help me giving the directions?

  • 2

    Create a simple window, with the basic to simulate the queue, and then add the buttons that have the listeners that cause the error. See? It’s not that complicated.

  • Good afternoon, I did as you suggested, I started from scratch doing step by step and I got, much fought!

Show 9 more comments

1 answer

-1


The problem is that you have two builders in the Fila 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 turns out that when you invoke the methods temProximo, cheia or vazia, you try to access the variable this.fila. It turns out that it has not yet been initialized (due to the constructor used). This ends up causing the NullPointerException:

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

A solution to your problem is to correctly implement both 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;
}
  • Thank you very much, I got it some other way but I will use your explanation to analyze the code at the weekend.

Browser other questions tagged

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