Visualizzazione dei risultati da 1 a 4 su 4

Discussione: NullPointer Array

  1. #1

    NullPointer Array

    Ciao sto tentando di popolare un array di JLabel e uno di TextField
    ma quando nel metodo addLabel aggiungo la label l'array mi va in nullpointer???


    codice:
    class JTableButtonMouseListener implements MouseListener {
         
      private JLabel[] lblVet;
      private  JTextField[] txtVet;
       private JTableButton frameOwner;
    
       
       public JTableButtonMouseListener(JTable table,JTableButton frameOwner) {
      tableListener = table;
      this.frameOwner = frameOwner;
           
       }
     public void clickOpenDialog(MouseEvent e) {
       MouseEvent buttonEvent;
        int nCol = frameOwner.getTable().getColumnCount()-1;
         JPanel pan = new JPanel();
        lblVet = new JLabel[nCol];
        txtVet = new JTextField[nCol];
        System.out.println(nCol);
      //aggiungere bottoni
         for(int k=0;k<nCol;k++)   
       {   
           this.addLabel(k);
          pan.add(lblVet[k]);
           this.addTextField(k);
          pan.add(txtVet[k]);
       
       }
       for(int k=0;k<nCol;k++)   
       {
         lblVet[k].setText(frameOwner.getTable().getColumnName(k));
         txtVet[k].setText(frameOwner.getTable().getValueAt(frameOwner.getTable().getSelectedRow(),k).toString());
         
       }
    
       
       frm = new JFrame();
       
       btn_Salva.setText("SALVA");
       btn_Salva.addActionListener(new ActionListener()
         {
           public void actionPerformed(ActionEvent e)
           {
            btn_Salva_actionPerformed(e);
           }
         });
        GridLayout layout = new GridLayout(3,2);
       btn_Salva.setSize(100,20);
       pan.add(btn_Salva);
       pan.setLayout(layout);
       frm.getContentPane().add(pan);
       frm.pack();
       frm.setVisible(true);
     } 
    
     private void addLabel(int k)
     {
       JLabel lbl = new JLabel();
        lbl.setSize(50,50);
        lblVet[k].add(lbl);
     }
     private void addTextField(int k)
     {
       JTextField txt = new JTextField();
       txt.setSize(50,50);
       txtVet[k].add(txt);
     }

  2. #2
    Confondi l'array con la classe Vector. Tu non stai popolando un Vector, cosa che invece ti consiglio.

    invece di
    lblVet[k].add(lbl);
    prova con
    lblVet[k] = lbl;


  3. #3
    Ok
    Thank you!!!!!

  4. #4
    Sei pregato di inserire il LINGUAGGIO utilizzato... nel titolo del thread... come da regolamento.

    :regolamento_forum:
    ...Terrible warlords, good warlords, and an english song

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.