Results 1 to 10 of 10
  1. #1
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

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

    Java 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.
    Last edited by lenois; 02-05-2013 at 05:35 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Programming problem

    Post the full exception plus stack trace and highlight the line it occurs on in your code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: Programming problem

    currentLine=new StringBuffer(fileIn(key));

    this line, 21

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Programming problem

    "Post the full exception plus stack trace ..."
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default 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..
    Java 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.
    Last edited by StormyWaters; 02-05-2013 at 05:52 PM. Reason: Saw the exception...

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default 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?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

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

  8. #8
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: Programming problem

    So works fine with a string, i'll just make another function to make the buffer for later use

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    depsai is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Programming problem
    By pyler in forum New To Java
    Replies: 3
    Last Post: 12-08-2012, 06:11 AM
  2. Simple Problem with Object-Oriented Programming...
    By CuppaCoffee in forum New To Java
    Replies: 12
    Last Post: 01-04-2012, 10:12 PM
  3. Problem with Graphics OO programming Java.
    By Martino in forum Java 2D
    Replies: 2
    Last Post: 04-27-2011, 02:33 PM
  4. A challenging Engineering Programming problem
    By datastructures in forum New To Java
    Replies: 4
    Last Post: 12-03-2010, 10:36 PM
  5. [SOLVED] Problem with socket programming(Vista)
    By jagatsastry in forum Networking
    Replies: 1
    Last Post: 02-02-2009, 03:56 PM

Posting Permissions

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