0

I have made five classes (as shown below). When I run my code it gives me "NullPointerException"

First class

public class PasPosition {

      boolean lessThanThirty;
      boolean ThirtyToFifty;
      boolean FiftyToHundred;

     public PasPosition(boolean value1, boolean value2, boolean value3)
     {
        lessThanThirty = value1;
        ThirtyToFifty = value2;
        FiftyToHundred = value3;

    }

}

Second Class

  public class CssPosition {

    boolean lessThanThirty;
    boolean ThirtyToFifty;
    boolean FiftyToHundred;
    public CssPosition(boolean value1,boolean value2, boolean value3)
    {
        lessThanThirty = value1;
        ThirtyToFifty = value2;
        FiftyToHundred = value3;
   }
 }

Third class

  public class SiteData {

     PasPosition pas;
     CssPosition css;


  }

last class

  public class Test {

    SiteData[] sitedata = new SiteData[2];

    public void test()
    {

        for(int i=0;i<sitedata.length;i++)
        {
            System.out.println(sitedata[i].css.FiftyToHundred);
            System.out.println(sitedata[i].css.ThirtyToFifty);
            System.out.println(sitedata[i].css.lessThanThirty);
            System.out.println();
        }
    }

  }
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Bakhtawar
  • 19
  • 5

1 Answers1

-1

It seems that you didn't init the objects.

You must know that in java, an object in a class will not be automatically inited. You must use new operator.

public class SiteData {
    PasPosition pas=new PasPosition(false,false,false); 
    CssPosition css=new CssPosition(false,false,false);//use your own initial value
}
ch271828n
  • 15,854
  • 5
  • 53
  • 88
  • Its is still giving me the same error. Following is the error : Exception in thread "main" java.lang.NullPointerException at javaapplication16.Test.test(Test.java:21) at javaapplication16.Main.main(Main.java:25) Java Result: 1 – Bakhtawar Mar 13 '15 at 04:30
  • @Bakhtawar Please give the full Main.javaor I will not know where line 25 is. – ch271828n Mar 13 '15 at 05:12
  • I figured out the problem...Thanks though...after reading one of the comments above I realized I have declared the array but not initialized it. – Bakhtawar Mar 14 '15 at 06:53