不能在数组或列表中使用对象

时间:2011-11-23 08:11:04

标签: java arrays list

我创建了一个只扩展java.lang.Object的类。

我可以单独使用这个对象来创建它的任意数量的实例,但我似乎无法在数组或列表中使用它。

public static Area[] areas = null;
areas[0] = new Area(340, 64, -189, 20, wl);

// Note that <Area> was something my compiler warned me to do, after I tried without it
public static List<Area> areaList;
areaList.add(0, new Area(340, 64, -189, 20, wl));

始终会出现NullPointerException

07:58:11 [SEVERE] Error occurred while enabling EasyProtect v1.0 (Is it up to date?): null
java.lang.NullPointerException
    at main.EasyProtect.onEnable(EasyProtect.java:41)
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:126)
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:920)
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:278)
    at net.madjawa.pluginreloader.PluginReloader.loadPlugin(PluginReloader.java:232)
    at net.madjawa.pluginreloader.PluginReloader.onCommand(PluginReloader.java:85)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:39)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:163)
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:353)
    at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:756)
    at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:721)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:714)
    at net.minecraft.server.Packet3Chat.a(Packet3Chat.java:33)
    at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:92)
    at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
    at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:464)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:374)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:417)

3 个答案:

答案 0 :(得分:2)

您是否实例化了您的清单?

public static List<Area> areaList = new ArrayList<Area>();
areaList.add(new Area(340, 64, -189, 20, wl));

答案 1 :(得分:1)

您需要在使用之前实例化数组。

public static Area[] areas = new Area[100];
areas[0] = new Area(340, 64, -189, 20, wl);

否则,当您尝试使用该数组时,您将获得NullPointerException

任何对象都是如此,包括List

答案 2 :(得分:0)

这是因为它们是null。 使用:

public static List<Area> areaList = new ArrayList<Area>();
public static Area[] areas = new Area[10];