Results 1 to 8 of 8
- 02-04-2013, 06:03 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 3
- Rep Power
- 0
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
- 02-04-2013, 06:08 PM #2
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.
- 02-04-2013, 06:12 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 02-04-2013, 06:18 PM #4
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
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.
- 02-04-2013, 06:24 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 02-04-2013, 06:24 PM #6
Member
- Join Date
- Feb 2013
- Posts
- 3
- Rep Power
- 0
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.
- 02-04-2013, 06:26 PM #7
Member
- Join Date
- Feb 2013
- Posts
- 3
- Rep Power
- 0
Re: Need help! Cant Assign values to 2 dimensional Array!!!
Got it!!!
Thanks everyone!
- 02-04-2013, 06:27 PM #8
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
Similar Threads
-
How to assign letter values?
By jsaavedra in forum New To JavaReplies: 1Last Post: 11-14-2011, 05:55 PM -
assign a value to two dimensional array
By makpandian in forum New To JavaReplies: 4Last Post: 10-05-2011, 05:21 AM -
to assign values dynamically
By su5 in forum New To JavaReplies: 1Last Post: 04-28-2011, 08:00 PM -
Iterators - can you assign new values to them?
By DerekRaimann in forum New To JavaReplies: 2Last Post: 12-09-2010, 07:11 PM -
Array Assign Values from a Textfile
By fawadafr in forum Java AppletsReplies: 6Last Post: 11-30-2008, 12:10 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks