Eugene
Eugene

Reputation: 5553

Why the 'Null-pointer exception" is raised?

I'm trying to implement an interface, but I get the 'Null-pointer' exception, but I don't know why and I don't know how to avoid it.

class SpaceComposite.java

import java.util.List;
public class SpaceComposite extends SpaceComponent implements ISpaceComposite {

    private List<ISpaceComponent> _components;

    public SpaceComposite(String _title) {
        super(_title);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void addComponent(ISpaceComponent component) {
        _components.add(component);// HERE IS THE EXCEPTION RAISED

    }
}

which derrives from SpaceComponent class

public abstract class SpaceComponent implements ISpaceComponent{
private String _title;

public SpaceComponent(String _title) {
    super();
    setTitle( _title);
}

interface ISpaceComposite derrives from ISpaceComponent

public interface ISpaceComposite extends ISpaceComponent{
    public void addComponent(ISpaceComponent component);

}


public interface ISpaceComponent {
    // Title getter/setter
        String getTitle();
        void setTitle(String title);

    // Looking for a specific child
        ISpaceComponent findChild(String name);

    // We need to test this brave new world
    void runTtest();
}

class Demo.class tries to create a SpaceComposite

public static ISpaceComposite createSolarSystem() {

    ISpaceComposite solarSystem = new SpaceComposite("The Solar System");

    Star theSol = new Star("Sol");
    solarSystem.addComponent(theSol);
}

This line generates the exception

public void addComponent(ISpaceComponent component) {
    _components.add(component);// HERE IS THE EXCEPTION RAISED

}

but the component 'component' is passed to the function, and i can't look to the List class.

Do you know, what am I doing wrong?

P.S. This code was adopted from C# .NET and in the .NET there is no mistakes..

P.P.S. Guys! You're right! But so many correct answers.... Really confused, which I should to mark

Upvotes: 2

Views: 403

Answers (7)

Kingamajick
Kingamajick

Reputation: 2291

It appears you're not initialising _components so when you try and add to it you get a NullPointerException. I assume this was done elsewhere in the C# .NET version

In SpaceComposite change

private List<ISpaceComponent> _components;

to

private List<ISpaceComponent> _components = new ArrayList<ISpaceComponent>();

You could also initialise it in the constructor if you so wished.

Upvotes: 2

wmorrison365
wmorrison365

Reputation: 6318

The error occurs as you haven't initialised your _components in your SpaceComposite class. So, it remains null and causes the NPE on first invocation.

You should initialise _components in the SpaceComposite constructor (as this is the class in which it's declared).

Upvotes: 1

millimoose
millimoose

Reputation: 39990

You never assign a value to the _components field.

Initialise it with:

private List<ISpaceComponent> _components = new ArrayList<>();

Upvotes: 1

Urs Reupke
Urs Reupke

Reputation: 6931

You need to initialize the field _components, e.g.

private List<ISpaceComponent> _components = new ArrayList<ISpaceComponent>();

Upvotes: 1

Nick Garvey
Nick Garvey

Reputation: 3000

_components is never instantiated. Change

private List<ISpaceComponent> _components;

to

private List<ISpaceComponent> _components = new ArrayList<ISpaceComponent>();

Upvotes: 2

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 341003

You are not initializing _components field, change to:

private List<ISpaceComponent> _components = new ArrayList<ISpaceComponent>();

It must have been initialized somewhere in the C# version.

Upvotes: 2

Savino Sguera
Savino Sguera

Reputation: 3572

Where do you instantiate _components? It seems the list is never created.

I would modify the constructor as follows:

public SpaceComposite(String _title) {
    super(_title);
    _components = new ArrayList<ISpaceComponent>();
}

Upvotes: 4

Related Questions