私はいくつかのプロパティと共にいくつかのMinecraftアイテムのデータを保存しているプラグインを作成しようとしています。Java get config.ymlからマップマップのリスト
これは私のYAMLファイルの内容です:
rates:
- 391:
mul: 10000
store: 5000
- 392:
mul: 9000
store: 5000
は、だから、基本的にマップのマップのリストです(私はそう少なくとも思います)。 この私が「391」のキー「MUL」にアクセスしようとしている私のJavaコードです:提案の答えを1として
List<Map<?,?>> rates;
rates= getConfig().getMapList("rates");
for(Map<?,?> mp : rates){
Map <?,?> test = (Map<?,?>) mp.get("" + item);
player.sendMessage(test.toString());// HERE I get null pointer exception, and the following lines if this line wasn't there in the first place
player.sendMessage("Mul is: " + test.get("mul"));
player.sendMessage("Store is: " + test.get("store"));
}
を、ここで私はまだNullPointerExceptionが取得私のテストコードは、次のとおりです。
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Map;
import net.sourceforge.yamlbeans.YamlException;
import net.sourceforge.yamlbeans.YamlReader;
public class Test {
public static void main(String[] args) throws FileNotFoundException, YamlException{
YamlReader reader = new YamlReader(new FileReader("config.yml"));
Map map = (Map) reader.read();
Map itemMap = (Map) map.get("391");
System.out.println(itemMap.get("mul"));//This is where I get the exception now
System.out.println(itemMap.get("store"));
}
}
[NullPointerExceptionとは何ですか?それを修正するにはどうすればいいですか?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix -it) – pvg
@pvg私はNullPointerExceptionが何であるか知っています、なぜ私のコードに起こっているのか分かりません。これはyamlの解析と関係があります。 –
答えはNPEの原因を診断する方法であるため、それはまだまだです。そうすれば、問題はほぼ確実に解決されます。また、あなたが持っている特定の例外や他の人が試すことができるコードを提供していない - [MCVE]を書く方法についての提案を見てください – pvg