Results 1 to 8 of 8
  1. #1
    ecstokely is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default Need help! Cant Assign values to 2 dimensional Array!!!

    I'm working on a class lab, and unfortunately I can't make it to any open lab hours to get help. I am having trouble assigning values to a 2 dimensional array. I believe i have all the syntax right, but when I run i get this error:

    Exception in thread "main" java.lang.NullPointerException
    at Precipitation.readFile(Precipitation.java:52)
    at Precipitation.main(Precipitation.java:118)

    The values being stored are from a text file. I have added some println's to make sure I am getting the correct values and storing them as doubles. But when I try assigning them to any index in the array I get my errors. I have been staring for hours and can't figure it out. A nudge in the right direction would be very helpful! Thanks in advance.

    Heres what I have:
    Java Code:
    import java.io.*;			// to use file streams
    import java.text.*;			// to format numbers for printing
    import java.util.Scanner;
    
    
    /**
    This class can be used to process precipitaion data for 5 years. 
    It can create a table of the data as well as monthly averages and yearly totals.
    It will also find the maximum and minimum precipitation and display it with the table.
    Written by YOUR NAME HERE
    TODAY'S DATE HERE
    */
    public class Precipitation 
    {
    	private final static int MONTHS = 12;
    	private final static int YEARS = 5;
    	private final static int STARTYEAR = 1995;
    	private final String[] monthLabel = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
    		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
    	
    	private double[][] percipitationAmount;
    
    	//DECLARE ARRAYS HERE FOR TASK #1
    
    	public Precipitation()
    	{
    		
    		double yearTotal[]; 
    		double monthlyAverage[];
    		double percipitationAmount[][];
    		
    		yearTotal = new double[YEARS];
    		monthlyAverage = new double[MONTHS];
    		percipitationAmount =new double[12][5];
    	
    	}
    
    	public void readFile(Scanner infile)
    	{
    		//CREATE A LOOPING STRUCTURE TO READ DATA FROM THE FILE AND
    		infile.useDelimiter("\n");
    		String line;
    		double num;
    		for(int y = 0; y < YEARS;y++)
    		{
    			for(int m = 0; m < MONTHS; m++)
    			{
    				line = infile.nextLine();
    				num = Double.parseDouble(line);
    				
    				//Added to test that scanner is getting correct values
    				
    				//System.out.println("Month: " + (m +1));
    				//System.out.println("Year: " + (y + STARTYEAR));
    				//System.out.println("Value being Stored: " + num);
                                    
    
                                    //THIS IS WHERE THE ERROR POINTS ME TO...[/COLOR][/U][/B]
    				percipitationAmount[m][y] = num; 
    				
    			}
    		}
    		
    		infile.close();
    		calculateMonthlyAverage();
    		calculateYearTotal();
    	}
    
    	public void printTable(PrintWriter outfile)
    	{
    		//OUTPUT THE TABLE USING NICELY FORMATTED NUMBERS #.##
    		//AND NEAT COLUMNS FOR TASK #4
    		outfile.close();
    	}
    
    	public static void main(String[] args) throws IOException
    	{
    		Precipitation wetStuff = new Precipitation();
    		
    		File file  = new File("precip.dat");
    
    		Scanner infile = new Scanner(file);
    			
    		PrintWriter outfile = new PrintWriter(new FileWriter("precip.out"));
    
    		wetStuff.readFile(infile);
    		wetStuff.printTable(outfile);
    		
    	}
    }
    Last edited by ecstokely; 02-04-2013 at 06:19 PM. Reason: forgot to format code

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Need help! Cant Assign values to 2 dimensional Array!!!

    Whoa brochacho, that's a lot of code to be looking at with no formatting. Please post your code again (from your editor) in to [code][/code] tags to preserve formatting so we can read it here.

    Also if your code is not compiling it would be rather beneficial to post your compiler error along with your code, giving someone more information than may be needed is considered much more preferable than not enough information in this situation.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

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

    Default Re: Need help! Cant Assign values to 2 dimensional Array!!!

    Please use [code] tags [/code] when posting code so the formatting is retained.
    Please do not ask for code as refusal often offends.

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Need help! Cant Assign values to 2 dimensional Array!!!

    Hi ecstokely

    The folks are right about the formatting.
    It looks like there are a couple of issue with the code but I will let you have a crack at them first ;).
    Your initial problem is caused by the redeclaration of percipitationAmount within the Precipitation() method.

    Regards.

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

    Default Re: Need help! Cant Assign values to 2 dimensional Array!!!

    Ah!
    I can see it now the tags are in place.
    Please do not ask for code as refusal often offends.

  6. #6
    ecstokely is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default Re: Need help! Cant Assign values to 2 dimensional Array!!!

    Sorry about the bad formatting. Its my first post. Thanks for the quick replies...
    I also pulled out the methods that arent yet being used.

    ronin - thanks for the pointer. Ill try playing around with it and see where i can get.

  7. #7
    ecstokely is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default Re: Need help! Cant Assign values to 2 dimensional Array!!!

    Got it!!!

    Thanks everyone!

  8. #8
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Need help! Cant Assign values to 2 dimensional Array!!!

    You're welcome, and welcome to the forums :D.

Similar Threads

  1. How to assign letter values?
    By jsaavedra in forum New To Java
    Replies: 1
    Last Post: 11-14-2011, 05:55 PM
  2. assign a value to two dimensional array
    By makpandian in forum New To Java
    Replies: 4
    Last Post: 10-05-2011, 05:21 AM
  3. to assign values dynamically
    By su5 in forum New To Java
    Replies: 1
    Last Post: 04-28-2011, 08:00 PM
  4. Iterators - can you assign new values to them?
    By DerekRaimann in forum New To Java
    Replies: 2
    Last Post: 12-09-2010, 07:11 PM
  5. Array Assign Values from a Textfile
    By fawadafr in forum Java Applets
    Replies: 6
    Last Post: 11-30-2008, 12:10 AM

Tags for this Thread

Posting Permissions

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