隨着社會管理我的幫助的陣列中的一員問題得到這個問題很好解決:How to convert String to the name of the Array?與調用字符串
但現在我得到「NullPointerException異常」。下面是我的代碼使用方法:
public class IroncladsAdder
{
public static String weaponId = null;
public static String ship = null;
public static String wing = null;
//map code
private static Map<String, List<Integer>> arrays = new HashMap<String, List<Integer>>();
public void Holder(String... names) {
for (String name : names) {
arrays.put(name, new ArrayList<Integer>());
}
}
//adds weapons to fleets and stations
public static void AddWeapons(CargoAPI cargo, String fac, int count, int type) {
String arrayName = null;
int quantity = (int) (Math.random()*5f + count/2) + 1;
if (count == 1) {quantity = 1;}
if (type == 0) {arrayName = fac+"_mil_weps";}
else if (type == 1) {arrayName = fac+"_civ_weps";}
else {arrayName = fac+"_tech_weps";}
List<Integer> array = arrays.get(arrayName);
for (int j = 0; j <= count; j++)
{
weaponId = valueOf(arrays.get(arrayName).get((int) (Math.random() * arrays.get(arrayName).size())));
cargo.addWeapons(weaponId, quantity);
}
}
這裏是數組的一個例子:
//high-tech UIN weapons
private static String [] uin_tech_weps =
{
"med-en-uin-partpulse",
"lrg-en-uin-partacc",
"med-bal-uin-driver",
"lrg-bal-uin-terminator",
"lrg-bal-uin-hvydriver",
"lrg-bal-uin-shotgundriver",
"lrg-en-uin-empbeam",
};
錯誤指示,什麼是錯用這種結構:
weaponId = valueOf(arrays.get(arrayName).get((int) (Math.random() * arrays.get(arrayName).size())));
注:i` m使用Intellij IDEA和Java 6.大多數情況下,應用程序對某些錯誤提供建議/修復,在這種情況下顯示一切正常。
我需要的是從特定數組中獲取字符串(即使用代碼生成的名稱)並將其分配給'weaponId'。
你的例外在哪裏? –
看起來'array.get(arrayName)'爲空。通過明確地檢查前面的行來驗證/反駁這一點。 Holder構造函數的調用者通過了什麼?你列出你的'uin_tech_weps',但不是那個和'arrays'之間的連接。 – CPerkins
異常是'java.lang.NullPointerException',它指向字符串64,這正是'weaponId = ...'部分。它沒有提供任何其他細節。 – Okim