-2

im using a function called decrypt to decrypt my encrypted strings in a file using the bufferedreader but the problem is when i use the function it throws NullPointerException, can any one help me? Thanks in advance!

This is my functon:

public  String Decrypt (String Word,int key)
{
   String result="";
   for (int i=0;i<Word.length();i++)
    {
        result+=(char)(Word.charAt(i)-key);
    }
   return result;
}

bufferedreader code:

 try {
       BufferedReader out=new BufferedReader(new FileReader("array.txt"));
       String line="";
       while((line=Decrypt(out.readLine(), 30)) !=null) // unknown exception
       {
         output.append("Your String is: \n"+ line);
       }
     }catch (FileNotFoundException ex) {

          } catch (IOException ex) {

          }
menna
  • 41
  • 1
  • 10
  • 4
    Your empty catch blocks are not a good sign. But out being null suggests that your path to your file is incorrect. – Hovercraft Full Of Eels Dec 11 '16 at 21:06
  • i checked the path to my file its correct 100% sure of that and do you think my exception being empty is the main problem? if yes what can i add inside the catch blocks? ( sorry as i am a beginner in java) – menna Dec 11 '16 at 21:24
  • i've also added the complete path to the file with no use – menna Dec 11 '16 at 21:27
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – 1615903 Dec 14 '16 at 06:24

1 Answers1

1

This is not clean:

while((line=Decrypt(out.readLine(), 30)) !=null)

We know that readline will return null when the BufferedReader reaches the end of the stream as per the BufferedReader API, but your code does not properly handle this, namely your Decrypt method will call call the length() method on the Word parameter whether or not it is null. I suggest that you not try to make your code too brief and instead separate out the reading of the Reader from acting on it. e.g.,

while((line= out.readLine) !=null) {
    line = Decrypt(line, 30);
    output.append("Your String is: \n"+ line);
}

As an aside, you will want to learn and use Java naming conventions. Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others.

Aside 2: don't leave your exception's catch blocks empty as this is the Java equivalent of driving your car with youer eyes closed. At least print out the stack trace and definitely check out the Exceptions Tutorial

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I've edited my code according to your example but i guess the problem is from the decrypt function it self specifically in the for loop ( the compiler stops at the exception their) – menna Dec 11 '16 at 22:12
  • @menna: Please clarify as I've no idea what your comment above means. Perhaps you will want to edit your question and update it for us. – Hovercraft Full Of Eels Dec 11 '16 at 22:19
  • when i press in my console the exception is specified automatically using the compiler ( Netbeans ) it goes back to my decrypt function – menna Dec 11 '16 at 22:35
  • @menna: ..... OK. But I'm still not clear -- are you asking something? – Hovercraft Full Of Eels Dec 11 '16 at 23:18