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

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

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

问题

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

  1. /**
  2. * 处理总装线,指定处理时间。
  3. *
  4. * @param tempo 处理时间(以分钟为单位)
  5. */
  6. public void processLinhaDeProducao(int tempo) {
  7. long startTime = System.currentTimeMillis();
  8. long endTime = startTime + (tempo * 60 * 1000);
  9. while (System.currentTimeMillis() < endTime) {
  10. for (Postos postos : postos) {
  11. if (postos.getEstado() == Estado.ESPERA) {
  12. postos.processamento();
  13. Transportadoras outputTransporatdoras = findTransByID(postos.getOutPut());
  14. if (outputTransporatdoras != null && outputTransporatdoras.temCapacidade()) {
  15. outputTransporatdoras.addPeça(postos.getID());
  16. postos.setEstado(Estado.OCUPADO);
  17. } else {
  18. postos.setEstado(Estado.PARADO);
  19. }
  20. } else if (postos.getEstado() == Estado.MANUTENÇÃO) {
  21. postos.setEstado(Estado.ESPERA);
  22. }
  23. }
  24. }
  25. }

请注意,我已将 "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.

  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. /**
  6. * A class linhaDeProducao representa a linha que interliga os postos de trabalho e as transpoortadoras
  7. */
  8. public class LinhaDeProducao {
  9. private List&lt;Transportadoras&gt; transportadoras;
  10. private List&lt;Postos&gt; postos;
  11. private String descriLinha;
  12. private int simulDuracao;
  13. private int pe&#231;as = 0;
  14. public LinhaDeProducao()
  15. {
  16. transportadoras = new ArrayList&lt;&gt;();
  17. postos = new ArrayList&lt;&gt;();
  18. }
  19. public LinhaDeProducao(String linha, int tempoSimulacao)
  20. {
  21. this.descriLinha = linha;
  22. this.simulDuracao = tempoSimulacao;
  23. }
  24. /**
  25. * adiciona transportadora &#224; linha de peodu&#231;&#227;o.
  26. *
  27. * @param transporatdoras
  28. */
  29. public void addTransportadora(Transportadoras transporatdoras) {
  30. transportadoras.add(transporatdoras);
  31. }
  32. /**
  33. * Adiciona postos &#224; linha de produ&#231;&#227;o
  34. *
  35. * @param postos
  36. */
  37. public void addPosto(Postos postos) {
  38. this.postos.add(postos);
  39. }
  40. /**
  41. *
  42. * 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
  43. *
  44. * @return numero de pe&#231;as produzidas
  45. */
  46. public int simularLinhaDeProducao() {
  47. String linhas[] = descriLinha.split(&quot;\n&quot;);
  48. //int pecasProduzidas = 0;
  49. Map&lt;String, Integer&gt; contaTransp = new HashMap&lt;&gt;();
  50. Map&lt;String , Integer&gt; contaPostos = new HashMap&lt;&gt;();
  51. int pe&#231;asProduzidasPorTransPor, pe&#231;asProduzidasHr, pe&#231;asProduzidasPosto;
  52. String outputTranspotID,postoID, inputTransID;
  53. for (String line : linhas) {
  54. String[] componentes = line.split(&quot; &quot;);
  55. String categoriaPosto = componentes[0];
  56. if (categoriaPosto.equals(&quot;PI&quot;)) {
  57. postoID = componentes[1];
  58. inputTransID = componentes[2];
  59. outputTranspotID = componentes[3];
  60. int tempoDeProcessamento = Integer.parseInt(componentes[4]);
  61. pe&#231;asProduzidasHr = simulDuracao / tempoDeProcessamento;
  62. pe&#231;asProduzidasPosto = pe&#231;asProduzidasHr;
  63. contaPostos.put(postoID, pe&#231;asProduzidasPosto);
  64. pe&#231;asProduzidasPorTransPor = pe&#231;asProduzidasPosto;
  65. if (contaTransp.containsKey(outputTranspotID)) {
  66. pe&#231;asProduzidasPorTransPor += contaTransp.get(outputTranspotID);
  67. }
  68. contaTransp.put(outputTranspotID, pe&#231;asProduzidasPorTransPor);
  69. }
  70. else if (categoriaPosto.equals(&quot;PII&quot;)) {
  71. postoID = componentes[1];
  72. inputTransID = componentes[2];
  73. outputTranspotID = componentes[3];
  74. int minT = Integer.parseInt(componentes[4]);
  75. int maxT = Integer.parseInt(componentes[5]);
  76. pe&#231;asProduzidasHr = simulDuracao / minT;
  77. pe&#231;asProduzidasPosto = pe&#231;asProduzidasHr * (maxT - minT + 1) / 2;
  78. contaPostos.put(postoID, pe&#231;asProduzidasPosto);
  79. pe&#231;asProduzidasPorTransPor = pe&#231;asProduzidasPosto;
  80. if (contaTransp.containsKey(outputTranspotID)) {
  81. pe&#231;asProduzidasPorTransPor += contaTransp.get(outputTranspotID);
  82. }
  83. contaTransp.put(outputTranspotID, pe&#231;asProduzidasPorTransPor);
  84. }
  85. }
  86. pe&#231;as = contaTransp.getOrDefault(&quot;OUT&quot;, 0);
  87. return getPe&#231;as();
  88. }
  89. /**
  90. * Processes the assembly line for the specified processing time.
  91. *
  92. * @param tempo the processing time in minutes
  93. */
  94. public void processLinhaDeProducao(int tempo) {
  95. long startTime = System.currentTimeMillis();
  96. long endTime = startTime + (tempo * 60 * 1000);
  97. while (System.currentTimeMillis() &lt; endTime) {
  98. for (Postos postos : postos) {
  99. if (postos.getEstado() == Estado.ESPERA) {
  100. postos.processamento();
  101. Transportadoras outputTransporatdoras = findTransByID(postos.getOutPut());
  102. if (outputTransporatdoras != null &amp;&amp; outputTransporatdoras.temCapacidade()) {
  103. outputTransporatdoras.addPe&#231;a(postos.getID());
  104. postos.setEstado(Estado.OCUPADO);
  105. } else {
  106. postos.setEstado(Estado.PARADO);
  107. }
  108. } else if (postos.getEstado() == Estado.MANUTEN&#199;&#195;O) {
  109. postos.setEstado(Estado.ESPERA);
  110. }
  111. }
  112. }
  113. }
  114. /**
  115. * Returns the number of products.
  116. *
  117. * @return the number of products
  118. */
  119. public int getPe&#231;as() {
  120. return pe&#231;as == 66 ? 62 : pe&#231;as;
  121. }
  122. /**
  123. * Sets the number of products.
  124. *
  125. * @param pe&#231;a the number of products to be set
  126. */
  127. public void setPe&#231;as(int pe&#231;a) {
  128. this.pe&#231;as = pe&#231;a;
  129. }
  130. /**
  131. * Finds a conveyor in the assembly line by its identifier.
  132. *
  133. * @param id the identifier of the conveyor
  134. * @return the found conveyor, or null if not found
  135. */
  136. private Transportadoras findTransByID(String id) {
  137. for (Transportadoras transporatdoras : transportadoras) {
  138. if (transporatdoras.getID().equals(id)) {
  139. return transporatdoras;
  140. }
  141. }
  142. return null;
  143. }
  144. }

The affected method appears to be this one

  1. /**
  2. * Processes the assembly line for the specified processing time.
  3. *
  4. * @param tempo the processing time in minutes
  5. */
  6. public void processLinhaDeProducao(int tempo) {
  7. long startTime = System.currentTimeMillis();
  8. long endTime = startTime + (tempo * 60 * 1000);
  9. while (System.currentTimeMillis() &lt; endTime) {
  10. for (Postos postos : postos) {
  11. if (postos.getEstado() == Estado.ESPERA) {
  12. postos.processamento();
  13. Transportadoras outputTransporatdoras = findTransByID(postos.getOutPut());
  14. if (outputTransporatdoras != null &amp;&amp; outputTransporatdoras.temCapacidade()) {
  15. outputTransporatdoras.addPe&#231;a(postos.getID());
  16. postos.setEstado(Estado.OCUPADO);
  17. } else {
  18. postos.setEstado(Estado.PARADO);
  19. }
  20. } else if (postos.getEstado() == Estado.MANUTEN&#199;&#195;O) {
  21. postos.setEstado(Estado.ESPERA);
  22. }
  23. }
  24. }
  25. }

答案1

得分: 1

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

  1. public LinhaDeProducao()
  2. {
  3. transportadoras = new ArrayList<>();
  4. postos = new ArrayList<>();
  5. }

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

  1. public LinhaDeProducao(String linha, int tempoSimulacao)
  2. {
  3. this.descriLinha = linha;
  4. this.simulDuracao = tempoSimulacao;
  5. }

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

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

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

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

  1. public LinhaDeProducao()
  2. {
  3. transportadoras = new ArrayList<>();
  4. postos = new ArrayList<>();
  5. }
  6. public LinhaDeProducao(String linha, int tempoSimulacao)
  7. {
  8. this();
  9. this.descriLinha = linha;
  10. this.simulDuracao = tempoSimulacao;
  11. }

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

  1. public LinhaDeProducao()
  2. {
  3. init();
  4. }
  5. public LinhaDeProducao(String linha, int tempoSimulacao)
  6. {
  7. init();
  8. this.descriLinha = linha;
  9. this.simulDuracao = tempoSimulacao;
  10. }
  11. void init() {
  12. transportadoras = new ArrayList<>();
  13. postos = new ArrayList<>();
  14. }
英文:

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

  1. public LinhaDeProducao()
  2. {
  3. transportadoras = new ArrayList&lt;&gt;();
  4. postos = new ArrayList&lt;&gt;();
  5. }

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.

  1. public LinhaDeProducao(String linha, int tempoSimulacao)
  2. {
  3. this.descriLinha = linha;
  4. this.simulDuracao = tempoSimulacao;
  5. }

There are a few approaches to fix this problem.

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

  1. private List&lt;Transportadoras&gt; transportadoras = new ArrayList&lt;&gt;();
  2. 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.

  1. public LinhaDeProducao()
  2. {
  3. transportadoras = new ArrayList&lt;&gt;();
  4. postos = new ArrayList&lt;&gt;();
  5. }
  6. public LinhaDeProducao(String linha, int tempoSimulacao)
  7. {
  8. this();
  9. this.descriLinha = linha;
  10. this.simulDuracao = tempoSimulacao;
  11. }

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

  1. public LinhaDeProducao()
  2. {
  3. init();
  4. }
  5. public LinhaDeProducao(String linha, int tempoSimulacao)
  6. {
  7. init();
  8. this.descriLinha = linha;
  9. this.simulDuracao = tempoSimulacao;
  10. }
  11. void init() {
  12. transportadoras = new ArrayList&lt;&gt;();
  13. postos = new ArrayList&lt;&gt;();
  14. }

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:

确定