Beef
Beef

Reputation: 1403

Data from my class is null?

I have a class holding a boolean, and two doubles, and then an array of that class, I need the boolean and doubles to have defaults values of false, 0.0, and 0.0, and then I have function that refers to an element of the array and the moment I try to access an one of the variables from the class it throws an exception saying its null. Here is my class and my function calling it.

    public class PanelData {
        boolean flag = false;
        double tempStart = 0.0;
        double tempEnd = 0.0;
    }

    private PanelData[] panelInfo = new PanelData[115];

private void panelInfoHandler (int i, double timeStart, double timeEnd) throws SQLException
    {
        if (!panelInfo[i].flag) {
            delete();
            insert();
            panelInfo[i].flag = true;
            panelInfo[i].tempStart = timeStart;
            panelInfo[i].tempEnd = timeEnd;
        }
        else if (panelInfo[i].tempStart <= timeStart && panelInfo[i].tempEnd >= timeEnd) {

        }
        else
        {
            insert();
            panelInfo[i].tempStart = timeStart;
            panelInfo[i].tempEnd = timeEnd;
        }
    }

here is how I call the class.

panelInfoHandler(9, parsedStart, parsedEnd);

Upvotes: 2

Views: 123

Answers (5)

FloppyDisk
FloppyDisk

Reputation: 1703

  public class PanelData {
        boolean flag = false;
        double tempStart;
        double tempEnd;

        public PanelData() {
            flag = false;
            tempStart = 0.0;
            tempEnd = 0.0;    
    }
    private PanelData[] panelInfo = new PanelData[115]; 

    for(int i = 0; i < 115; i++)
       panelInfo[i] = new PanelData();     

Creating the default constructor lets you instantiate the variables with the default values (false, 0.0, 0.0) in this case so you can test if you are getting a vanilla object back or not.

Upvotes: 0

Hugh Jones
Hugh Jones

Reputation: 2694

You need to do something like

for(int i=0;i<115; i++)
{
   PanelInfo[i] = new PanelData();
}

(Or whatever is the correct Java Syntax)

Upvotes: 1

NPE
NPE

Reputation: 500903

new PanelData[115] creates an array of 115 null references. Have you populated panelInfo with references to actual objects?

At a minimum, you then need to loop through that array and create new instances of PanelData for each element in the array, e.g.

for (int i = 0; i < panelInfo.length; i++)
  panelInfo[i] = new PanelData();

Upvotes: 6

Pedro
Pedro

Reputation: 4160

add this line and then assign the values:

 if(panelInfo[i] == null) panelInfo[i] = new PanelInfo(); 

Upvotes: 1

stevevls
stevevls

Reputation: 10853

Your array is full of null elements until you initialize it. To clarify, if you create an array of primitive objects, you get an array of default (i.e. 0) values. However, an array of Objects gets created with null elements.

int[] myIntArray = new int[10];  // 10 default values of 0
Integer[] myIntegerArray = new Integer[10];  // 10 null elements

Upvotes: 2

Related Questions