1

This is Element class:

public class Element {

    private String elementName;
    private int atomicNumber;
    private String Symbol;
    private double atomicWeight;

    public Element()
    {

    }

    public String getElementName()
    {
        return elementName;
    }
    public int getAtomicNumber()
    {
        return atomicNumber;
    }
    public String getSymbol()
    {
        return Symbol;
    }
    public double getAtomicWeight()
    {
        return atomicWeight;
    }
    public void setElementName(String elementName)
    {
        this.elementName = elementName;
    }
    public void setAtomicNumber(int atomicNumber)
    {
        this.atomicNumber = atomicNumber;
    }
    public void setSymbol(String Symbol)
    {
        this.Symbol = Symbol;
    }
    public void setAtomicWeight(double atomicWeight)
    {
        this.atomicWeight = atomicWeight;
    }

}

I am trying to make an Array of this class, this is my mainU.java class:

public class mainU {

    public static void main(String[] args){     

            Element[] element = new Element[103];

        element[0].setElementName("H");
        String s = element[0].getElementName();

        System.out.println(s);

}

The problem is that i'm getting this error:

Exception in thread "main" java.lang.NullPointerException
    at mainU.main(mainU.java:14)

Anyone can tell me the reason i'm getting such an error? I made my program as simple as possible in order for you to be able to help me.

1
  • you should learn how to use a debugger (in case you already don't know) - a debuggers would have helped you solve this many times quicker than writing this question. Commented Jan 10, 2013 at 12:42

8 Answers 8

2

The Problem is in the part:

Element[] element = new Element[103];
element[0].setElementName("H");

Your accessing the first element without creating it before that. you create the array but it doesnt containt automatically new objects of the type used, you have to declare these explicitly, for example:

element[0] = new element();

And then you can use the object.

Sign up to request clarification or add additional context in comments.

Comments

1

Creating new object arrays will yield arrays filled with null. If you need to have objects, you will have to iterate over the array after its creation and populate it with new instances.

1 Comment

Yes, but i am not accessing a NULL object, i am accessing element[0] which I have set for it an element name using the method setElementName...
1

You need to create objects. You just created array of object references.

Element[] element = new Element[103];
for(int i=0; i<103; i++) {
   element[i] = new Element();
}

As your element is object (arrays are object) and it is stored on heap. All Element object references are initialized to null value. Due to which you are getting NPEs.

Element[] element = new Element[103];
element[0].setElementName("H");  // element[0] is null

Comments

1

you have only initialized the Element Array but never initialized its elements,

        Element[] element = new Element[103];
    element[0].setElementName("H");// this cause NPE as element[0] is null

You need to initialize the elements like below:

      Element[] element = new Element[103];
       element[0] = new Element();
      element[0].setElementName("H");

Comments

1

Declaring an array like initialises the array object, but not the elements of the array. You need to create an Element object at each index of the array.

element[0] = new Element();
element[0].setElementName("H");

1 Comment

ahhh!! OK i understand now! i'll give it a try! but should the declaration stay Element[] element = new Element[103] ?
1

When you create an array of objects all it's elements are initialized to null. You simply have to create a new Element object and add it to the array like this:

Element[] elems = new Element[100];
elems[0] = new Element();
elem[0].setElementName("asdf");

Comments

1

You have not initialized element[0] . You need to initialize it first, in fact every array element if you intent to use them.

element[0]=new Element();

or you can initialize all of them using loop

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

Comments

0

Between those two lines

 Element[] element = new Element[103];
 element[0].setElementName("H");

you need to create element[0], that is to build a new instance of Element. In your code element[0] is null, hence the NullPointerException when you call setElementName.

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.