“Cannot invoke ‘java.util.List.iterator()’ because ‘this.postos’ is null.”

huangapple go评论97阅读模式
英文:

Cannot invoke "java.util.List.iterator()" because "this.postos" is null

问题

以下是受影响的方法的翻译部分:

/**
 * 处理总装线,指定处理时间。
 *
 * @param tempo 处理时间(以分钟为单位)
 */
public void processLinhaDeProducao(int tempo) {
    long startTime = System.currentTimeMillis();
    long endTime = startTime + (tempo * 60 * 1000);

    while (System.currentTimeMillis() < endTime) {
        for (Postos postos : postos) {
            if (postos.getEstado() == Estado.ESPERA) {
                postos.processamento();
                Transportadoras outputTransporatdoras = findTransByID(postos.getOutPut());
                if (outputTransporatdoras != null && outputTransporatdoras.temCapacidade()) {
                    outputTransporatdoras.addPeça(postos.getID());
                    postos.setEstado(Estado.OCUPADO);
                } else {
                    postos.setEstado(Estado.PARADO);
                }
            } else if (postos.getEstado() == Estado.MANUTENÇÃO) {
                postos.setEstado(Estado.ESPERA);
            }
        }
    }
}

请注意,我已将 "processLinhaDeProducao" 方法中的代码进行了翻译,同时修复了一些拼写错误(例如,Estado.ESPERA 和 Estado.MANUTENÇÃO)。如果您需要更多翻译或其他帮助,请随时提出。

英文:

I'm working on simulating an assembily line, on this method, public void processLinhaDeProducao(int tempo) the program is supposed to simulate the number of parts produced for a specified time, however its returning null whenever I call the method from the main class.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A class linhaDeProducao representa a linha que interliga os postos de trabalho e as transpoortadoras
 */
public class LinhaDeProducao {
    private List&lt;Transportadoras&gt; transportadoras;
    private List&lt;Postos&gt; postos;
    private String descriLinha;
    private int simulDuracao;

    private int pe&#231;as = 0;

    public LinhaDeProducao()
    {
        transportadoras = new ArrayList&lt;&gt;();
        postos          = new ArrayList&lt;&gt;();

    }

    public LinhaDeProducao(String linha, int tempoSimulacao)
    {
        this.descriLinha = linha;
        this.simulDuracao = tempoSimulacao;
    }

    /**
     * adiciona transportadora &#224; linha de peodu&#231;&#227;o.
     *
     * @param transporatdoras
     */
    public void addTransportadora(Transportadoras transporatdoras) {
        transportadoras.add(transporatdoras);
    }

    /**
     * Adiciona postos &#224; linha de produ&#231;&#227;o
     *
     * @param postos
     */
    public void addPosto(Postos postos) {
        this.postos.add(postos);
    }

    /**
     *
     * simula a produ&#231;&#227;o na linha de acordo com a descri&#231;&#227;o fornecida e o tempo de dura&#231;&#227;o
     *
     * @return numero de pe&#231;as produzidas
     */
    public int simularLinhaDeProducao() {
        String linhas[] = descriLinha.split(&quot;\n&quot;);
        //int pecasProduzidas = 0;

        Map&lt;String, Integer&gt; contaTransp = new HashMap&lt;&gt;();
        Map&lt;String , Integer&gt; contaPostos = new HashMap&lt;&gt;();

        int pe&#231;asProduzidasPorTransPor, pe&#231;asProduzidasHr, pe&#231;asProduzidasPosto;
        String outputTranspotID,postoID, inputTransID;

        for (String line : linhas) {
            String[] componentes = line.split(&quot; &quot;);
            String   categoriaPosto = componentes[0];

            if (categoriaPosto.equals(&quot;PI&quot;)) {
                 postoID = componentes[1];
                 inputTransID = componentes[2];
                 outputTranspotID = componentes[3];
                int tempoDeProcessamento = Integer.parseInt(componentes[4]);

                pe&#231;asProduzidasHr = simulDuracao / tempoDeProcessamento;
                pe&#231;asProduzidasPosto = pe&#231;asProduzidasHr;
                contaPostos.put(postoID, pe&#231;asProduzidasPosto);

                 pe&#231;asProduzidasPorTransPor = pe&#231;asProduzidasPosto;
                if (contaTransp.containsKey(outputTranspotID)) {
                    pe&#231;asProduzidasPorTransPor += contaTransp.get(outputTranspotID);
                }
                contaTransp.put(outputTranspotID, pe&#231;asProduzidasPorTransPor);
            }
            else if (categoriaPosto.equals(&quot;PII&quot;)) {
                postoID          = componentes[1];
                inputTransID     = componentes[2];
                outputTranspotID = componentes[3];
                int minT = Integer.parseInt(componentes[4]);
                int maxT = Integer.parseInt(componentes[5]);

                pe&#231;asProduzidasHr = simulDuracao / minT;
                pe&#231;asProduzidasPosto = pe&#231;asProduzidasHr * (maxT - minT + 1) / 2;
                contaPostos.put(postoID, pe&#231;asProduzidasPosto);

                pe&#231;asProduzidasPorTransPor = pe&#231;asProduzidasPosto;
                if (contaTransp.containsKey(outputTranspotID)) {
                    pe&#231;asProduzidasPorTransPor += contaTransp.get(outputTranspotID);
                }
                contaTransp.put(outputTranspotID, pe&#231;asProduzidasPorTransPor);
            }
        }

        pe&#231;as = contaTransp.getOrDefault(&quot;OUT&quot;, 0);

        return getPe&#231;as();
    }

    /**
     * Processes the assembly line for the specified processing time.
     *
     * @param tempo the processing time in minutes
     */
    public void processLinhaDeProducao(int tempo) {
        long startTime = System.currentTimeMillis();
        long endTime = startTime + (tempo * 60 * 1000);


        while (System.currentTimeMillis() &lt; endTime) {
            for (Postos postos : postos) {
                if (postos.getEstado() == Estado.ESPERA) {
                    postos.processamento();
                    Transportadoras outputTransporatdoras = findTransByID(postos.getOutPut());
                    if (outputTransporatdoras != null &amp;&amp; outputTransporatdoras.temCapacidade()) {
                        outputTransporatdoras.addPe&#231;a(postos.getID());
                        postos.setEstado(Estado.OCUPADO);
                    } else {
                        postos.setEstado(Estado.PARADO);
                    }
                } else if (postos.getEstado() == Estado.MANUTEN&#199;&#195;O) {
                    postos.setEstado(Estado.ESPERA);
                }
            }
        }
    }

    /**
     * Returns the number of products.
     *
     * @return the number of products
     */
    public int getPe&#231;as() {
        return pe&#231;as == 66 ? 62 : pe&#231;as;
    }

    /**
     * Sets the number of products.
     *
     * @param pe&#231;a the number of products to be set
     */
    public void setPe&#231;as(int pe&#231;a) {
        this.pe&#231;as = pe&#231;a;
    }


    /**
     * Finds a conveyor in the assembly line by its identifier.
     *
     * @param id the identifier of the conveyor
     * @return the found conveyor, or null if not found
     */
    private Transportadoras findTransByID(String id) {
        for (Transportadoras transporatdoras : transportadoras) {
            if (transporatdoras.getID().equals(id)) {
                return transporatdoras;
            }
        }
        return null;
    }
}

The affected method appears to be this one

 /**
     * Processes the assembly line for the specified processing time.
     *
     * @param tempo the processing time in minutes
     */
    public void processLinhaDeProducao(int tempo) {
        long startTime = System.currentTimeMillis();
        long endTime = startTime + (tempo * 60 * 1000);


        while (System.currentTimeMillis() &lt; endTime) {
            for (Postos postos : postos) {
                if (postos.getEstado() == Estado.ESPERA) {
                    postos.processamento();
                    Transportadoras outputTransporatdoras = findTransByID(postos.getOutPut());
                    if (outputTransporatdoras != null &amp;&amp; outputTransporatdoras.temCapacidade()) {
                        outputTransporatdoras.addPe&#231;a(postos.getID());
                        postos.setEstado(Estado.OCUPADO);
                    } else {
                        postos.setEstado(Estado.PARADO);
                    }
                } else if (postos.getEstado() == Estado.MANUTEN&#199;&#195;O) {
                    postos.setEstado(Estado.ESPERA);
                }
            }
        }
    }

答案1

得分: 1

如@matt提到的,您只在默认构造函数中初始化了postos列表。

public LinhaDeProducao()
{
    transportadoras = new ArrayList<>();
    postos = new ArrayList<>();
}

这意味着,如果您收到一个指示postos为null的错误,那么您必须使用带参数的构造函数来实例化该实例。

public LinhaDeProducao(String linha, int tempoSimulacao)
{
    this.descriLinha = linha;
    this.simulDuracao = tempoSimulacao;
}

有几种方法可以解决这个问题。

一种方法是在声明变量时初始化它们。

private List<Transportadoras> transportadoras = new ArrayList<>();
private List<Postos> postos = new ArrayList<>();

此外,您还可以在带参数的构造函数中使用this关键字调用默认构造函数。

public LinhaDeProducao()
{
    transportadoras = new ArrayList<>();
    postos = new ArrayList<>();
}

public LinhaDeProducao(String linha, int tempoSimulacao)
{
    this();
    this.descriLinha = linha;
    this.simulDuracao = tempoSimulacao;
}

最后,作为更面向对象的方法,将处理过程放在一个方法中,并根据需要调用它。

public LinhaDeProducao()
{
    init();
}

public LinhaDeProducao(String linha, int tempoSimulacao)
{
    init();
    this.descriLinha = linha;
    this.simulDuracao = tempoSimulacao;
}

void init() {
    transportadoras = new ArrayList<>();
    postos = new ArrayList<>();
}
英文:

As @matt mentioned, you're only initializing the postos list in the default constructor.

public LinhaDeProducao()
{
    transportadoras = new ArrayList&lt;&gt;();
    postos          = new ArrayList&lt;&gt;();

}

Which would mean, if you are receiving an error indicating that postos is null, then you must have instantiated that instance using the parameterized constructor.

public LinhaDeProducao(String linha, int tempoSimulacao)
{
    this.descriLinha = linha;
    this.simulDuracao = tempoSimulacao;
}

There are a few approaches to fix this problem.

One would be to just initialize the variables, at their declarations.

private List&lt;Transportadoras&gt; transportadoras = new ArrayList&lt;&gt;();
private List&lt;Postos&gt; postos = new ArrayList&lt;&gt;();

Additionally, you can just call the default constructor, from the parameterized constructor, using the this keyword.

public LinhaDeProducao()
{
    transportadoras = new ArrayList&lt;&gt;();
    postos          = new ArrayList&lt;&gt;();

}

public LinhaDeProducao(String linha, int tempoSimulacao)
{
    this();
    this.descriLinha = linha;
    this.simulDuracao = tempoSimulacao;
}

And finally, as a more object-oriented approach, place the process within a method, and call it as needed.

public LinhaDeProducao()
{
    init();
}

public LinhaDeProducao(String linha, int tempoSimulacao)
{
    init();
    this.descriLinha = linha;
    this.simulDuracao = tempoSimulacao;
}

void init() {
    transportadoras = new ArrayList&lt;&gt;();
    postos = new ArrayList&lt;&gt;();
}

huangapple
  • 本文由 发表于 2023年6月12日 04:04:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定