无法在数组或列表中使用对象

发布于 2024-12-17 19:45:46 字数 2044 浏览 0 评论 0原文

我创建了一个简单地扩展 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)

I have created a class that simply extends java.lang.Object.

I can use this object on its own to create any number of instances of it, but I cannot seem to use it inside an array or list.

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));

It always comes up with a 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

芯好空 2024-12-24 19:45:47

这是因为它们是null
使用:

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

This is because they are null.
Use:

public static List<Area> areaList = new ArrayList<Area>();
public static Area[] areas = new Area[10];
千柳 2024-12-24 19:45:46

你实例化了你的列表吗?

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

Did you instantiated your list?

public static List<Area> areaList = new ArrayList<Area>();
areaList.add(new Area(340, 64, -189, 20, wl));
我家小可爱 2024-12-24 19:45:46

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

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

否则,当您尝试使用该数组时,您将收到 NullPointerException

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

You need to instantiate the array before you use it.

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

Otherwise, when you try to use the array, you'll get a NullPointerException.

The same is true for any object, including List.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文