0

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

So it gives null pointer exception at arr[i] = i;. I am new to Java programming so I wanted to try arrays

 public class Multiples
    {
        private int arr[];
        private int sum = 0;

    public void multiples1()
      {
        for(int i= 0 ; i <= 1000; i++)
            {
                arr[i] = i ;
                if((i%3 == 0 && i%5==0))
                    {
                        sum = sum + arr[i];                         
                    }                   
            }   
        System.out.println(sum);
      }


}
jww
  • 83,594
  • 69
  • 338
  • 732
  • You need to declare the size of your array! It's not enough to leave it blank. – Floris Sep 17 '13 at 02:33
  • 2
    @Floris - No, you don't declare the size of arrays in Java. But you do need to create the arrays -- they don't create themselves/ – Hot Licks Sep 17 '13 at 02:35
  • you have to init the array, allocate memory to arr – nachokk Sep 17 '13 at 02:37
  • 1
    just a note, if i%3 == 0 and i%5 == 0 then `i%15 ==0` – nachokk Sep 17 '13 at 02:51
  • @HotLicks - initialize, declare... I was sloppy in my wording. It is late here (and Java is not "my" language). Thanks for pointing out the correct phrase. – Floris Sep 17 '13 at 03:08
  • "multiples of 3 OR 5" - implies that you should use the `||` operator, not the `&&` operator, in your `if` statement! – Floris Sep 17 '13 at 03:10
  • @Floris - I see related mistakes made in Objective-C quite often. In some case people believe they must "declare" a pointer by initializing it with an object (which they immediately discard by overwriting with a new object). Or they believe that simply declaring a pointer is equivalent to initializing it with an object of that type. – Hot Licks Sep 17 '13 at 03:23
  • @HotLicks - I have no argument with you there. The simple lack of making sure that the compiler assigns valid memory locations for the objects you want to manipulate in your code must account for half the bugs in the known universe. – Floris Sep 17 '13 at 03:31

2 Answers2

4
  • It should be private int arr[] = new int[arraySize];
  • An array is an Object so if you don't initialize it using new int[size], it will be null.
  • If you perform operation of a reference variable that is referring to null, you will face NullPointerException

Your revised code would be:

public class Multiples
    {
        private int arr[] = new int[1000];
        private int sum = 0;

    public void multiples1()
      {
        for(int i= 0 ; i < 1000; i++)
            {
                arr[i] = i ;
                if((i%3 == 0 && i%5==0))
                    {
                        sum = sum + arr[i];                         
                    }                   
            }   
        System.out.println(sum);
      }


}
Prasad Kharkar
  • 12,721
  • 3
  • 35
  • 55
1

The following code snippet would do what you want to achieve.

public class Multiples
{
    private int arr[] = new int[1001];
    private int sum = 0;

public void multiples1()
  {
    for(int i= 0 ; i <= 1000; i++)
        {
            arr[i] = i ;
            if((i%3 == 0 && i%5==0))
                {
                    sum = sum + arr[i];                         
                }                   
        }   
    System.out.println(sum);
  }
}
Apurv
  • 4,153
  • 2
  • 15
  • 30