Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

This question already has an answer here:

I am a beginner in object oriented programming what i wanted to do here is create a array of test and set length and breadth in each objects of array "a[]" and print the area. I tried with only one object but the error pop out as null pointer. The setLength() and setBreadth() method is not working or what.

package test;

public class test {

    private double length;
    private double breadth;
    private double area;

    public test()
    {
        this.length=0;
        this.breadth=0;
    }

    public test(double length, double breadth)
    {
        this.length=length;
        this.breadth=breadth;
    }

    public void setLength(double length)
    {
        System.out.println("length setted");
        this.length=length;
    }

    public void setBreadth (double breadth)
    {
        this.breadth=breadth;
        System.out.println("breadth setted");
    }


    public double getArea()
    {
        return 2*(this.length+this.breadth);
    }
}


package test;
import java.util.*;
public class hellop {

    public static void main(String[] args) {
        // TODO Auto-generated method stub


    test a[]= new test[5];

    a[1].setLength(5.9);

    a[1].setBreadth(9.5);
    System.out.println(a[1].getArea());

    }
}
share|improve this question

marked as duplicate by Jan Dvorak, Cerbrus, Tunaki java 16 hours ago

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 8 down vote accepted

Change your code to this:

a[1] = new test ();  

Which can also be used as:

test a[]= new test[]{new test(),new test(),new test(),new test(),new test()};

Or using a for loop:

for (int i=0; i<5; i++){
    a[i] = new test();
}

And then use get and set methods.

You got NullPointerException because the array is full of nulls as default

** You also need to change the class name to have first capital letter like - Test and not test

share|improve this answer
1  
Because arrays are initialized with the default value in every cells (0 for numeric, null for any object). – AxelH 18 hours ago

You are creating an array of test objects but not actually filling it with any instances:

// Creates a array of test 5 objects, but each is null!
test a[]= new test[5];

At this point, the following are true:

a[0] == null
a[1] == null
a[2] == null
// etc... 

You need to fill it before using any elements:

for (int i = 0; i < a.length; i++) {
    a[i] = new test();
}

As an aside, classes in Java normally start with an upper-case letter: class Test; not class test.

share|improve this answer
    
thank you i got it now. – Prashant Pokhrel 18 hours ago
    
for (int i = 0; i < a.length; i++) { a[i] = new test(); } a.length will be 5 and there will be a[0], a[1], a[2], a[3] and a[4] because the loop will be executed 5 times am i correct?? – Prashant Pokhrel 18 hours ago
    
Correct. You could add a breakpoint to the inside of the loop to verify this. – sdgfsdh 18 hours ago

This line test a[]= new test[5]; is creating array of objects which contains 5 null values. That's why it's showing NullPointerException.

There are many good answers here and apart from them, one more thing I would like to add is that you can take advantage of your parametrized constructor

test a[]= new test[5];
a[1] = new test(5.9, 9.5);
System.out.println(a[1].getArea());
share|improve this answer

You created the array of your "test" objects, but the elements in the array are not initialized.

You can find the answer here: How to initialize an array of objects in Java

share|improve this answer

You just created an array of test types but each array is empty and does not reference an instance of test. So you need to change to

for (int i = 0; i < a.length; i++) {
    a[i] = new test();
}

Incidentally, the first letter of your class name needs to be capitalized

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.