So I'm having trouble with an application I've been working on and I only saw one other post with the -1 error and doesn't seem to help me with what is going on. We have an items tab that when you click on a consignor, it should display the items and it does, but it spits out this error and sometimes doesn't update.
Error:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at com.GiggaFriggs.ItemPanel$5.valueChanged(ItemPanel.java:502)
at javax.swing.JList.fireSelectionValueChanged(Unknown Source)
at javax.swing.JList$ListSelectionHandler.valueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
at javax.swing.DefaultListSelectionModel.removeSelectionIntervalImpl(Unknown Source)
at javax.swing.DefaultListSelectionModel.clearSelection(Unknown Source)
at javax.swing.JList.clearSelection(Unknown Source)
at javax.swing.JList.setModel(Unknown Source)
at javax.swing.JList.setListData(Unknown Source)
at com.GiggaFriggs.ItemPanel.refreshItemList(ItemPanel.java:533)
at com.GiggaFriggs.ItemPanel$4.valueChanged(ItemPanel.java:460)
at javax.swing.JList.fireSelectionValueChanged(Unknown Source)
at javax.swing.JList$ListSelectionHandler.valueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
at javax.swing.DefaultListSelectionModel.setSelectionInterval(Unknown Source)
at javax.swing.JList.setSelectionInterval(Unknown Source)
at javax.swing.plaf.basic.BasicListUI$Handler.adjustSelection(Unknown Source)
at javax.swing.plaf.basic.BasicListUI$Handler.mousePressed(Unknown Source)
at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(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.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.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)
Code:
package com.GiggaFriggs;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class ItemPanel extends JPanel {
protected static int conID;
private int itemID;
private JLabel itemIdLabel;
private JTextField searchTF;
private JRadioButton conRB, dealRB;
private ButtonGroup searchBG;
private JButton searchB;
protected static JList <String> conList;
private static JList <String> itemList;
private JScrollPane itemPane;
private String[] sampleItemA;
private JFormattedTextField priceTF;
private JTextField itemDesTF;
private JFormattedTextField lowPriceTF;
private JComboBox<?> categoryCB;
final private String[] categoryListA = { "Home Decor", "Womens Accessories", "Furniture", "Collectables"};
private JTextField commentsTF;
private static final String IMG_PATH = "src/images/testImage.jpg";
protected static String[] conSampleList;
private JLabel priceL;
private JLabel lowPriceL;
private JButton saveB;
private JButton newB;
private JButton deleteB;
private JButton sellB;
protected static Consigner[] consignerList;
private JScrollPane scrollPane;
private static int conMarker;
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://173.194.80.143:3306/Test";
static Connection conn = null;
static Statement stmt = null;
private final SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
public ItemPanel(){
conMarker = 0;
consignerList = DatabaseConnector.pullConInfoDB();
conSampleList = new String[consignerList.length];
for (int i = 0; i < conSampleList.length; i++){
conSampleList[i] = "ID " + String.valueOf(consignerList[i].getIdNumber() + " " + consignerList[i].getLastName() + ", " + consignerList[i].getFirstName());
}
//consignerID = consignerList[0].getIdNumber();
searchTF = new JTextField("Search for Consigner/Dealer");
searchB = new JButton("Search");
conList = new JList<String>(conSampleList);
conList.setVisibleRowCount(5);
conList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
conList.setSelectedIndex(0);
scrollPane = new JScrollPane(conList);
sampleItemA = new String[consignerList[conMarker].items.length];
for (int i = 0; i < sampleItemA.length; i++){
if(consignerList[conMarker].items[i].isSold() == false)
sampleItemA[i] = consignerList[conMarker].items[i].getItemDesc();
}
itemList = new JList<String>(sampleItemA);
itemList.setVisibleRowCount(5);
itemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
itemList.setSelectedIndex(0);
itemPane = new JScrollPane(itemList);
itemIdLabel = new JLabel("Item # " + Integer.toString(itemID));
itemDesTF = new JTextField(consignerList[conMarker].items[0].getItemDesc());
itemDesTF.setColumns(21);
categoryCB= new JComboBox<String>(categoryListA);
categoryCB.setSelectedItem(consignerList[conMarker].items[0].getCategory());
priceTF = new JFormattedTextField(NumberFormat.getNumberInstance());
priceTF.setColumns(5);
priceTF.setValue(consignerList[conMarker].items[0].getSalePrice());
lowPriceTF = new JFormattedTextField(NumberFormat.getNumberInstance());
lowPriceTF.setValue(consignerList[conMarker].items[0].getLowBid());
lowPriceTF.setColumns(5);
priceL = new JLabel("Requested Price");
lowPriceL = new JLabel("Lowest Price Accepted");
commentsTF = new JTextField(consignerList[conMarker].items[0].getComments());
conRB = new JRadioButton("Consigner", true);
dealRB = new JRadioButton("Dealer", false);
searchBG = new ButtonGroup();
searchBG.add(conRB);
searchBG.add(dealRB);
saveB = new JButton("Save/Edit");
newB = new JButton("New Item");
deleteB = new JButton("Delete Item");
sellB = new JButton("Sell Item");
saveB.setDefaultCapable(true);
saveB.getPreferredSize();
}
public static int getConMarker() {
return conMarker;
}
protected JPanel createItemsPanel(){
JPanel outerPanel = new JPanel();
outerPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//adding constraints to items and adding them to the panel
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
outerPanel.add(searchTF, c);
c.gridx = 2;
outerPanel.add(searchB, c);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 1;
outerPanel.add(conRB, c);
c.gridx =1;
outerPanel.add(dealRB, c);
c.gridy =2;
c.gridx = 0;
c.gridwidth = 2;
outerPanel.add(scrollPane, c);
JPanel innerPanel = new JPanel();
innerPanel.setLayout(new GridBagLayout());
GridBagConstraints c2 = new GridBagConstraints();
c2.fill = GridBagConstraints.BOTH;
TitledBorder titled = BorderFactory.createTitledBorder("Item Information");
innerPanel.setBorder(titled);
c2.gridx = 0;
c2.gridy = 0;
c2.insets = new Insets(10,10,10,10);
innerPanel.add(itemIdLabel, c2);
c2.gridx = 1;
c2.gridwidth = 2;
innerPanel.add(itemPane, c2);
//this is how you show an image
// try{
// BufferedImage img = ImageIO.read(new File(IMG_PATH));
// ImageIcon icon = new ImageIcon(img);
// pictureLabel = new JLabel(icon);
//
// } catch (IOException e){
// e.printStackTrace();
// }
// c2.gridx = 4;
// c2.gridwidth = 1;
// innerPanel.add(pictureLabel, c2);
c2.gridx = 0;
c2.gridy=1;
c2.gridwidth = 2;
innerPanel.add(itemDesTF, c2);
c2.gridx = 2;
c2.gridwidth = 1;
innerPanel.add(categoryCB, c2);
c2.gridx = 0;
c2.gridy = 2;
innerPanel.add(priceL, c2);
c2.gridx = 2;
c2.gridwidth = 1;
innerPanel.add(priceTF, c2);
c2.gridx = 0;
c2.gridy = 3;
innerPanel.add(lowPriceL, c2);
c2.gridwidth = 1;
c2.gridx = 2;
innerPanel.add(lowPriceTF, c2);
c2.gridwidth = 3;
c2.gridx = 0;
c2.gridy =4;
innerPanel.add(commentsTF, c2);
c2.gridwidth = 1;
c2.gridx = 0;
c2.gridy = 5;
innerPanel.add(saveB, c2);
c2.gridx = 1;
innerPanel.add(deleteB, c2);
c2.gridx = 2;
innerPanel.add(newB, c2);
c2.gridx = 3;
innerPanel.add(sellB, c2);
c.gridy = 3;
c.gridx = 0;
c.gridwidth = 5;
outerPanel.add(innerPanel, c);
EditHandler eh = new EditHandler();
saveB.addActionListener(eh);
SearchingHands sh = new SearchingHands();
searchB.addActionListener(sh);
searchTF.addActionListener(sh);
newB.addActionListener(
new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
conMarker = (int) conList.getSelectedIndex();
consignerList = DatabaseConnector.pullConInfoDB();
System.out.println(conMarker);
System.out.println(consignerList[conMarker].itemsForSale());
if (consignerList[conMarker].itemsForSale() < 15 ){
NewItem ni = new NewItem();
ni.setUpInter();
} else {
JOptionPane.showMessageDialog(null,
"Sorry",
"This Consignor already has 15 items for sale",
JOptionPane.ERROR_MESSAGE);
}
}
});
deleteB.addActionListener(
new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
int x = (int) itemList.getSelectedIndex();
String temp = itemIdLabel.getText();
String temp2 = temp.substring(7);
int i = Integer.parseInt(temp2);
System.out.println(i);
String input = (String)JOptionPane.showInputDialog(
null,
"Why are you deleting this item?",
"Deleting " + consignerList[conMarker].items[x].getItemDesc(),
JOptionPane.PLAIN_MESSAGE,
null, null, "Explain Yourself!!!");
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, "root", "swingset");
stmt=conn.createStatement();
String sq1 = "UPDATE items SET itemStatus = 'D', itemComments = '" + input + "' WHERE itemID = " + i;
stmt.executeUpdate(sq1);
conn.close();
} catch (SQLException ex){
ex.printStackTrace();
}catch(Exception er){
er.printStackTrace();
}
consignerList[conMarker].items[x].setSold(true);
refreshConList();
refreshItemList(conMarker);
//
// Object[] options = {"Yes",
// "No"};
// int n = JOptionPane.showOptionDialog(null,
// "Are you sure you want to delete this item?",
// "Delete Item?",
// JOptionPane.YES_NO_OPTION,
// JOptionPane.QUESTION_MESSAGE,
// null, //do not use a custom Icon
// options, //the titles of buttons
// options[0]); //default button title
//
// if (n == JOptionPane.YES_OPTION){
//
// try{
// Class.forName("com.mysql.jdbc.Driver");
//
// conn = DriverManager.getConnection(DB_URL, "root", "swingset");
// stmt=conn.createStatement();
//
// String sq1 = "DELETE FROM items WHERE itemID = " + i;
// stmt.executeUpdate(sq1);
//
// conn.close();
//
// } catch (SQLException ex){
// ex.printStackTrace();
// }catch(Exception er){
// er.printStackTrace();
// }
}
}
);
sellB.addActionListener(
new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
int x = (int) itemList.getSelectedIndex();
double soldFor = 0.00;
String input = (String)JOptionPane.showInputDialog(
null,
"How much are you selling "
+ consignerList[conMarker].items[x].getItemDesc() + " for?",
"Selling " + consignerList[conMarker].items[x].getItemDesc(),
JOptionPane.PLAIN_MESSAGE,
null, null, "$00.00");
// String input = JOptionPane.showInputDialog("Selling " + consignerList[conMarker].items[x].getItemDesc(), "How much are you selling "
// + consignerList[conMarker].items[x].getItemDesc() + "for?");
try{
soldFor = Double.parseDouble(input);
} catch (NumberFormatException ex){
JOptionPane.showMessageDialog(null,
"Please Enter a Number.",
"That is not a Number!!!!",
JOptionPane.ERROR_MESSAGE);
}
if (soldFor >= consignerList[conMarker].items[x].getLowBid()){
consignerList[conMarker].items[x].sellItem(soldFor);
String temp = itemIdLabel.getText();
String temp2 = temp.substring(7);
int i = Integer.parseInt(temp2);
Date d = new Date();
String soldDate = sdf.format(d);
//SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM dd, yyy");
//Calendar calendar = new GregorianCalendar();
//calendar.add(Calendar.DAY_OF_MONTH, 90);
//consignerList[conMarker].items[x].setCreatedOn(sdf.format(calendar.getTime()));
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, "*****", "*******");
stmt=conn.createStatement();
String sq1 = "UPDATE items SET itemSoldFor = '" + soldFor + "' WHERE itemID = '" + i + "AND itemStatus = 'S' WHERE itemID = " + i + "ANDitemSoldDate = '" + soldDate + "' WHERE itemID = " + i;
// String sq2 = "UPDATE items SET itemStatus = 'S' WHERE itemID = " + i;
// String sq3 = "UPDATE items SET itemSoldDate = '" + soldDate + "' WHERE itemID = " + i;
stmt.executeUpdate(sq1);
// stmt.executeUpdate(sq2);
// stmt.executeUpdate(sq3);
conn.close();
} catch (SQLException ex){
ex.printStackTrace();
}catch(Exception er){
er.printStackTrace();
}
refreshItemList(conMarker);
} else {
JOptionPane.showMessageDialog(null,
"The Consignor wants more money than that man.",
"Not Enough Moola",
JOptionPane.ERROR_MESSAGE);
}
}});//end of sellB action Listener
conList.addListSelectionListener(
new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent e){
int conMarker = (int) (conList.getSelectedIndex());
refreshItemList(conMarker);
// System.out.println(conMarker);
// System.out.println(consignerList[conMarker].items.length);
// if (consignerList[conMarker].hasItems() == true) {
//
// String[] testA;
//
// testA = new String[consignerList[conMarker].items.length];
//
// for (int i = 0; i < testA.length; i++){
// if(consignerList[conMarker].items[i].isSold() == false)
// testA[i] = consignerList[conMarker].items[i].getItemDesc();
// }
//
// itemList.setListData(testA);
// //itemPane.setViewportView(itemList);
// } else {
// // itemList.setVisible(false);
// String[] noItems = new String[1];
// noItems[0] = "No Items for Sale";
// itemList.setListData(noItems);
// }
}
}
);//end of conList Listener
itemList.addListSelectionListener(
new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent e){
int conMarker = (int) (conList.getSelectedIndex());
int i = (int) itemList.getSelectedIndex();
itemID = consignerList[conMarker].items[i].getItemNumber();
itemIdLabel.setText("Item # " + Integer.toString(itemID));
itemDesTF.setText(consignerList[conMarker].items[i].getItemDesc());
categoryCB.setSelectedIndex(consignerList[conMarker].items[i].getCategory());
priceTF.setText(Double.toString(consignerList[conMarker].items[i].getSalePrice()));
lowPriceTF.setText(Double.toString(consignerList[conMarker].items[i].getLowBid()));
commentsTF.setText(consignerList[conMarker].items[conMarker].getComments());
}
});//end of itemList Listener
outerPanel.setVisible(true);
return outerPanel;
}
public static void refreshItemList(int conMarker){
if (consignerList[conMarker].hasItems() == true) {
String[] testA;
testA = new String[consignerList[conMarker].items.length];
for (int i = 0; i < testA.length; i++){
if(consignerList[conMarker].items[i].isSold() == false)
testA[i] = consignerList[conMarker].items[i].getItemDesc();
}
itemList.setListData(testA);
//itemPane.setViewportView(itemList);
} else {
// itemList.setVisible(false);
String[] noItems = new String[1];
noItems[0] = "No Items for Sale";
itemList.setListData(noItems);
}
}
public static void refreshConList(){
consignerList = DatabaseConnector.pullConInfoDB();
conSampleList = new String[consignerList.length];
for (int i = 0; i < conSampleList.length; i++){
conSampleList[i] = "ID " + String.valueOf(consignerList[i].getIdNumber() + " " + consignerList[i].getLastName() + ", " + consignerList[i].getFirstName());
}
//conList.setListData(conSampleList);
}
private class EditHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
String temp = itemIdLabel.getText();
String temp2 = temp.substring(7);
int i = Integer.parseInt(temp2);
conMarker = (int) (conList.getSelectedIndex());
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, "*****", "*******");
stmt=conn.createStatement();
String sq1 = "UPDATE items SET itemDesc = '" + itemDesTF.getText() + "' WHERE itemID = " + i + "AND itemSalePrice = " + Double.parseDouble(priceTF.getText()) + " WHERE itemID = " + i + "AND itemLowBid = " + Double.parseDouble(lowPriceTF.getText()) + " WHERE itemID = " + i + "AND itemComments = '" + commentsTF.getText() + "' WHERE itemID = " + i + "AND itemCategory = '" +categoryCB.getSelectedIndex() + "' WHERE itemID = " + i;
// String sq2 = "UPDATE items SET itemSalePrice = " + Double.parseDouble(priceTF.getText()) + " WHERE itemID = " + i;
// String sq3 = "UPDATE items SET itemLowBid = " + Double.parseDouble(lowPriceTF.getText()) + " WHERE itemID = " + i;
// String sq4 = "UPDATE items SET itemComments = '" + commentsTF.getText() + "' WHERE itemID = " + i;
// String sq5 = "UPDATE items SET itemCategory = '" +categoryCB.getSelectedIndex() + "' WHERE itemID = " + i;
stmt.executeUpdate(sq1);
// stmt.executeUpdate(sq2);
// stmt.executeUpdate(sq3);
// stmt.executeUpdate(sq4);
// stmt.executeUpdate(sq5);
conn.close();
} catch (SQLException ex){
ex.printStackTrace();
}catch(Exception er){
er.printStackTrace();
}
refreshConList();
refreshItemList(conMarker);
}
}
private class SearchingHands implements ActionListener{
@Override
public void actionPerformed(ActionEvent event){
int x = 0;
try{
x = Integer.parseInt(searchTF.getText());
} catch (NumberFormatException e){
JOptionPane.showMessageDialog(null,
"Please Enter a Number.",
"That is not a Number!!!!",
JOptionPane.ERROR_MESSAGE);
}
if (x >= 0 && x <= consignerList.length){
conMarker = x;
conList.setSelectedIndex(x);
String[] testA = new String[consignerList[conMarker].items.length];
for (int i = 0; i < sampleItemA.length; i++){
if(consignerList[conMarker].items[i].isSold() == false)
testA[i] = consignerList[conMarker].items[i].getItemDesc();
}
itemList.setListData(testA);
itemList.setSelectedIndex(0);
} else {
JOptionPane.showMessageDialog(null,
"Please Enter a Number between 0 & " + (consignerList.length) + ".",
"Not In Range!",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
getSelectedIndex
will return a value of -1
if no value is selected in the JList
conList
. Ensure both JList
components have selected items before attempting to access any fields in the list consignerList
if (!conList.isSelectionEmpty() && !itemList.isSelectionEmpty()) {
...
}