Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8468695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:00:29+00:00 2026-06-10T16:00:29+00:00

I am having a chop at making a password generator and manager. I was

  • 0

I am having a chop at making a password generator and manager. I was creating the password by generating a character at a time from a for loop. At the end of each for loop iteration it would print out the chosen random character. I have been making an attempt to store the password char by char into the index of an array that relates to the value of i in the for loop. That is rather than it just printing and me not being able to do anything. As such I have two questions, my main one is: is it possible to capture the chars that I print and then store them in a string? Or, as in the code that it below, can I avoid the null pointer exceptions that I get upon running (via my arrays)? The null pointer exceptions occur when i try to assign a String to my passwordString at the index i. It may be better explained by my code.
Thanks for any help 🙂

package pass.gen;


public class PassGen {


    public static void main(String[] args) {
        PassGen passGen = new PassGen();
        passGen.generate();
    }

    String lAlpha = "abcdefghijklmnopqrstuvwxyz"; //used to generate lowercase pass chars
    String uAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //used to generate uppercase pass chars
    int minNum = 0; //the mininum number that a integer value can be in the password
    int maxNum = 9; //the max number "" ...........................................""
    int randomNumber; 
    int minString = 0;
    int maxString = 25;
    int randomLNum;
    int randomUNum;
    int low1 = 1;
    int high3 = 3;
    int ran3;
    char randomLChar;
    char randomUChar;
    char randomNumChar;
    String randomLString;
    String randomUString;
    String randomNumString;
    String passString []; //should this be an array of chars? 
    boolean case1; //if the case is true (case1 is for random numbers) then a random number is added to the passString
    boolean case2; //if the case is true (case2 is for random lowercase letters) then a random lowercase letter is added to the passString
    boolean case3; //if the case is true (case3 is for random uppercase letters) then a random uppercase letter is added to the passString

    void generate(){
        for(int i = 0; i < 4; i++){
            ran3 = low1 + (int)(Math.random() * ((high3 - low1) + 1));
            switch(ran3){
            case 1:genNumber(0,9);
            break;
            case 2:genLAlpha();
            break;
            case 3:genUAlpha();
            break;
            default:System.out.println("Unable to Generate a Password.");
            }
            if(case1 == true){
                passString[i] = randomNumString;

            }if(case2 == true){
                passString[i] = randomLString;

            }if(case3 == true){
                passString[i] = randomUString;
            }
        }
        System.out.println(passString);
    }

    void genNumber(int min, int max){
        randomNumber = min + (int)(Math.random() * ((max - min) + 1));
        randomNumChar = Character.forDigit(randomNumber,5);
        case1 = true;
    }

    void genLAlpha(){
        randomLNum = minString + (int)(Math.random() * ((maxString - minString) + 1));
        randomLChar = lAlpha.charAt(randomLNum);
        randomLString = Character.toString(randomLChar);
        case2 = true;
    }

    void genUAlpha(){
        randomUNum = minString + (int)(Math.random() * ((maxString - minString) + 1));
        randomUChar = uAlpha.charAt(randomUNum);
        randomUString = Character.toString(randomUChar);
        case3 = true;
    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:00:30+00:00Added an answer on June 10, 2026 at 4:00 pm
    • passString is uninitialized, hence the cause of your NullPointerException
    • genNumber does not set the value of randomNumString, hence it will always be null

    Your “generate” methods should return the value they generate. This will remove the reliance on the member variables, reducing the number of areas of possible problems. This also means you can add new generation methods without having to change a lot of code…

    Even if you choice not to do this, you should have a single variable that holds the result of each of these calculations…

    Try this instead…

    public class PassGen {
    
        public static void main(String[] args) {
            PassGen passGen = new PassGen();
    
            String generate = passGen.generate();
            System.out.println(generate);
    
        }
        String lAlpha = "abcdefghijklmnopqrstuvwxyz"; //used to generate lowercase pass chars
        String uAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //used to generate uppercase pass chars
        int minNum = 0; //the mininum number that a integer value can be in the password
        int maxNum = 9; //the max number "" ...........................................""
        int minString = 0;
        int maxString = 25;
        int low1 = 1;
        int high3 = 3;
    
        public String generate() {
            StringBuilder sb = new StringBuilder(4);
    
            for (int i = 0; i < 4; i++) {
                sb.append(generateCharacter());
            }
            return sb.toString();
        }
    
        protected char generateCharacter() {
            char result = '-';
            int ran3 = low1 + (int) (Math.random() * ((high3 - low1)));
            switch (ran3) {
                case 0:
                    result = genNumber(0, 9);
                    break;
                case 1:
                    result = genLAlpha();
                    break;
                case 2:
                    result = genUAlpha();
                    break;
            }
    
            return result;
    
        }
    
        protected char genNumber(int min, int max) {
            int randomNumber = min + (int) (Math.random() * ((max - min) + 1));
            char randomNumChar = Character.forDigit(randomNumber, 5);
            return randomNumChar;
        }
    
        protected char genLAlpha() {
            int randomLNum = minString + (int) (Math.random() * ((maxString - minString) + 1));
            char randomLChar = lAlpha.charAt(randomLNum);
            return randomLChar;
        }
    
        protected char genUAlpha() {
            int randomUNum = minString + (int) (Math.random() * ((maxString - minString) + 1));
            char randomUChar = uAlpha.charAt(randomUNum);
            return randomUChar;
        }
    }
    

    Also, I strongly suggest you make some time to learn how to use your IDE’s debugger. If you’re not using and IDE, I strongly suggest you start. It took me 30 seconds to add a break point and find your NullPointerException

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble making a sequence. Basically I need to chop a sequence
Having trouble with each function... Will try to explain by example... In my code,
Having a hard time with labels on a ggplot2 plot. Here's a similar plot
having read the docs, https://docs.djangoproject.com/en/dev/topics/signals/ i have created this in my signals.py file: from
Having this problem with my android app I struggled with this for some time
I am having problems using the split function in Ruby. /Users/simonprochazka/Downloads/pes_test_p00/lib/main.rb:28: premature end of
Having a heckuva time with this one, though I feel I'm missing something obvious.
Having worked with Classic ASP for about 2 years now by creating a few
Having to, for the first time, define relationships amongst related objects, I found myself
Having noticed a small mistake in my C# code (end of line 4): Domain.Models.Patient

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.