Results 1 to 19 of 19
  1. #1
    lazarat is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Angry (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?

    Java 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();
    	}
    }
    Last edited by Fubarable; 09-28-2012 at 04:43 AM. Reason: corrected code tags

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.
    Last edited by Fubarable; 09-28-2012 at 04:29 AM.

  3. #3
    lazarat is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default 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
    Last edited by lazarat; 09-28-2012 at 04:32 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: (help) morse code java => null

    Quote Originally Posted by lazarat View Post
    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.

  5. #5
    lazarat is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default 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!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  7. #7
    lazarat is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default Re: (help) morse code java => null

    Quote Originally Posted by Fubarable View Post
    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;?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: (help) morse code java => null

    Quote Originally Posted by lazarat View Post
    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?

    maybe i need to add an else statement after the bracket under found=true;?
    No, there are other problems going on, I think.

  9. #9
    lazarat is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default Re: (help) morse code java => null

    do i need to declare word as string at the top?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  11. #11
    lazarat is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default 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:(

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.
    Last edited by Fubarable; 09-28-2012 at 05:18 AM.

  13. #13
    lazarat is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default Re: (help) morse code java => null

    no no my bad i missed the sopl with the j variable thats the one causing the problem

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: (help) morse code java => null

    I think it's this one:
    Java Code:
    System.out.println(code[j]+" ");
    You can test this by changing it *temporarily* to:
    Java Code:
    System.out.println("is this null? " + code[j]+" ");
    Please do so.

  15. #15
    lazarat is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default 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!!

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.
    Last edited by Fubarable; 09-28-2012 at 05:30 AM.

  17. #17
    lazarat is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default 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

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: (help) morse code java => null

    Quote Originally Posted by lazarat View Post
    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.

  19. #19
    lazarat is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default Re: (help) morse code java => null

    for(int i=0; i<letters.length; i++){
    sopl (letters[i] + " " + code[i]);
    }

    ineed this to be added?

Similar Threads

  1. Morse Code Help Please!!
    By javaa in forum New To Java
    Replies: 25
    Last Post: 09-28-2012, 04:28 AM
  2. Why my code does not work without null ?
    By fatabass in forum New To Java
    Replies: 6
    Last Post: 01-12-2012, 11:27 PM
  3. Morse code translation?
    By Koba in forum New To Java
    Replies: 6
    Last Post: 10-10-2011, 12:48 AM
  4. Morse Code Java Program - Help Needed Please!!
    By dungeondragon in forum New To Java
    Replies: 8
    Last Post: 02-25-2011, 05:36 AM
  5. Morse Code
    By Jamison5213 in forum New To Java
    Replies: 6
    Last Post: 04-14-2010, 05:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •