Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By JosAH

Thread: rainfall programming- exception in main thread

  1. #1
    news4toya is offline Member
    Join Date
    Sep 2014
    Posts
    2
    Rep Power
    0

    Default rainfall programming- exception in main thread

    exception in thread main java.lang.nullpointerexception code 45 and code 116
    the lines are highlighted if can help me solve the problem. and no the code tags did not show up when I uploaded the file. Thanks in advance if anyone can help

    I typed '[code]' below:
    Java Code:
    /**
    This program demostrates rainfall for a year
    */
    
    import java.util.Scanner;     //needed for Scanner class
    
    public class drake_latoya_chap7_1
     {
         Scanner keyboard = new Scanner(System.in);     //create scanner object
    
             private int month = 12;        //declare variable as private 
             private double months[ ];        // declare variable as private
    
             public void RainFall()            //declare variable for rainfall
           {
            months = new double [ 12 ];        // create object to hold months
            }
    
         public void setMonths()         // call months
        {
           // get amount of rain fall from user
            for (int n = 0; n < month; n++) 
         {    [COLOR=#ff0000]
                                                                               System.out.print("Enter rainfall for month " + (n+1) + " : ");[/COLOR] 
                 months[ n ] = keyboard.nextInt();
    
                 if(months[ n ] < 0)    //input validation: Does not except negative numbers for rainfall
                 {
                     System.out.println("Please enter positive value.");
                     n--;
                 }
            }
         }
    /**
    getTotal method
    @return The total amount of rainfall for the year.
    */
         public double getTotal()
         {
            double total = 0;        // initialize variable and set to zero
    
            // call input information and return total
            for (int i = 0; i < month; i++) 
          {
                 total = total + months[ i ];     //calculate total rainfall
            }
            return total;
         }
    /**
    getAverage
    @return The average rainfall for the year
    */
         public double getAverage() 
              {
            return getTotal() / month;    // calculate the average rainfall
             }
    /**
    getLargest
    @return The month with the largest amount of rainfall
    */
         public int getLargest()
        {
            double largest = months[ 0 ];     //initailize and set variable to zero
            int m=0;        // initalize variable
    
        // calculate largest rainfall
            for (int i = 0; i < month; i++) 
               {
                 if (months[ i ] > largest) 
        {
                     m=i;
                      }
                 }
            return m+1; 
         }
    /**
    getSmallest
    @return The month with the least amount of rainfall
    */
          public int getSmallest() 
           {
            double smallest = months[ 0 ];        //initialize variable
            int m=0;            //initialize varibale
    
        //calculate month with least amount of rainfall and return total
            for (int i = 0; i < month; i++) 
        {
                 if (months[ i ] < smallest)
                     m=i;
            }
            return m+1;
         }
         public static void main(String[ ] args)
        {
            [COLOR=#ff0000]drake_latoya_chap7_1 rf = new drake_latoya_chap7_1();[/COLOR]    //create new object
            rf.setMonths();        //pass the object
            
        //display results
            System.out.println("The total rainfall for the year: " + rf.getTotal());
            System.out.println("The average rainfall for the year: " + rf.getAverage());
            System.out.println("The month with most rain: " + rf.getLargest());
            System.out.println("The month with least rain: " + rf.getSmallest());
         }
      }
    I typed '[/code]' above this line
    Last edited by JosAH; 09-10-2014 at 10:34 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    12

    Default Re: rainfall programming- exception in main thread

    Post code in code tags to retain formatting
    Copy and paste the exact error message you get
    Indicate which line error occurs on
    Ask a specific question, get a specific answer

  3. #3
    news4toya is offline Member
    Join Date
    Sep 2014
    Posts
    2
    Rep Power
    0

    Default Re: rainfall programming- exception in main thread

    lines are highlight, as far as using code tags, they did not transfer over when I uploaded the file
    code 45 and code 116 but those are just the lines with the error, that why i highlighted them........

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    12

    Default Re: rainfall programming- exception in main thread

    When posting code place [ code ] before and [ /code ] after (without the spaces).

    Neither of the highlighted lines would throw a NPE.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    12

    Default Re: rainfall programming- exception in main thread

    I just noticed the months array is created inside the RainFall method. Do you call this method before attempting to use the array elsewhere in your program?

  6. #6
    omkaradg is offline Member
    Join Date
    Sep 2014
    Posts
    1
    Rep Power
    0

    Default Re: rainfall programming- exception in main thread

    You have not initialized the array months[] before using it.. give a call to RainFall() method before using setMonths() method.

  7. #7
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default Re: rainfall programming- exception in main thread

    declare the months array outside next to months declaration.no need to keep inside Rainfall()
    Ramya:cool:

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: rainfall programming- exception in main thread

    @OP: reread you original post and note the comments I made just before and after your code ...

    kind regards,

    Jos
    RamyaSivakanth likes this.
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Exception in thread main java.lang.nosuchmethoderror main
    By prikesh_joshi7 in forum Java Applets
    Replies: 3
    Last Post: 01-26-2012, 05:19 PM
  2. Replies: 1
    Last Post: 11-23-2011, 06:52 AM
  3. Replies: 6
    Last Post: 07-16-2009, 04:30 PM
  4. Replies: 1
    Last Post: 07-16-2009, 11:35 AM
  5. Replies: 1
    Last Post: 08-07-2007, 08:10 AM

Posting Permissions

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