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;
}
}
passStringis uninitialized, hence the cause of yourNullPointerExceptionrandomNumString, hence it will always be nullYour “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…
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