(help) morse code java => null
I have a hw assignment due today for reading a morsecode. I wrote the code but i do not know why it says null when i run it :(
help me please?
Code:
import java.util.*;
import java.io.*;
public class lab3 {
/**
* @param args
*/
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
Scanner scan=new Scanner (System.in);
//putting user input in a character array
char [] letters = new char [26];
String [] code = new String [26];
//object creation
Scanner infile = new Scanner (new File ("morsecode.txt"));
FileWriter fw = new FileWriter("results.txt");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter outfile = new PrintWriter(bw);
int ctr = 0;
while(infile.hasNext()){
letters [ctr] = infile.next().charAt(0);
code [0] = infile.next();
ctr++;
}
System.out.println("enter word or enter 'done' to quit");
String word= scan.nextLine();
while(!word.equals("done")){
word = word.toUpperCase();
outfile.print(word + " ");
for(int i=0; i<word.length(); i++){ // use a loop to iterate over user input stored in word
char let=word.charAt(i);
boolean found=false;
for (int j=0; j<letters.length && !found; j++){
if (letters[j]==let){
System.out.println(code[j]+" ");
outfile.print(code[j] + " ");
found=true;
}
}
}
outfile.println();
System.out.println("\nEnter another word or enter 'done' to quit");
word=scan.next();
}
outfile.close();
}
}
Re: (help) morse code java => null
Please show us the line that this throwing the NPE. Also please look at my link in my signature on how to use code tags so we can better be able to read and understand your posted code.
Also I've locked your hijack post. Please do not do this.
Re: (help) morse code java => null
it does not show any errors on eclipse. just when i hit ctr+f11 to run the program it declares enter word or enter 'done' to quit and when i type word and hit enter it says null instead of giving the morse code
edit: sorry for the hijack post was unintentional cuz thought i was not allowed to open new threads since was new memeber
Re: (help) morse code java => null
Quote:
Originally Posted by
lazarat
it does not show any errors on eclipse. just when i hit ctr+f11 to run the program it declares enter word or enter 'done' to quit and when i type word and hit enter it says null instead of giving the morse code
The error message will tell you what line is throwing the NullPointerException (or NPE) as the stack trace will give you a line number. Please look at your code and find that line and let us know which one it is. Again, please edit your original post and place [code] [/code] tags around the posted code.
Re: (help) morse code java => null
ugh i am sorry i am not so good at this stuff...it does not give an error code...it simply posts null null null (7times) and then posts again the message enter word or enter done to quit...and i edited the tags, is that correct way now? thanks for trying to help btw!
Re: (help) morse code java => null
OK, different tack. Check which lines are printing out null, and try to use that information to see what variable is null. If you do this, you'll likely find the error (which I see now).
Also, I fixed your bottom code tag. Note that it is different from the top code tag as it needs to have a slash in it: [/code]
As a hint, the word code will have special relevance to your problem as well.
Re: (help) morse code java => null
Quote:
Originally Posted by
Fubarable
OK, different tack. Check which lines are printing out null, and try to use that information to see what variable is null. If you do this, you'll likely find the error (which I see now).
Also, I fixed your bottom code tag. Note that it is different from the top code tag as it needs to have a slash in it: [/code]
As a hint, the word code will have special relevance to your problem as well.
variable j? not sure cuz all the lines print out null actually...
maybe i need to add an else statement after the bracket under found=true;?
Re: (help) morse code java => null
Quote:
Originally Posted by
lazarat
variable j? not sure cuz all the lines print out null actually...
No, which lines in your *program* are printing out the word null. You don't have System.out.println's on every line in your program, and so there are only a few places you need to look. Once you find the lines, find the variables that you are trying to print out (that are in the parentheses of the println method). That's the variable that's null -- so which variable is it?
Quote:
maybe i need to add an else statement after the bracket under found=true;?
No, there are other problems going on, I think.
Re: (help) morse code java => null
do i need to declare word as string at the top?
Re: (help) morse code java => null
First things first. Again, which variable is the one that's at fault here.
Please reply soon as I'm going to bed soon.
Re: (help) morse code java => null
sorry i can only think of adding
String word;
word = infile.nextLine();
above FileWriterfw = newFileWriter....
can you please tell me I need to submit it today:(
Re: (help) morse code java => null
No, I'm not asking you what to add or solve. First please let's find the source of the error!
Please show me which variable is null. Or show me the line of code that has printlin(something) in it that prints out null. Just do that, please and only that. If you're going to succeed in this course you will have to know not the solution to this current problem, but instead *how to debug*, and this first involves looking for the source of the problem.
Re: (help) morse code java => null
no no my bad i missed the sopl with the j variable thats the one causing the problem
Re: (help) morse code java => null
I think it's this one:
Code:
System.out.println(code[j]+" ");
You can test this by changing it *temporarily* to:
Code:
System.out.println("is this null? " + code[j]+" ");
Please do so.
Re: (help) morse code java => null
that must be it cuz in class when she explained she was confused too and gave us only that part starting with code[]. thanks I cannot run it now cuz i need the morsecode.txt file but i hope that was the only thing missing .... thanks again!!
Re: (help) morse code java => null
You haven't solved it yet. Now you have to figure out why the String array named code holds nulls. Look to where you try to fill the array. Please show me that code loop where you do this.
Re: (help) morse code java => null
code[0] stores into code [0] thats where it tries to fill the array i think
and charAt(0) reads up to a space while infile.next() reads up to a space or a new line character and then adds one to the counter and moves back up
Re: (help) morse code java => null
Quote:
Originally Posted by
lazarat
code[0] stores into code [0] thats where it tries to fill the array i think
So you're only assigning values to the 0'th item in the code array. All the other items in the array will be empty! Or in other words, only code[0] will hold a String while code[1] will hold null as will code[2], code[3], code[4], ... etc... So you must change the code where you try to fill this array so that all of the items in the array actually get assigned values.
Myself, I'd use a Map<Character, String>, but your mileage may vary.
Re: (help) morse code java => null
for(int i=0; i<letters.length; i++){
sopl (letters[i] + " " + code[i]);
}
ineed this to be added?