2016-12-21 4 views
0

私はいくつかのプロパティと共にいくつかの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")); 
} 
} 
+2

[NullPointerExceptionとは何ですか?それを修正するにはどうすればいいですか?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix -it) – pvg

+0

@pvg私はNullPointerExceptionが何であるか知っています、なぜ私のコードに起こっているのか分かりません。これはyamlの解析と関係があります。 –

+0

答えはNPEの原因を診断する方法であるため、それはまだまだです。そうすれば、問題はほぼ確実に解決されます。また、あなたが持っている特定の例外や他の人が試すことができるコードを提供していない - [MCVE]を書く方法についての提案を見てください – pvg

答えて

2

手でyamlを解析するのは面倒でエラーが発生する可能性があります。 yamlbeansのようなライブラリを使う方が簡単かもしれません。

http://yamlbeans.sourceforge.net/

package com.jbirdvegas.q41267676; 

import com.esotericsoftware.yamlbeans.YamlReader; 

import java.io.StringReader; 
import java.util.List; 
import java.util.Map; 

public class YamlExample { 
    public static void main(String[] args) throws Exception { 

     String yamlInput = 
       "rates:\n" + 
         "- 391:\n" + 
         " mul: 10000\n" + 
         " store: 5000\n" + 
         "- 392:\n" + 
         " mul: 9000\n" + 
         " store: 5000"; 

     YamlReader reader = new YamlReader(new StringReader(yamlInput)); 
     Map map = (Map) reader.read(); 
     // rates is a list 
     List<Map> rates = (List<Map>) map.get("rates"); 
     // each item in the list is a map 
     for (Map itemMap : rates) { 
      // each map contains a single map the key is [ 391|392 ] 
      itemMap.forEach((key, value) -> { 
       System.out.println("Key: " + key); 
       // the value in in this map is itself a map 
       Map embededMap = (Map) value; 
       // this map contains the values you want 
       System.out.println(embededMap.get("mul")); 
       System.out.println(embededMap.get("store")); 
      }); 
     } 
    } 
} 

この版画:

Key: 391 
10000 
5000 
Key: 392 
9000 
5000 

これは、単純なユースケースですが、それはあなたのニーズに適しであるかどうyamlbeansは、反射クラスモデル人口ようGSONを提供します。

+0

このソリューションを試しましたか?私はまだnullポインタの例外を取得しますが、今回は 'itemMap.get( "mul")行にあります。 –

+0

完全なコードを解析して回答を更新しましたが、最初にフォーマットを誤解しました – JBirdVegas

+0

ありがとうございました!完璧な作品 –

0

YAMLが地図のリストであるという前提は間違っています。最上位レベルは、単一のキーと値のペアを持つマップで、そのキーはratesであり、値は2つの要素を持つシーケンスです。

これらの要素のそれぞれは、キーが数字(391,392)であり、その値がそれぞれ2つのキーと値のペアを持つマッピングである単一のキーの値のペアでマッピングされています。

左手にダッシュ(-)があるということは、トップレベルにあることを意味するものではありません(YAMLファイルにはリストがなく、プログラミング言語で構成されています)。シーケンスが特定のキーの値である場合、それらのシーケンス要素は、YAMLファイルの場合と同じインデントレベルになります。

関連する問題