NullPointerException

I can't see why this code doesnt work. maybe someone else can help me see the problem? I get a NullPointerException when it reaches either of the inner IF statements that decide where the letter should be assigned.

if this isnt clear enough please aks and I will try to clarify!

THANKS! =)

public VowelCons(String str)//CTor that takes in a string from user

{

String lwrstr = str.toLowerCase();// assign the String lwrstr all lowercase letters from String str

char [] strArray = lwrstr.toCharArray();// convert the String lwrstr into a character array strArray

for (int i = 0; i < strArray.length; i++)

{

// if index[i] of the array strArray is equal to any of these vowels then....

if(strArray[i] =='a' || strArray[i] =='e' ||

strArray[i] =='i' || strArray[i] =='o' || strArray[i] =='u')

{

// another counter to try and keep the array index together and no null indexes in between

for(int j = 0; j <= strArray[j]; j++)

{

vowels[j] = strArray[i];// assign strArray[i] to the character array vowels[j] NullPointerException ERROR

}

}

else// or else they are consonants

{

// another counter to try and keep the array index together and no null indexes in between

for(int k = 0; k <= strArray[k]; k++)

{

consonants[k] = strArray[i];// assign strArray[i] to the character array consonants[k] NullPointerException ERROR

}

}

}

}

[2580 byte] By [Blue_Chimeraa] at [2007-11-27 11:29:29]
# 1

Have you actually initialised the vowel and consonant arrays?

floundera at 2007-7-29 16:28:14 > top of Java-index,Java Essentials,New To Java...
# 2

> Have you actually initialised the vowel and consonant

> arrays?

no, hmmm.... let me try that and see if that works

thanks!

Blue_Chimeraa at 2007-7-29 16:28:14 > top of Java-index,Java Essentials,New To Java...
# 3

Another thing, I'm not sure what you are trying to do but I'm sure this won't solve it.

for(int j = 0; j <= strArray[j]; j++) {

vowels[j] = strArray[i];

}

floundera at 2007-7-29 16:28:14 > top of Java-index,Java Essentials,New To Java...
# 4

Back to the drawing board.. =(

Thank you for the help flounder...

I was trying to sort consonants and vowels seperately into different arrays and then count them.. I forgot that you had to initialize the arrays and so I dont see it working.

Thanks again!

-Jason

Blue_Chimeraa at 2007-7-29 16:28:14 > top of Java-index,Java Essentials,New To Java...
# 5

Why bother with arrays.

loop over string {

if current char is a vowel {

increment vowel count;

} else {

increment consonant count;

}

}

Note this will not take into account other chars such as punctuation and whitespace.

floundera at 2007-7-29 16:28:14 > top of Java-index,Java Essentials,New To Java...
# 6

I am frustrated at the moment.. this is a project assignment (homework) form y java class. I have worked with this for several hours now and I am ready to give up for the night. =)

so, I need to do this project the way it is set up in the description on the sheet our instructor gave us.. that is why i am doing it the way i am =)

Thanks again!

Blue_Chimeraa at 2007-7-29 16:28:14 > top of Java-index,Java Essentials,New To Java...
# 7

If your instructions tell you to use arrays then tell your teacher he/she is an idiot. You should always use the right tool for the job and using arrays to solve this is overkill.

floundera at 2007-7-29 16:28:14 > top of Java-index,Java Essentials,New To Java...
# 8

lol!!

well, We only implement what we have learned thus far. therefore I think that is why it is the way it is =)

and.. I am Not defending the assignment our instructor is awesome.. ITT TECH wrote the assignment.. he just gave it to us =)

see ya around the forums! (as I am sure I will be back with more questions) =)

Blue_Chimeraa at 2007-7-29 16:28:14 > top of Java-index,Java Essentials,New To Java...
# 9

> lol!!

> well, We only implement what we have learned thus

> far. therefore I think that is why it is the way it

> is =)

I understand that but you should have already been introduced to Strings, variables, incrementing variables, loops and if statements. All of these are needed to solve the problem as per my pseudocode in above reply. There is no need to use arrays. Arrays simply make this task harder. If your teacher wants to teach you how to use arrays then they need to come up with a better exercise as this one BITES!

floundera at 2007-7-29 16:28:14 > top of Java-index,Java Essentials,New To Java...