4 Replies - 289 Views - Last Post: 04 November 2011 - 03:15 PM Rate Topic: -----

#1 autodog  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 24-May 08

Can't set random chars in a String[] and display it in textfield

Posted 03 November 2011 - 03:20 PM

Hi :wink:

I'm using NetBeans IDE 6.8 and have created a desktop application that will generate a random password.
There is a generate button which has an action applied to it. And thats where things go right and wrong.

@Action
    public void generatePsw() {
        Random rndm = new Random();
        char[] cChars = new char[] {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9' };
        int rndNum = rndm.nextInt(cChars.length);
        char rndmChar = cChars[rndNum];
        String[] sPassw = null;
        for( int i=1;i <= Integer.parseInt(jTextNumOfChars.getText()); i++ ) {
            sPassw[i] = Character.toString(rndmChar);
        }
        jLabel1.setText("" + sPassw );
    }



When I run my code everything seems Ok but when I try to generate a password it halts and throws an exception.
I am suspicious about using a for -loop with Character.toString(rndmChar). Is this my problem? How can I solve this? It works fine using only one character with jLabel1.setText("" + rndmChar ); But want my app to print more than one character.

EXCEPTION:
Exception in thread "AWT-EventQueue-0" java.lang.Error: java.lang.reflect.InvocationTargetException
        at org.jdesktop.application.ApplicationAction.actionFailed(ApplicationAction.java:859)
        at org.jdesktop.application.ApplicationAction.noProxyActionPerformed(ApplicationAction.java:665)
        at org.jdesktop.application.ApplicationAction.actionPerformed(ApplicationAction.java:698)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253)
        at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
        at java.awt.Component.processMouseEvent(Component.java:6203)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
        at java.awt.Component.processEvent(Component.java:5968)
        at java.awt.Container.processEvent(Container.java:2105)
        at java.awt.Component.dispatchEventImpl(Component.java:4564)
        at java.awt.Container.dispatchEventImpl(Container.java:2163)
        at java.awt.Component.dispatchEvent(Component.java:4390)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)
        at java.awt.Container.dispatchEventImpl(Container.java:2149)
        at java.awt.window.dispatchEventImpl(window.java:2478)
        at java.awt.Component.dispatchEvent(Component.java:4390)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java: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.doIntersectionPrivilege(AccessControlContext.java:105)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(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.doIntersectionPrivilege(AccessControlContext.java:105)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:619)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:616)
        at org.jdesktop.application.ApplicationAction.noProxyActionPerformed(ApplicationAction.java:662)
        ... 38 more
Caused by: java.lang.NullPointerException
        at passwordgenerator.PasswordGeneratorView.generatePsw(PasswordGeneratorView.java:239)



Sorry for my bad english <-- swedish

Is This A Good Question/Topic? 0
  • +

Replies To: Can't set random chars in a String[] and display it in textfield

#2 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: Can't set random chars in a String[] and display it in textfield

Posted 03 November 2011 - 03:44 PM

Line 7 in the top code box up there, you're setting your array to null, meaning there are no elements in it, but then you're trying to assign to nonexistent elements on line 9. Why are you using an array for this? It would be simpler to just use a string, and concatenate to it on each iteration of the loop. You'll also probably want to generate a new random character on each iteration of the loop -- right now you'll end up with a password of all the same character (wwwwww or something like that).
int rndNum;
char rndmChar;
String sPassw = "";
for( int i=1;i <= Integer.parseInt(jTextNumOfChars.getText()); i++ ) {
    rndNum = rndm.nextInt(cChars.length);
    rndmChar = cChars[rndNum];
    sPassw += Character.toString(rndmChar);
}

This post has been edited by Tryparse: 03 November 2011 - 03:45 PM

Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,300
  • Joined: 20-September 08

Re: Can't set random chars in a String[] and display it in textfield

Posted 03 November 2011 - 04:22 PM

Better still, use a StringBuilder. You should avoid string concatenation,particularly in a loop.

int len = Integer.parseInt(jTextNumOfChars.getText());// Validation would be good
StringBuilder sb = new StringBuilder(len); // Now append to it

This post has been edited by g00se: 04 November 2011 - 11:39 AM

Was This Post Helpful? 0
  • +
  • -

#4 autodog  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 24-May 08

Re: Can't set random chars in a String[] and display it in textfield

Posted 04 November 2011 - 11:15 AM

Tryparse: Thanks alot for your help =)

g00se: I'll try using StringBuilder
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Can't set random chars in a String[] and display it in textfield

Posted 04 November 2011 - 03:15 PM

The String class has a constructor that accepts a char[] array as parameter

char[] cChars = {'a', 'b', 'c',....};
Randon ran = new Random();
char[] rdmChar = new char[ran.nextInt(5) + 8];
for(int i = 0; i < rdmChar.length; ++i)
   rdmChar[i] = cChars[ran.nextInt(cChars.length)];
String sPassw = new String(rdmChar);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1