现在我正试图从 MongoDB 中检索一个表,目前只包含1个值。因此,每当我尝试从Mongo中提取数据并将其插入包含ArrayList的ArrayList或比较器hashmap时,它会出于某种奇怪的原因输出空指针。我还不确定为什么当表格自身为空时,它会抛出空指针 。 的错误
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$1.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
}
}
答案 0 :(得分:0)
您可能需要使用Account的“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;
}
}
答案 1 :(得分:0)
您正在声明变量multipliers
,但您从未对其进行初始化。
在这种情况下,除了将有效的List<String>
传递给Account
构造函数之外,该变量的值为null,但您的代码仅使用默认构造函数(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);
}
编辑:哦,我太慢了。)