jackosonでjsonを読み込みましたが、うまくいきません
ヌルポインターが出ます
原因を昨日から追求していますがわかりませんでした
jsonはライブドアの天気apiです
aeraという項目は読み込むことができましたが
telopという項目は配列になっておりますがそこができないのです
public void tenki(String location, String area) throws MalformedURLException, IOException { this.area = area; URL url = new URL("http://weather.livedoor.com/forecast/webservice/json/v1?city=" + location); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.connect(); String json_st = ""; try (BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()))) { String line = ""; while ((line = reader.readLine()) != null) { json_st += line; } System.out.println(json_st); } ObjectMapper ob = new ObjectMapper(); JsonNode json = ob.readTree(json_st); area = json.get("title").textValue(); Weather = json.get("forecasts").get("telop").textValue();//ここでヌルポインタが発生する... System.out.println(area); } }
jsonの例です(大阪) { "pinpointLocations": [ { "link": "http://weather.livedoor.com/area/forecast/2710000", "name": "大阪市" }, { "link": "http://weather.livedoor.com/area/forecast/2714000", "name": "堺市" }, { "link": "http://weather.livedoor.com/area/forecast/2720200", "name": "岸和田市" }, { "link": "http://weather.livedoor.com/area/forecast/2720300", "name": "豊中市" }, { "link": "http://weather.livedoor.com/area/forecast/2720400", "name": "池田市" }, { "link": "http://weather.livedoor.com/area/forecast/2720500", "name": "吹田市" }, { "link": "http://weather.livedoor.com/area/forecast/2720600", "name": "泉大津市" }, { "link": "http://weather.livedoor.com/area/forecast/2720700", "name": "高槻市" }, { "link": "http://weather.livedoor.com/area/forecast/2720800", "name": "貝塚市" }, { "link": "http://weather.livedoor.com/area/forecast/2720900", "name": "守口市" }, { "link": "http://weather.livedoor.com/area/forecast/2721000", "name": "枚方市" }, { "link": "http://weather.livedoor.com/area/forecast/2721100", "name": "茨木市" }, { "link": "http://weather.livedoor.com/area/forecast/2721200", "name": "八尾市" }, { "link": "http://weather.livedoor.com/area/forecast/2721300", "name": "泉佐野市" }, { "link": "http://weather.livedoor.com/area/forecast/2721400", "name": "富田林市" }, { "link": "http://weather.livedoor.com/area/forecast/2721500", "name": "寝屋川市" }, { "link": "http://weather.livedoor.com/area/forecast/2721600", "name": "河内長野市" }, { "link": "http://weather.livedoor.com/area/forecast/2721700", "name": "松原市" }, { "link": "http://weather.livedoor.com/area/forecast/2721800", "name": "大東市" }, { "link": "http://weather.livedoor.com/area/forecast/2721900", "name": "和泉市" }, { "link": "http://weather.livedoor.com/area/forecast/2722000", "name": "箕面市" }, { "link": "http://weather.livedoor.com/area/forecast/2722100", "name": "柏原市" }, { "link": "http://weather.livedoor.com/area/forecast/2722200", "name": "羽曳野市" }, { "link": "http://weather.livedoor.com/area/forecast/2722300", "name": "門真市" }, { "link": "http://weather.livedoor.com/area/forecast/2722400", "name": "摂津市" }, { "link": "http://weather.livedoor.com/area/forecast/2722500", "name": "高石市" }, { "link": "http://weather.livedoor.com/area/forecast/2722600", "name": "藤井寺市" }, { "link": "http://weather.livedoor.com/area/forecast/2722700", "name": "東大阪市" }, { "link": "http://weather.livedoor.com/area/forecast/2722800", "name": "泉南市" }, { "link": "http://weather.livedoor.com/area/forecast/2722900", "name": "四條畷市" }, { "link": "http://weather.livedoor.com/area/forecast/2723000", "name": "交野市" }, { "link": "http://weather.livedoor.com/area/forecast/2723100", "name": "大阪狭山市" }, { "link": "http://weather.livedoor.com/area/forecast/2723200", "name": "阪南市" }, { "link": "http://weather.livedoor.com/area/forecast/2730100", "name": "島本町" }, { "link": "http://weather.livedoor.com/area/forecast/2732100", "name": "豊能町" }, { "link": "http://weather.livedoor.com/area/forecast/2732200", "name": "能勢町" }, { "link": "http://weather.livedoor.com/area/forecast/2734100", "name": "忠岡町" }, { "link": "http://weather.livedoor.com/area/forecast/2736100", "name": "熊取町" }, { "link": "http://weather.livedoor.com/area/forecast/2736200", "name": "田尻町" }, { "link": "http://weather.livedoor.com/area/forecast/2736600", "name": "岬町" }, { "link": "http://weather.livedoor.com/area/forecast/2738100", "name": "太子町" }, { "link": "http://weather.livedoor.com/area/forecast/2738200", "name": "河南町" }, { "link": "http://weather.livedoor.com/area/forecast/2738300", "name": "千早赤阪村" } ], "link": "http://weather.livedoor.com/area/forecast/270000", "forecasts": [ { "dateLabel": "今日", "telop": "雨", "date": "2017-10-29", "temperature": { "min": null, "max": { "celsius": "18", "fahrenheit": "64.4" } }, "image": { "width": 50, "url": "http://weather.livedoor.com/img/icon/15.gif", "title": "雨", "height": 31 } }, { "dateLabel": "明日", "telop": "曇のち晴", "date": "2017-10-30", "temperature": { "min": { "celsius": "12", "fahrenheit": "53.6" }, "max": { "celsius": "15", "fahrenheit": "59.0" } }, "image": { "width": 50, "url": "http://weather.livedoor.com/img/icon/12.gif", "title": "曇のち晴", "height": 31 } }, { "dateLabel": "明後日", "telop": "晴れ", "date": "2017-10-31", "temperature": { "min": null, "max": null }, "image": { "width": 50, "url": "http://weather.livedoor.com/img/icon/1.gif", "title": "晴れ", "height": 31 } } ], "location": { "city": "大阪", "area": "近畿", "prefecture": "大阪府" }, "publicTime": "2017-10-29T11:00:00+0900", "copyright": { "provider": [ { "link": "http://tenki.jp/", "name": "日本気象協会" } ], "link": "http://weather.livedoor.com/", "title": "(C) LINE Corporation", "image": { "width": 118, "link": "http://weather.livedoor.com/", "url": "http://weather.livedoor.com/img/cmn/livedoor.gif", "title": "livedoor 天気情報", "height": 26 } }, "title": "大阪府 大阪 の天気", "description": { "text": " 近畿地方は、前線や台風第22号の影響で雨が降っています。\n\n 今日の近畿地方は、前線や台風第22号の影響で、雷を伴い非常に激しい\n雨が降り、南部では大荒れの天気となるでしょう。\n\n 明日の近畿地方は、寒気や湿った空気の影響で、おおむね曇りとなり、北\n部では雨の降る所がある見込みです。", "publicTime": "2017-10-29T10:38:00+0900" } }
エラー内容は
Exception in thread "main" java.lang.NullPointerException
at gotenki.tenki(gotenki.java:58)
at main.tenki(main.java:181)
at main.main(main.java:133)
mainの方は呼び出しメソッドで引数に間違いはありません
ぜひどうすればいいのかご教授ください
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/29 03:50
2017/10/29 03:51
2017/10/29 04:11 編集
2017/10/29 04:35
2017/10/29 06:05