Erro foreach

2 respostas
java
Thiago_Correia_Lima

Ta dando erro na linha 154, no ForEach, e na linha 159. Alguem ai pode dar um help!!???

  1. import java.sql.Connection;

  2. import java.sql.ResultSet;

  3. import java.sql.Statement;

  4. import java.util.ArrayList;

  5. import java.util.Arrays;

  6. import javax.swing.DefaultListModel;

  7. /*

    • To change this license header, choose License Headers in Project Properties.
    • To change this template file, choose Tools | Templates
    • and open the template in the editor.
  8. */

  9. /**

    • @author tigones
  10. */

  11. public class MeusAlunos extends javax.swing.JFrame {

  12. /**
    
  13. * Creates new form MeusAlunos
    
  14. */
    
  15. public MeusAlunos() {
    
  16. initComponents();

  17. atualizaAlunos();

  18. }

  19. /**
    
  20. * This method is called from within the constructor to initialize the form.
    
  21. * WARNING: Do NOT modify this code. The content of this method is always
    
  22. * regenerated by the Form Editor.
    
  23. */
    
  24. @SuppressWarnings("unchecked")
    
  25. // <editor-fold defaultstate="collapsed" desc="Generated Code">
    
  26. private void initComponents() {
    
  27. jRadioButton1 = new javax.swing.JRadioButton();
    
  28. jLabel1 = new javax.swing.JLabel();
    
  29. jScrollPane1 = new javax.swing.JScrollPane();
    
  30. jList1 = new javax.swing.JList<>();
    
  31. btnAdicionar = new javax.swing.JButton();
    
  32. btnExcluir = new javax.swing.JButton();
    
  33. btnVoltar = new javax.swing.JButton();
    
  34. jRadioButton1.setText("jRadioButton1");
    
  35. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
  36. jLabel1.setFont(new java.awt.Font("Ubuntu", 0, 36)); // NOI18N
    
  37. jLabel1.setText("ALUNOS");
    
  38. jList1.setModel(new javax.swing.AbstractListModel<String>() {
    
  39. String[] strings = { "Aluno 1", "Aluno 2", "Aluno 3", "Aluno 4", "Aluno 5", "Aluno 6", "Aluno 7", "Aluno 8", "Aluno9" };
    
  40. public int getSize() { return strings.length; }
    
  41. public String getElementAt(int i) { return strings[i]; }
    
  42. });
    
  43. jScrollPane1.setViewportView(jList1);
    
  44. btnAdicionar.setText("Adicionar");
    
  45. btnExcluir.setText("Excluir");
    
  46. btnVoltar.setText("Voltar");
    
  47. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    
  48. getContentPane().setLayout(layout);
    
  49. layout.setHorizontalGroup(
    
  50. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    
  51. .addGroup(layout.createSequentialGroup()
    
  52. .addContainerGap()
    
  53. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
    
  54. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
    
  55. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    
  56. .addComponent(btnAdicionar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
    
  57. .addComponent(btnExcluir, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
    
  58. .addComponent(btnVoltar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))
    
  59. .addContainerGap())
    
  60. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
    
  61. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    
  62. .addComponent(jLabel1)
    
  63. .addGap(112, 112, 112))
    
  64. );
    
  65. layout.setVerticalGroup(
    
  66. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    
  67. .addGroup(layout.createSequentialGroup()
    
  68. .addContainerGap(26, Short.MAX_VALUE)
    
  69. .addComponent(jLabel1)
    
  70. .addGap(18, 18, 18)
    
  71. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
    
  72. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
    
  73. .addGroup(layout.createSequentialGroup()
    
  74. .addComponent(btnAdicionar)
    
  75. .addGap(18, 18, 18)
    
  76. .addComponent(btnExcluir)
    
  77. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    
  78. .addComponent(btnVoltar)))
    
  79. .addContainerGap())
    
  80. );
    
  81. pack();
    
  82. }// </editor-fold>
    
  83. /**
    
  84. * @param args the command line arguments
    
  85. */
    
  86. public static void main(String args[]) {
    
  87. /* Set the Nimbus look and feel */
    
  88. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    
  89. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
    
  90. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
    
  91. */
    
  92. try {
    
  93. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    
  94. if ("Nimbus".equals(info.getName())) {
    
  95. javax.swing.UIManager.setLookAndFeel(info.getClassName());
    
  96. break;
    
  97. }
    
  98. }
    
  99. } catch (ClassNotFoundException ex) {
    
  100. java.util.logging.Logger.getLogger(MeusAlunos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    
  101. } catch (InstantiationException ex) {
    
  102. java.util.logging.Logger.getLogger(MeusAlunos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    
  103. } catch (IllegalAccessException ex) {
    
  104. java.util.logging.Logger.getLogger(MeusAlunos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    
  105. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    
  106. java.util.logging.Logger.getLogger(MeusAlunos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    
  107. }
    
  108. //</editor-fold>
    
  109. /* Create and display the form */
    
  110. java.awt.EventQueue.invokeLater(new Runnable() {
    
  111. public void run() {
    
  112. new MeusAlunos().setVisible(true);
    
  113. }
    
  114. });
    
  115. }
    
  116. // Variables declaration - do not modify
    
  117. private javax.swing.JButton btnAdicionar;
    
  118. private javax.swing.JButton btnExcluir;
    
  119. private javax.swing.JButton btnVoltar;
    
  120. private javax.swing.JLabel jLabel1;
    
  121. private javax.swing.JList<String> jList1;
    
  122. private javax.swing.JRadioButton jRadioButton1;
    
  123. private javax.swing.JScrollPane jScrollPane1;
    
  124. // End of variables declaration
    
  125. private void atualizaAlunos() {
    
  126. //ArrayList<String> bandas = new ArrayList<String>
    
  127. ArrayList alunos = new ArrayList();

  128. Connection conexao = Conexao.getInstance().sqlConnection;

  129. //fill client list

  130. String query = “SELECT nome, cpf FROM aluno”;

  131. Statement stmt = null;

  132. try{

  133. stmt = conexao.createStatement();

  134. ResultSet rs = stmt.executeQuery(query);

  135. while(rs.next()){

  136. String nome = rs.getString(“nome”);

  137. String cpf = rs.getString(“cpf”);

  138. //adiciona cliente na lista

  139. aluno.add(new aluno(nome, cpf));

  140. }

  141. }catch(Exception e){

  142. e.printStackTrace();

  143. }

  144. DefaultListModel listModel = new DefaultListModel();

    • for(Alunos c : alunos){
    • listModel.addElement(new String(c.getNome() + " - " + c.getCPF()));
    • System.out.println("Aluno name: " + c.getNome());
    • }
    • listAlunos.setModel(listModel);
    • }
    • }
      
    • }

2 Respostas

FacaNaCaveira

Thiago Bom dia, na linha 146 não deveria ser:

Acho que ta faltando o S de alunos que é um ArrayList correto?
Se possível posta os erros que esão sendo mostrados a você pois fica mais facil podermos ajuda-lo

Abraços
Max

Thiago_Correia_Lima

Cara, estava errado mesmo na construção do Array.

Agora estou com um outro erro louco aqui que eu nunca vi na vida!!! kkkk

Exception in thread “main” java.lang.UnsupportedOperationException: Not supported yet.

at sistemadecadastro.MeuMenu.setDefaultCloseOperation(MeuMenu.java:26)

at sistemadecadastro.SistemaDeCadastro.main(SistemaDeCadastro.java:34)

/home/tigones/.cache/netbeans/8.2/executor-snippets/run.xml:53: Java returned: 1

FALHA NA CONSTRUÇÃO (tempo total: 0 segundos)
Criado 17 de agosto de 2018
Ultima resposta 20 de ago. de 2018
Respostas 2
Participantes 2