Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Exception (payword protocol)

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Exception (payword protocol)

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
            at java.util.ArrayList.get(ArrayList.java:323)
            at payword.user.CertificateTableModel.getSelected(CertificateTableModel.java:61)
            at payword.user.ClientWindow.vendorConnectButtonActionPerformed(ClientWindow.java:373)
            at payword.user.ClientWindow.access$500(ClientWindow.java:23)
            at payword.user.ClientWindow$6.actionPerformed(ClientWindow.java:182)
            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
            at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
            at java.awt.Component.processMouseEvent(Component.java:5517)
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)
            at java.awt.Component.processEvent(Component.java:5282)
            at java.awt.Container.processEvent(Container.java:1966)
            at java.awt.Component.dispatchEventImpl(Component.java:3984)
            at java.awt.Container.dispatchEventImpl(Container.java:2024)
            at java.awt.Component.dispatchEvent(Component.java:3819)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
            at java.awt.Container.dispatchEventImpl(Container.java:2010)
            at java.awt.Window.dispatchEventImpl(Window.java:1791)
            at java.awt.Component.dispatchEvent(Component.java:3819)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
            at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

    any idea for this exception???



    This exception appears when i push "Connect" button for ports : 11201 (client-vendor connection) and 11202 (vendor-broker connection).

    The database is created by WampServer application..


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Exception (payword protocol)

    Without code posted we all have to rely on guesses. Here's my guess: you are accessing an ArrayList for index -1, which will throw an exception. Based upon the stack trace, I'd guess you are trying to get the selected row or column within a table - but there are none selected so the returned selected row is -1 (JTable (Java 2 Platform SE 5.0)). Again beyond that its anyone's guess...

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception (payword protocol)

    Quote Originally Posted by copeg View Post
    Without code posted we all have to rely on guesses. Here's my guess: you are accessing an ArrayList for index -1, which will throw an exception. Based upon the stack trace, I'd guess you are trying to get the selected row or column within a table - but there are none selected so the returned selected row is -1 (JTable (Java 2 Platform SE 5.0)). Again beyond that its anyone's guess...
    Client to Vendor Connection

    package payword.user;
     
    import payword.User;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.Socket;
    import payword.ClientCommitment;
     
    /**
     *
     * @author Thodoris
     */
    public class ClientToVendorConnection {
        private Socket socket;
        private String address;
        private int port;
        private ClientWindow parent;
        private ClientCommitment commitment;
        private User user;
     
        public ClientToVendorConnection(String address, int port, ClientWindow parent, ClientCommitment commitment, User user) {
            this.address = address;
            this.port = port;
            this.parent = parent;
            this.commitment = commitment;
            this.user = user;
     
            connect();
        }
     
     
        public void connect(){
            Thread t = new Thread(new Runnable() {
     
                public void run() {
                    try {
                        socket = new Socket(address, port);
                        parent.log("Connected to: " + address + ":" + port);
     
                        // Στέλνω το commitment για να γράψει ο vendor τα στοιχεία του
                        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                        out.writeObject(commitment);
                        out.flush();
     
                        // Παίρνω πίσω το commitment μαζί με τα στοιχεία του vendor και υπολογίζω την υπογραφή και το paychain
                        ObjectInputStream iin = new ObjectInputStream(socket.getInputStream());
                        commitment = (ClientCommitment)iin.readObject();
                        commitment.calculatePayChain(user.getUserPrivateKey());
     
                        commitment.setWRoot(null);
     
                        // Στέλνω στον έμπορο το τελικό commitment.
                        out.writeObject(commitment);
     
     
                        parent.log("User commitment sent.");
     
                        socket.close();
     
                    } catch (Exception ex) {
                        parent.log("ERROR: " + ex.getMessage());
                    } 
     
                }
            });
            t.run();
        }
     
     
    }


    Vendor to Client connection

    package payword.vendor;
     
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import payword.ClientCommitment;
     
    /**
     *
     * @author Thodoris
     */
    public class VendorToClientConnection {
        private VendorWindow parent;
        private ServerSocket serverSocket;
        private ClientCommitment commitment;
        private int port;
     
        public VendorToClientConnection(int port, VendorWindow parent, ClientCommitment commitment) {
            this.parent = parent;
            this.commitment = commitment;
            this.port = port;
        }
     
     
        public void startListening(){
            Thread thread = new Thread(new Runnable() {
                public void run() {
                    try {
                        serverSocket = new ServerSocket(port);
                        parent.log("VendorToClient: Start listening port " + port);
     
                    } catch (IOException ex) {
                        parent.log("ERROR: Could not start listening port: " + port);
                    }
     
                    try {
                        Socket socket = serverSocket.accept();
                        parent.log("Accepted connection.");
     
                        // Παίρνω το commitment και γράφω το vendorInfo
                        ObjectInputStream iin = new ObjectInputStream(socket.getInputStream());
                        ClientCommitment commitment = (ClientCommitment)iin.readObject();
                        parent.log("Commitment received.");
     
                        commitment.setVendorInfo("Vendor One");
                        // Στέλνω πίσω το commitment για να μπορέσει ο client να υπολογίσει το wRoot
                        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                        out.writeObject(commitment);
                        parent.log("Sent.");
     
                        commitment = (ClientCommitment)iin.readObject();
                        parent.log("Final commitment received.");
     
                        parent.log("vendorInfo: " + commitment.getVendorInfo());
                        parent.log("userInfo: " + commitment.getCertificate().getUserName());
                        parent.log("ammount: " + commitment.getAmmount());
     
                        parent.log("Vendor: Commitment received.");
     
                        VendorDbConnection con = new VendorDbConnection();
                        con.saveCommitment(commitment);
     
                        socket.close();
                    } catch (Exception ex) {
                        Logger.getLogger(VendorToClientConnection.class.getName()).log(Level.SEVERE, null, ex);
                    }
     
                }
            });
     
            thread.start();
        }
     
        public void disconnect(){
            try {
                serverSocket.close();
            } catch (IOException ex) {
                parent.log("ERROR: Could not disconnect.");
            }
        }
     
    }

    And this is the Certificate Table Model .. that comes the error

    package payword.user;
     
    import payword.User;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.table.AbstractTableModel;
    import payword.ClientCertificate;
     
    /**
     *
     * @author Thodoris
     */
    public class CertificateTableModel extends AbstractTableModel {
        int lines = 0;
        List<ClientCertificate> contents = new ArrayList<ClientCertificate>();
     
        public void refreshTable(User user){
            ClientDbConnection connection = new ClientDbConnection();
            contents = connection.getCertificateList(user);
            lines = contents.size();
            fireTableDataChanged();
            connection.close();
        }
     
        public int getRowCount() {
            return lines;
        }
     
        public int getColumnCount() {
            return 3;
        }
     
        public Object getValueAt(int rowIndex, int columnIndex) {
            ClientCertificate cert = contents.get(rowIndex);
            if(columnIndex == 0){
                return cert.getUserName();
            }else if(columnIndex == 1){
                return cert.getExpirationDate();
            }else{
                return cert.getAmmount();
            }
        }
     
        @Override
        public String getColumnName(int column){
            if(column == 0){
                return "Broker";
            }else if(column==1){
                return "Expires";
            }else{
                return "Ammount";
            }
        }
     
        public ClientCertificate getSelected(int i){
            return contents.get(i);
        }
    }
    Last edited by ThodorisVon; January 26th, 2010 at 05:47 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Exception (payword protocol)

    The exception is being thrown in the ClientWindow vendorConnectButtonActionPerformed() method, please post that code.

  5. #5
    Junior Member
    Join Date
    Jan 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception (payword protocol)

    here you are..

    private void vendorConnectButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                    
            int port = Integer.parseInt(portTf1.getText());
     
            int amount = (Integer)amountSpinner.getValue();
     
            int i = certificateTable.getSelectedRow();
            CertificateTableModel model = (CertificateTableModel)certificateTable.getModel();
     
            ClientCommitment commitment = new ClientCommitment(model.getSelected(i), new Date(), "", amount);
     
            vendorConnection = new ClientToVendorConnection(addressTf1.getText(), port, this, commitment, user);

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Exception (payword protocol)

    And here I thought you may have actually taken my first suggestion...Read the API for getSelectedRow() JTable (Java 2 Platform SE 5.0)
     int i = certificateTable.getSelectedRow();
    i will be -1 if there are no rows selected, and you cannot access an ArrayList index at -1 in the following line of code
    model.getSelected(-1) ///will throw an exception

    Fix is to make sure i is within the bounds of your list
    Last edited by copeg; January 27th, 2010 at 01:23 PM.

  7. #7
    Junior Member
    Join Date
    Jan 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception (payword protocol)

    ill give it a try
    tnx for your help copeg!

Similar Threads

  1. Exception-Based Problem
    By Hax007 in forum Exceptions
    Replies: 1
    Last Post: December 10th, 2009, 03:53 AM
  2. Exception Errors
    By TIMBERings in forum Exceptions
    Replies: 1
    Last Post: December 10th, 2009, 02:13 AM
  3. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM
  4. Error of "ClassNotFound Exception"
    By multicoder in forum Exceptions
    Replies: 3
    Last Post: July 1st, 2009, 02:58 PM
  5. Exception handling
    By AnithaBabu1 in forum Exceptions
    Replies: 6
    Last Post: August 27th, 2008, 09:37 AM