从 MongoDB 中获取列表并将其插入数组列表时出现 NullPointer
NullPointer when grabbing a list from MongoDB and inserting it into an arraylist
所以现在我正在尝试从 MongoDB 中检索一个 table,它目前只包含 1 个值。因此,每当我尝试从 Mongo 中提取数据并将其插入包含 ArrayList 的 ArrayList 或比较器散列图中时,它都会出于某种奇怪的原因输出 空指针 。我不确定为什么当 table 本身不为空时它会抛出 空指针 。
错误
Caused by: java.lang.NullPointerException
at krimsonlib.account.Account.addMultiplier(Account.java:538) ~[?:?]
at krimsonlib.account.AccountManager.MongoTOMap(AccountManager.java:169) ~[?:?]
at krimsonlib.account.AccountManager.makeProfile(AccountManager.java:61) ~[?:?]
at krimsonlib.core.join(core.java:37) ~[?:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_66]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_66]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_66]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_66]
at org.bukkit.plugin.java.JavaPluginLoader.execute(JavaPluginLoader.java:306) ~[spigot-1.8.7-
... 14 more
这是将列表添加到现有列表的比较器方法。
第 538 行
List<String> multipliers;
//comparator
public Account(String id, String uuid, String name, String kingdom, String rank, String recruit, String joinDate, String lastOnline, List<String> multipliers, List<String> friends,int favor, int playtime, int crystals, int coins, int networklevel, int networkexp, List<String> achievements, List<String> pets, List<String> gadgets, List<String> particles, List<String> cloaking, List<String> clothing){
this.id = id;
this.uuid = uuid;
this.name = name;
this.kingdom = kingdom;
this.rank = rank;
this.recruit = recruit;
this.joinDate = joinDate;
this.lastOnline = lastOnline;
this.multipliers = multipliers;
this.friends = friends;
this.favor = favor;
this.playTime = playtime;
this.crystals = crystals;
this.coins = coins;
this.networklevel = networklevel;
this.networkexp = networkexp;
this.achievements = achievements;
this.pets = pets;
this.gadgets = gadgets;
this.particles = particles;
this.cloaking = cloaking;
this.clothing = clothing;
}
public void newAccount(){
UtilMap.account.put(uuid, new Account(getID(), getUUID(), getName(), getKingdom(), getRank(), getRecruit(), getJoinDate(), getLastOnline(), getMultipliers(), getFriends(), getFavor(), getPlayTime(), getCrystals(), getCoins(), getLevel(), getExp(), getAchievements(), getPets(), getGadgets(), getParticles(), getCloaking(), getClothing()));
}
public void addMultiplier(List<String> multipliers) {
this.multipliers.addAll(multipliers); // this is line 538
}
此方法从该特定用户提取所有数据并将其插入比较器
MongoToMap 方法
public static void MongoTOMap(UUID uuid){
String trim = uuid.toString().replace("-", "");
String nontrim = uuid.toString();
Account ac = new Account();
DBObject r = new BasicDBObject("uuid", nontrim);
DBObject found = Mongo.users.findOne(r);
if ( found != null ){
ac.setID((String) found.get("_id"));
ac.setUUID((String) found.get("trimuuid"));
ac.setName((String) found.get("name"));
ac.setKingdom((String) found.get("Kingdom"));
ac.setRecruit((String) found.get("recruit"));
ac.setCrystals((int) found.get("crystals"));
ac.setCoins((int) found.get("coins"));
ac.setRank((String) found.get("rank"));
ac.setFavor((int) found.get("favor"));
ac.setJoinDate((String) found.get("First_Join_Date"));
ac.setLastOnline((String) found.get("Last_Join_Date"));
ac.addPlayTime((int) found.get("Playtime"));
ac.setLevel((int) found.get("Network_Level"));
ac.setExp((int) found.get("Network_Exp"));
ac.addMultiplier((List) found.get("Multipliers"));
ac.addFriend((List) found.get("friends"));
ac.addAchievement((List) found.get("achievements"));
ac.addPet((List) found.get("pets"));
ac.addGadget((List) found.get("gadgets"));
ac.addParticle((List) found.get("particles"));
ac.addCloak((List) found.get("cloaking"));
ac.addClothes((List) found.get("clothing"));
ac.newAccount();
System.out.println("[Mongo]- Converted user `" + UtilMap.account.get(trim).getName() + "` data to Map, Method/MongoTOMap #" + UtilMath.genReceipt());
} else {
//this error should NEVER occur
}
}
您可能需要使用帐户的 "long" 构造函数:
if (found != null) {
Account ac = new Account(
(String) found.get("_id"),
(String) found.get("trimuuid"),
... ,
(List) found.get("Multipliers"),
...
);
ac.newAccount();
}
或更改帐户:
public void addMultiplier(List<String> multipliers) {
if (this.multipliers != null) {
this.multipliers.addAll(multipliers);
} else {
this.multipliers = multipliers;
}
}
您正在声明变量 multipliers
,但您从未对其进行初始化。
在这种情况下,除非您将有效的 List<String>
传递给 Account
构造函数,否则变量具有空值,但您的代码仅使用默认构造函数 (new Account()
),因此该列表保持为空。
如果您的乘数是有效列表,则给定行中出现 NullPointerException
的唯一原因是您将无效(空)列表传递给 addMultiplier
。
要找出异常的原因,您可以调试您的应用程序或添加这样的日志记录:
public void addMultiplier(List<String> multipliers) {
if(this.multipliers == null)
{
System.out.println("this.multipliers is not set correctly");
}
if(multipliers == null)
{
System.out.println("parameter multipliers is not set correctly");
}
this.multipliers.addAll(multipliers);
}
编辑:哦,我太慢了:)
所以现在我正在尝试从 MongoDB 中检索一个 table,它目前只包含 1 个值。因此,每当我尝试从 Mongo 中提取数据并将其插入包含 ArrayList 的 ArrayList 或比较器散列图中时,它都会出于某种奇怪的原因输出 空指针 。我不确定为什么当 table 本身不为空时它会抛出 空指针 。 错误
Caused by: java.lang.NullPointerException
at krimsonlib.account.Account.addMultiplier(Account.java:538) ~[?:?]
at krimsonlib.account.AccountManager.MongoTOMap(AccountManager.java:169) ~[?:?]
at krimsonlib.account.AccountManager.makeProfile(AccountManager.java:61) ~[?:?]
at krimsonlib.core.join(core.java:37) ~[?:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_66]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_66]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_66]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_66]
at org.bukkit.plugin.java.JavaPluginLoader.execute(JavaPluginLoader.java:306) ~[spigot-1.8.7-
... 14 more
这是将列表添加到现有列表的比较器方法。
第 538 行
List<String> multipliers;
//comparator
public Account(String id, String uuid, String name, String kingdom, String rank, String recruit, String joinDate, String lastOnline, List<String> multipliers, List<String> friends,int favor, int playtime, int crystals, int coins, int networklevel, int networkexp, List<String> achievements, List<String> pets, List<String> gadgets, List<String> particles, List<String> cloaking, List<String> clothing){
this.id = id;
this.uuid = uuid;
this.name = name;
this.kingdom = kingdom;
this.rank = rank;
this.recruit = recruit;
this.joinDate = joinDate;
this.lastOnline = lastOnline;
this.multipliers = multipliers;
this.friends = friends;
this.favor = favor;
this.playTime = playtime;
this.crystals = crystals;
this.coins = coins;
this.networklevel = networklevel;
this.networkexp = networkexp;
this.achievements = achievements;
this.pets = pets;
this.gadgets = gadgets;
this.particles = particles;
this.cloaking = cloaking;
this.clothing = clothing;
}
public void newAccount(){
UtilMap.account.put(uuid, new Account(getID(), getUUID(), getName(), getKingdom(), getRank(), getRecruit(), getJoinDate(), getLastOnline(), getMultipliers(), getFriends(), getFavor(), getPlayTime(), getCrystals(), getCoins(), getLevel(), getExp(), getAchievements(), getPets(), getGadgets(), getParticles(), getCloaking(), getClothing()));
}
public void addMultiplier(List<String> multipliers) {
this.multipliers.addAll(multipliers); // this is line 538
}
此方法从该特定用户提取所有数据并将其插入比较器
MongoToMap 方法
public static void MongoTOMap(UUID uuid){
String trim = uuid.toString().replace("-", "");
String nontrim = uuid.toString();
Account ac = new Account();
DBObject r = new BasicDBObject("uuid", nontrim);
DBObject found = Mongo.users.findOne(r);
if ( found != null ){
ac.setID((String) found.get("_id"));
ac.setUUID((String) found.get("trimuuid"));
ac.setName((String) found.get("name"));
ac.setKingdom((String) found.get("Kingdom"));
ac.setRecruit((String) found.get("recruit"));
ac.setCrystals((int) found.get("crystals"));
ac.setCoins((int) found.get("coins"));
ac.setRank((String) found.get("rank"));
ac.setFavor((int) found.get("favor"));
ac.setJoinDate((String) found.get("First_Join_Date"));
ac.setLastOnline((String) found.get("Last_Join_Date"));
ac.addPlayTime((int) found.get("Playtime"));
ac.setLevel((int) found.get("Network_Level"));
ac.setExp((int) found.get("Network_Exp"));
ac.addMultiplier((List) found.get("Multipliers"));
ac.addFriend((List) found.get("friends"));
ac.addAchievement((List) found.get("achievements"));
ac.addPet((List) found.get("pets"));
ac.addGadget((List) found.get("gadgets"));
ac.addParticle((List) found.get("particles"));
ac.addCloak((List) found.get("cloaking"));
ac.addClothes((List) found.get("clothing"));
ac.newAccount();
System.out.println("[Mongo]- Converted user `" + UtilMap.account.get(trim).getName() + "` data to Map, Method/MongoTOMap #" + UtilMath.genReceipt());
} else {
//this error should NEVER occur
}
}
您可能需要使用帐户的 "long" 构造函数:
if (found != null) {
Account ac = new Account(
(String) found.get("_id"),
(String) found.get("trimuuid"),
... ,
(List) found.get("Multipliers"),
...
);
ac.newAccount();
}
或更改帐户:
public void addMultiplier(List<String> multipliers) {
if (this.multipliers != null) {
this.multipliers.addAll(multipliers);
} else {
this.multipliers = multipliers;
}
}
您正在声明变量 multipliers
,但您从未对其进行初始化。
在这种情况下,除非您将有效的 List<String>
传递给 Account
构造函数,否则变量具有空值,但您的代码仅使用默认构造函数 (new Account()
),因此该列表保持为空。
如果您的乘数是有效列表,则给定行中出现 NullPointerException
的唯一原因是您将无效(空)列表传递给 addMultiplier
。
要找出异常的原因,您可以调试您的应用程序或添加这样的日志记录:
public void addMultiplier(List<String> multipliers) {
if(this.multipliers == null)
{
System.out.println("this.multipliers is not set correctly");
}
if(multipliers == null)
{
System.out.println("parameter multipliers is not set correctly");
}
this.multipliers.addAll(multipliers);
}
编辑:哦,我太慢了:)