Czyszczenie wektora

DA
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 30
0

Witam
Mam taki problem, że gdy czyszczę wektor to otrzymuję błąd

Kopiuj
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
	at java.util.Vector.elementAt(Unknown Source)
	at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)
	at javax.swing.JTable.getValueAt(Unknown Source)
	at javax.swing.JTable.prepareRenderer(Unknown Source)
	at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
	at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
	at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
	at javax.swing.plaf.ComponentUI.update(Unknown Source)
	at javax.swing.JComponent.paintComponent(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JViewport.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
	at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
	at javax.swing.RepaintManager.paint(Unknown Source)
	at javax.swing.JComponent._paintImmediately(Unknown Source)
	at javax.swing.JComponent.paintImmediately(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.access$700(Unknown Source)
	at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

Oto mój kawałek kodu:

Kopiuj
	public void updateProductList()
	{
		Vector<String> v = new Vector<>();
		//DefaultTableModel model;
		Statement stat;
		String id;
		String name;
		String price;
		String plu;
		String code;
		String tax;
		String amount;

		try
		{
			stat = conn.createStatement();
			ResultSet rs = stat.executeQuery("select * from Products;");
			while (rs.next())
			{
				id = rs.getString(1);
				v.addElement(id);
				name = rs.getString(2);
				v.addElement(name);
				price = rs.getString(3);
				v.addElement(price);
				plu = rs.getString(4);
				v.addElement(plu);
				code = rs.getString(5);
				v.addElement(code);
				tax = rs.getString(6);
				v.addElement(tax);
				amount = rs.getString(7);
				v.addElement(amount);

				model.addRow(v);
				v.clear();
			}
			rs.close();
			stat.close();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
	}

Gdzie model jest modelem obiektu JTable. W każdym razie instrukcja v.clear() powoduje wystąpienie błędu. Proszę o podpowiedź czemu tak się dzieje.

  • Rejestracja: dni
  • Ostatnio: dni
1

Nie podoba mi się Twój pomysł z czyszczeniem wektora, który jest już umieszczony w modelu. Ja bym zrobił tak:

Kopiuj
                        while (rs.next())
                        {
                                Vecor<String> v = new Vector<String>();
                                id = rs.getString(1);
                                v.addElement(id);
                                name = rs.getString(2);
                                v.addElement(name);
                                price = rs.getString(3);
                                v.addElement(price);
                                plu = rs.getString(4);
                                v.addElement(plu);
                                code = rs.getString(5);
                                v.addElement(code);
                                tax = rs.getString(6);
                                v.addElement(tax);
                                amount = rs.getString(7);
                                v.addElement(amount);
 
                                model.addRow(v);
                        }
DA
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 30
0

Rozważałem takie podejście tylko nie rozumiem, czemu nie mogę zrobić tak jak w moim kodzie.

  • Rejestracja: dni
  • Ostatnio: dni
1

Bo w każdym wierszu modelu masz tę samą referencję. Nawet jak nie będzie błędu, to wyświetlisz identyczne wiersze (z ostatnim odczytanym rekordem).

DA
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 30
0

OK już chyba rozumiem. Dzięki

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.