-1

The project is to encoding decoding: abcdefghijklmnopqrstuvwxyz kngcadsxbvfhjtiumylzqropwe at first, program should asks the user to enter a key string like above. The program will then read the file and output to the console its encoded contents.Capital letters are mapped the same way as the lower case letters above, but remain capitalized.For example, with the key above, every 'A' becomes a 'K' when encoding a text. Numbers and other characters are not encoded and remain the same. For example, with the mapping above, every 'a' becomes a 'k' when encoding a text, and every 'k' becomes an 'a' when decoding.

Run 1: Enter encoding key: maxnrslkbpwfzjidouetchgvyq Enter the file name: sampleInput.txt ************** Encoded Contents **************** Grfxizr ti XE 20A! Xizdctrue mur liin mt siffigbjl bjetucxtbije, act jit mt urmnbjl yicu zbjn. Bs yic mctizmtr m zree, yic lrt mj mctizmtrn zree.


Good Bye!

I get error : Exception in thread "main" java.lang.NullPointerException

on line char outputFile= hmap.get(FileChar); C:\Users.....\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1

which I want assign input file to hash map get value.

is it the reason because I dont make key value about other characters except [a-z] in hashmap like“, ! ," etc, then it dont have this key value then make null exception?

Here is some code for hashmap :

HashMap<Character,Character>hmap = new HashMap<>();
  for(int j =0; j<26; j++){

      if(Character.isUpperCase(originalResult.charAt(j))){     
          hmap.put(originalResult.charAt(j),Character.toUpperCase(inputResult.charAt(j)));
      }
      else{
      hmap.put(originalResult.charAt(j),inputResult.charAt(j));
      }
  }

Here is some code I think the problem:

while((Fileline =bin.readLine())!=null )
{
    char[] FileArray = Fileline.toCharArray();

    for(int j=0;j<Fileline.length();j++ )
    {
        FileChar =FileArray[j];  
        char outputFile= hmap.get(FileChar);   ////here is the problem   
    }

    System.out.println( String.valueOf(outputFile));
}

I think the problem might be the char when I print like:

while((Fileline =bin.readLine())!=null ){

    char[] FileArray = Fileline.toCharArray();

   for(int j=0;j<Fileline.length();j++ ){
          FileChar =FileArray[j]; 
          s = new String(String.valueOf(FileChar));

  }
   System.out.println(s);

the result is nothing:

y

.

but when I print in the loop,it looks each line only have one letter,and lots of line:

while((Fileline =bin.readLine())!=null ){

    char[] FileArray = Fileline.toCharArray();

   for(int j=0;j<Fileline.length();j++ ){
          FileChar =FileArray[j]; 
          s = new String(String.valueOf(FileChar));
         System.out.println(s);
  }
Jongware
  • 22,200
  • 8
  • 54
  • 100
Zhu Sijia
  • 1
  • 1
  • 3
  • Use camel casing in Java (fileChar instead of FileChar) for variables and methods, it looks like FileChar is a reference to a static class. If this is the case then that is the beginning of the problem. – Kafros Sep 06 '16 at 14:56
  • I have change to uppercase of first letter to lowercasse, I think the problem might be the valueof(char)? I not sure and I dont know how to fix – Zhu Sijia Sep 06 '16 at 15:03
  • Post the full stack trace (error), not just the pointer exception – Kafros Sep 06 '16 at 15:21
  • Exception in thread "main" java.lang.NullPointerException at Main.main(Main.java:140) C:\Users\.......\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 which is BUILD FAILED (total time: 7 seconds) – Zhu Sijia Sep 06 '16 at 15:24

1 Answers1

0

The reason you get no output (only the final character of your File) is because you are re-instantiating a new String every time you call:

s = new String(String.valueOf(FileChar));

Try this:

    char[] FileArray = Fileline.toCharArray();
    s = "";
       for(int j=0;j<Fileline.length();j++ ){
              FileChar =FileArray[j]; 
              s = s + String.ValueOf(FileChar);   
      }
  System.out.println(s);

same thing but cleaner:

      char[] FileArray = Fileline.toCharArray();
    s = "";
       for(int j=0;j<Fileline.length();j++ ){
              s += String.ValueOf(FileArray[j]); 
      }
  System.out.println(s);

Finally:

    while((Fileline =bin.readLine())!=null ){

        char[] FileArray = Fileline.toCharArray();
        s = "";
        for(int j=0;j<Fileline.length();j++ ){
              s += String.ValueOf(FileArray[j]);
        }
        System.out.println(s);
}

You get the null pointer exception because hmap does not have a value for every character you are trying to 'get'. If you haven't put a value for the key 'a', you cannot expect to get a value for the key 'a'; For example if I do:

hmap.put('a', 'd');

hmapt.get('z');

Will reutrn a null pointer exception since a value for the key 'z' does not exist, only for 'a' which is 'd'.

If you are not compiling at all, that is because you have not initialised some variable 'hmap' or 'FileChar' in the scope that you are running the '.get(FileChar)'.

Kafros
  • 166
  • 14
  • yes, you are right, if I change to s += String.ValueOf(FileArray[j]) , char works fine. However, I do this is only for check hashmap get(), and char outputFile= hmap.get(fileChar); here is have problem:Exception in thread "main" java.lang.NullPointerException – Zhu Sijia Sep 06 '16 at 15:14