英文:
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<Transportadoras> transportadoras;
    private List<Postos> postos;
    private String descriLinha;
    private int simulDuracao;
    private int peças = 0;
    public LinhaDeProducao()
    {
        transportadoras = new ArrayList<>();
        postos          = new ArrayList<>();
    }
    public LinhaDeProducao(String linha, int tempoSimulacao)
    {
        this.descriLinha = linha;
        this.simulDuracao = tempoSimulacao;
    }
    /**
     * adiciona transportadora à linha de peodução.
     *
     * @param transporatdoras
     */
    public void addTransportadora(Transportadoras transporatdoras) {
        transportadoras.add(transporatdoras);
    }
    /**
     * Adiciona postos à linha de produção
     *
     * @param postos
     */
    public void addPosto(Postos postos) {
        this.postos.add(postos);
    }
    /**
     *
     * simula a produção na linha de acordo com a descrição fornecida e o tempo de duração
     *
     * @return numero de peças produzidas
     */
    public int simularLinhaDeProducao() {
        String linhas[] = descriLinha.split("\n");
        //int pecasProduzidas = 0;
        Map<String, Integer> contaTransp = new HashMap<>();
        Map<String , Integer> contaPostos = new HashMap<>();
        int peçasProduzidasPorTransPor, peçasProduzidasHr, peçasProduzidasPosto;
        String outputTranspotID,postoID, inputTransID;
        for (String line : linhas) {
            String[] componentes = line.split(" ");
            String   categoriaPosto = componentes[0];
            if (categoriaPosto.equals("PI")) {
                 postoID = componentes[1];
                 inputTransID = componentes[2];
                 outputTranspotID = componentes[3];
                int tempoDeProcessamento = Integer.parseInt(componentes[4]);
                peçasProduzidasHr = simulDuracao / tempoDeProcessamento;
                peçasProduzidasPosto = peçasProduzidasHr;
                contaPostos.put(postoID, peçasProduzidasPosto);
                 peçasProduzidasPorTransPor = peçasProduzidasPosto;
                if (contaTransp.containsKey(outputTranspotID)) {
                    peçasProduzidasPorTransPor += contaTransp.get(outputTranspotID);
                }
                contaTransp.put(outputTranspotID, peçasProduzidasPorTransPor);
            }
            else if (categoriaPosto.equals("PII")) {
                postoID          = componentes[1];
                inputTransID     = componentes[2];
                outputTranspotID = componentes[3];
                int minT = Integer.parseInt(componentes[4]);
                int maxT = Integer.parseInt(componentes[5]);
                peçasProduzidasHr = simulDuracao / minT;
                peçasProduzidasPosto = peçasProduzidasHr * (maxT - minT + 1) / 2;
                contaPostos.put(postoID, peçasProduzidasPosto);
                peçasProduzidasPorTransPor = peçasProduzidasPosto;
                if (contaTransp.containsKey(outputTranspotID)) {
                    peçasProduzidasPorTransPor += contaTransp.get(outputTranspotID);
                }
                contaTransp.put(outputTranspotID, peçasProduzidasPorTransPor);
            }
        }
        peças = contaTransp.getOrDefault("OUT", 0);
        return getPeç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() < 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);
                }
            }
        }
    }
    /**
     * Returns the number of products.
     *
     * @return the number of products
     */
    public int getPeças() {
        return peças == 66 ? 62 : peças;
    }
    /**
     * Sets the number of products.
     *
     * @param peça the number of products to be set
     */
    public void setPeças(int peça) {
        this.peças = peç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() < 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);
                }
            }
        }
    }
答案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<>();
    postos          = new ArrayList<>();
}
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<Transportadoras> transportadoras = new ArrayList<>();
private List<Postos> postos = new ArrayList<>();
Additionally, you can just call the default constructor, from the parameterized constructor, using the this keyword.
public LinhaDeProducao()
{
    transportadoras = new ArrayList<>();
    postos          = new ArrayList<>();
}
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<>();
    postos = new ArrayList<>();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论