-
Programming problem
This isn't complete, but I am getting a null pointer exception using the string buffer class in my while loop in main.
Is there any reason the test doesn't pick it up? Or is this something weird with the StringBuffer?
Output seems fine everything else checks out, but this thing is giving me some trouble.
Code:
import java.io.*;
public class CaesarCipherKeys
{
public static void main(String[] args) throws IOException
{
StringBuffer currentLine= new StringBuffer();//holds the results of fileIn
char[] cipherKey, //array that holds the original key from keys.txt, before trnslation take place.
translated;//Holds the Translated cipher alphabet.
BufferedReader key=
new BufferedReader(new FileReader("keys.txt"));
currentLine=new StringBuffer (fileIn(key));
while(currentLine!=null)
{
System.out.println(currentLine);
cipherKey=newCipher(currentLine);
System.out.println(cipherKey);
currentLine=new StringBuffer(fileIn(key));
}
}
public static String fileIn(BufferedReader key)throws IOException
{
String currentLineWithSpace=key.readLine();
return currentLineWithSpace;
}
public static char[] newCipher(StringBuffer currentLineWithSpace)
{
String alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
currentLineWithSpace.append(alphabet);
String cypherLine=currentLineWithSpace.toString();
String currentLineFinal=cypherLine.replace(" ","");
char[] cipherKey=currentLineFinal.toCharArray();
char[] translatedKey;
char temp;
boolean test;
int ct= cipherKey.length;
System.out.println();
for (int i=0;i<ct;i++)
{
temp=cipherKey[i];
test=false;
//for(int si=0;si<=
}
return cipherKey;
}
}
----jGRASP exec: java CaesarCipherKeys
ALAN TURING
ALANTURINGABCDEFGHIJKLMNOPQRSTUVWXYZ
SOFTWARE ENGINEERING
SOFTWAREENGINEERINGABCDEFGHIJKLMNOPQRSTUVWXYZ
DATA ABSTRACTION
DATAABSTRACTIONABCDEFGHIJKLMNOPQRSTUVWXYZ
I LOVE FLOWERS
ILOVEFLOWERSABCDEFGHIJKLMNOPQRSTUVWXYZ
APPLE PICKING TIME
APPLEPICKINGTIMEABCDEFGHIJKLMNOPQRSTUVWXYZ
ORANGE PUMPKINS
ORANGEPUMPKINSABCDEFGHIJKLMNOPQRSTUVWXYZ
Exception in thread "main" java.lang.NullPointerException
at java.lang.StringBuffer.<init>(StringBuffer.java:12 0)
at CaesarCipherKeys.main(CaesarCipherKeys.java:21)
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
-
Re: Programming problem
Post the full exception plus stack trace and highlight the line it occurs on in your code.
-
Re: Programming problem
currentLine=new StringBuffer(fileIn(key));
this line, 21
-
Re: Programming problem
"Post the full exception plus stack trace ..."
-
Re: Programming problem
Tolls, I believe it is at the bottom of the original post.
So you know where the problem is, but you don't see what is causing it. Since that line is essentially doing 2 things, 1) Calling the "fileIn" method to retrieve a String, and 2) Creating a StringBuffer from that String, try breaking it up and printing out the String being returned from "fileIn"... Maybe that will help point you in the right direction.
Actually, the original exception tells you what is wrong..
Code:
Exception in thread "main" java.lang.NullPointerException
at java.lang.StringBuffer.<init>(StringBuffer.java:12 0)
at CaesarCipherKeys.main(CaesarCipherKeys.java:21)
You are seeing a null pointer exception when you are creating the StringBuffer.
-
Re: Programming problem
Ah, missed it amongst all the other jGrasp stuff.
Apologies!
As StormyWaters points out, there's only one thing that can be null.
What does the API say about what BufferedReader.readLine() can return?
-
Re: Programming problem
I did I am aware that it returns null, I was asking if the string buffer can handle a null value. I have the while loop testing to check if it is not null.
should I have a string as the while loop condition?
-
Re: Programming problem
So works fine with a string, i'll just make another function to make the buffer for later use
-
Re: Programming problem
The compiler has tied your call to the String constructor of the StringBuffer and inside that it calls the length() method on the parameter, so no it can't.
The append() method does a null check.
But the thing is that 'null' from readLine() means you've hit the end of the file.
Most people use that to indicate they should stop processing a file.
-
Re: Programming problem