[英]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. 我正在尝试实现一个接口,但我得到'Null-pointer'异常,但我不知道为什么,我不知道如何避免它。
class SpaceComposite.java 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 它来自SpaceComponent类
public abstract class SpaceComponent implements ISpaceComponent{
private String _title;
public SpaceComponent(String _title) {
super();
setTitle( _title);
}
interface ISpaceComposite derrives from ISpaceComponent 接口ISpaceComposite来自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 class Demo.class尝试创建一个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. 但组件'component'被传递给函数,我无法查看List类。
Do you know, what am I doing wrong? 你知道吗,我做错了什么?
PS This code was adopted from C# .NET and in the .NET there is no mistakes.. PS此代码是从C#.NET采用的,在.NET中没有错误。
PPS Guys! PPS伙计们! You're right!
你是对的! But so many correct answers.... Really confused, which I should to mark
但是这么多正确的答案......真的很困惑,我应该标记
Where do you instantiate _components? 你在哪里实例化_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>();
}
You are not initializing _components
field, change to: 您没有初始化
_components
字段,请更改为:
private List<ISpaceComponent> _components = new ArrayList<ISpaceComponent>();
It must have been initialized somewhere in the C# version. 它必须已在C#版本的某处初始化。
_components is never instantiated. _components永远不会被实例化。 Change
更改
private List<ISpaceComponent> _components;
to 至
private List<ISpaceComponent> _components = new ArrayList<ISpaceComponent>();
It appears you're not initialising _components
so when you try and add to it you get a NullPointerException
. 看来你没有初始化
_components
所以当你尝试添加它时会得到一个NullPointerException
。 I assume this was done elsewhere in the C# .NET version 我假设这是在C#.NET版本的其他地方完成的
In SpaceComposite change 在SpaceComposite中更改
private List<ISpaceComponent> _components;
to 至
private List<ISpaceComponent> _components = new ArrayList<ISpaceComponent>();
You could also initialise it in the constructor if you so wished. 如果您愿意,也可以在构造函数中初始化它。
您需要初始化字段_components
,例如
private List<ISpaceComponent> _components = new ArrayList<ISpaceComponent>();
You never assign a value to the _components
field. 您永远不会为
_components
字段赋值。
Initialise it with: 用以下内容初始化:
private List<ISpaceComponent> _components = new ArrayList<>();
The error occurs as you haven't initialised your _components
in your SpaceComposite class. 由于
_components
在SpaceComposite类中初始化_components
,因此会发生错误。 So, it remains null and causes the NPE on first invocation. 因此,它保持为null并在第一次调用时导致NPE。
You should initialise _components
in the SpaceComposite constructor (as this is the class in which it's declared). 您应该在SpaceComposite构造函数中初始化
_components
(因为这是它声明的类)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.