I'm making a program to generate a random password with the desired length and chars.
All went smooth until i got to the generation itself. The error exists in this code:
package generate; import gui.PassPanel; import java.util.Random; public class GeneratePass { private final String[] letters = new String[] { "a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H", "i","I","j","J","k","K","l","L","m","M","n","N","o","O","p","P", "q","Q","r","R","s","S","t","T","u","U","v","V","w","W","x","X", "y","Y","z","Z" }; private final String[] numbers = new String[] { "0","1","2","3","4","5","6","7","8","9" }; private final String[] both = new String[] { "a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H", "i","I","j","J","k","K","l","L","m","M","n","N","o","O","p","P", "q","Q","r","R","s","S","t","T","u","U","v","V","w","W","x","X", "y","Y","z","Z","0","1","2","3","4","5","6","7","8","9" }; String pass = ""; public GeneratePass(String chars, int length) { String[] charsUsing; if (chars.equals("both")) { charsUsing = both; } else if (chars.equals("letters")) { charsUsing = letters; } else{ charsUsing = numbers; } Random r = new Random(charsUsing.length); for (int i = 0; i < length; i++) { pass = pass + charsUsing[r.nextInt()]; } PassPanel.setPassword(pass); } }
And I get this exception:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1165488049
at generate.GeneratePass.<init>(GeneratePass.java:44)
at gui.PassPanel$MyOkButtonListener.actionPerformed(P assPanel.java:103)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2012)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2335)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:404)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.jav a:6268)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
at java.awt.Component.processEvent(Component.java:603 3)
at java.awt.Container.processEvent(Container.java:204 5)
at java.awt.Component.dispatchEventImpl(Component.jav a:4629)
at java.awt.Container.dispatchEventImpl(Container.jav a:2103)
at java.awt.Component.dispatchEvent(Component.java:44 55)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4633)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4297)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4227)
at java.awt.Container.dispatchEventImpl(Container.jav a:2089)
at java.awt.Window.dispatchEventImpl(Window.java:2517 )
at java.awt.Component.dispatchEvent(Component.java:44 55)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:649)
at java.awt.EventQueue.access$000(EventQueue.java:96)
at java.awt.EventQueue$1.run(EventQueue.java:608)
at java.awt.EventQueue$1.run(EventQueue.java:606)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:105)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:116)
at java.awt.EventQueue$2.run(EventQueue.java:622)
at java.awt.EventQueue$2.run(EventQueue.java:620)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:105)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 619)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:138)
It points to this line of code:
pass = pass + charsUsing[r.nextInt()];
Please help me!