Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: My encounter with Mrs. NullPointerException

  1. #1
    Junior Member BlackStones's Avatar
    Join Date
    Feb 2014
    Posts
    15
    My Mood
    Fine
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default My encounter with Mrs. NullPointerException

    Hello companions,

    I've been working on a phone customer billing program for a few days now and I've been able to make considerable progress thanks to the aid of the members on this forum and the Oracle Java reference site. Yesterday, I made some minor changes to my Eclipse layout and basically from there I just started minimizing and maximizing and removing aspects of the interface unknowingly.

    Regardless, I have experienced a Null Pointer Exception and I've been doing the best debugging process I've ever done in my life. I still can't locate the resource or tip to help me get back on the right track.

    This is the program I am still working on;

    First Post (provides concept of program)

    This is my updated code;

    package spInputOutput;
     
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
     
    public class spInputOutput {
     
    	public static PrintWriter outputStream;
     
    	public static void main(String[] args) throws IOException
    	{
    		readData();
    	}
     
    	public static void readData() throws IOException
    	{
    		//Monthly Base Fee of $20
    		double monthlyBase = 20;
    		double startTime = 0.00;
    		double duration = 0.00;
    		//double amountDue = monthlyBase + (duration * startTime);
     
    		//ArrayList<Double> amtDue = new ArrayList<Double>();
    		ArrayList <String> accts = new ArrayList<String>();
    		//use 'contains' to check whether or not the account number has been accounted for, add or create new instance
     
    		double daytimeCall = 0.12; /*per minute for calls that started between 8:00 AM and 10:00PM, 
    		inclusive $0.05 per minute all other times*/
     
    		FileReader fr = new FileReader("input_data.txt");
    		BufferedReader br = new BufferedReader(fr);
     
    		String invoice = "invoice.txt";
            try {
    			outputStream = new PrintWriter(invoice);
    		}
            catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}
     
    		//String[] a = line.split(":"); for times
     
            outputStream.println("Invoice");
            outputStream.println("**********");
            outputStream.println("");
            outputStream.println("Account");
            outputStream.println("");
            outputStream.println("Amount Due");
            //Should eventually use algorithm to calculate the amounts due for each account
     
            /*
             Created to simulate values for Amounts Due
     
             for(int i=1; i<4; i++)
            {
            	double z = ((10+(i*2))+(i*(Math.random())));
            	outputStream.printf("%.2f", z);  
            	outputStream.println("\n"); 
            }
            */
     
            outputStream.println("");
            outputStream.println("**********");
            outputStream.println("Total:");
     
            String text = "";
            String line = br.readLine();
     
    		while(line != null)
    		{
    			text += line;
    			line = br.readLine();
     
                            //THE LINE BELOW IS WHERE MY PROGRAM IDENTIFIES THE NULL POINTER EXCEPTION
    			String [] a = line.split(" ");
     
    			//Account numbers loop
    			for(int i=0; i<1; i++)
    			{
    				//If account number is already in array if()
    				accts.add(a[i]);
    				outputStream.println(a[i]);
    			}		
    		}
     
    		System.out.println(text);
     
    		System.out.println("Inovice File Incorrectly Created");
    		System.out.println(accts.get(1));
     
    		outputStream.close();
    		br.close();
     
    	}
    }

    This is the message displayed in the console after attempting to run;

    Exception in thread "main" java.lang.NullPointerException
    at spInputOutput.spInputOutput.readData(spInputOutput .java:79)
    at spInputOutput.spInputOutput.main(spInputOutput.jav a:18)


    As always, I am grateful for assistance. Any suggestions are welcome, thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My encounter with Mrs. NullPointerException

    Exception in thread "main" java.lang.NullPointerException
    at spInputOutput.spInputOutput.readData(spInputOutput .java:79)
    Check the order in which a line is read and when the contents of line is checked for null. It looks like the code does it this way:
    read a line
    check if line is null
    read another line (what happened to first line read)
    use the contents of line (Without checking if null)
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: My encounter with Mrs. NullPointerException

    while(line != null)
    		{
    			text += line;
    			line = br.readLine();
     
                            //THE LINE BELOW IS WHERE MY PROGRAM IDENTIFIES THE NULL POINTER EXCEPTION
    			String [] a = line.split(" ");
    The line variable is null. This is because the br.readLine() method returned null. The while condition is ONLY checked at the beginning of each iteration of the loop. So, if the line variable becomes null during a loop iteration, the loop is not terminated automatically. In most cases, the dependent variables in the while condition (such as: line, in this case) should only be reevaluated as the last step in your loop, not in the beginning. There are several exceptions to this, but I'm just talking about generalization right now.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. The Following User Says Thank You to aussiemcgr For This Useful Post:

    GregBrannon (March 11th, 2014)

  5. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My encounter with Mrs. NullPointerException

    Very good explanation.

Similar Threads

  1. NullPointerException...how?
    By illusionust in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 13th, 2013, 02:05 AM
  2. NullPointerException?
    By NTWolf1220 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 5th, 2013, 11:46 AM
  3. why am I getting NullPointerException.
    By mia_tech in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 24th, 2012, 11:33 AM
  4. [SOLVED] Nullpointerexception
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 14th, 2010, 10:33 PM
  5. [SOLVED] NullPointerException
    By javapenguin in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2010, 12:10 AM