1

I used to write my program codes in C and now moving to Java trying to define a simple struct and create and array of it in the code below, however the run-time exception occurs Exception in thread "main" java.lang.NullPointerException at CPoint.main(CPoint.java:19) I know i have to allocate memory to my array somewhere, but do not know where. Any help will be appreciated:

  public class CPoint {
public int x;
public int y;

public CPoint(int size){

    System.out.println("Constructor1 is called");
}
    public CPoint(){
    System.out.println("Constructor2 is called");
}


public static void main(String[] args){

    CPoint [] p = new CPoint [3];

    p[0].x=90;p[0].y=80;        // this is line 19

    System.out.println(p[0].x);
}

}

PS. I would like to allocate memory somewhere in the class CPoint , not in main, if possible I would like to keep the main() code as simple as possible

0

4 Answers 4

2

You need to allocate each of the objects in the array:

public static void main(String[] args){

    CPoint [] p = new CPoint [3];

    p[0] = new CPoint();
    p[0].x=90;p[0].y=80;

    System.out.println(p[0].x);
}

Edit: You can wrap array initialisation into a static method in the class - essentially an array factory method:

public class CPoint {
    public int x;
    public int y;

    public CPoint() { System.out.println("Inside constructor 1"); }

    public static CPoint[] CPointSet(int size) {
        CPoint[] p= new CPoint[size];
        for(int i=0; i<size; i++)
            p[i] = new CPoint();
        return p;
    }

    public static void main(String[] args) {
        CPoint[] p = CPoint.CPointSet(3);
        p[0].x = 90;
        p[0].y = 80;
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

I would like to allocate memory somewhere in the class CPoint , not in main, if possible I would like to keep the main() code as simple as possible
@Cgraphics, That's not possible. But you can try inlining the array initialization.
@Cgraphics what you can do is create a class wrapper around the array - although this is not really a good java practice. You would create one class passing the number of elements into it - and then in the constructor you would allocate the array and then allocate each element in a loop. Again, this is not really a good practice in java.
also to @Moonbeam: Would you please suggest me a better way which would be a better practice. Also, is there anyway to allocate memory for the array p inside the constructor 1? Thanks
@Cgraphics see my edits: you can create a static method in the class that would take as a parameter the size of array, init the array and then init each element of the array - this way you can have one call from your main method and the get a fully initialised set.
|
1

Array p contains 3 null entries. Initializing an array will not create the objects therein for you. For more information, see Arrays.

Comments

0

You need to create your objects before your use them.

CPoint [] p = new CPoint [3];
p[0] = new CPoint();

Comments

0

Agree to Moonbeam, new CPoint[3] will create an Array object, which in fact an array of references (that's different to C), and all three elements refer to 'null' initially. No memory (for CPoint struct) is allocated util you new some CPoint objects.

You can initialize arrays in this concise way:

CPoint[] p = { new CPoint(), new CPoint(), new CPoint() }

p[0].x=90; p[0].y=80;

System.out.println(p[0].x);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.