简体   繁体   English

会话setAttribute抛出空指针

[英]Session setAttribute throwing null pointer

I'm writing a Java code where in if I enter a country name, the session should set an attribute to corresponding country code, else it should not save the session. 我正在编写Java代码,如果要输入国家/地区名称,则会话应将属性设置为相应的国家/地区代码,否则不应保存会话。

Below is my code 下面是我的代码

Temp Class 临时班

package onlyJava;

import java.util.Locale;
import java.util.Scanner;

import com.amazon.speech.speechlet.Session;

public class Temp {

    String cityName;

    public void getTheCurrentWeather(Session session) {
        System.out.println("Enter a city Name\n");
        Scanner sc = new Scanner(System.in);
        cityName = sc.next();
        getCountryCode(cityName, session);
        System.out.println(session.getAttribute("cCode").toString());
    }

    public String getCountryCode(String countryName, Session session) {
        System.out.println(countryName + "\t" + session);
        String result = "";
        String[] locales = Locale.getISOCountries();

        for (String countryCode : locales) {

            Locale obj = new Locale("", countryCode);
            if (countryName.equals(obj.getDisplayCountry().toString())) {
                result = obj.getCountry();
                session.setAttribute("cCode", result);
            }
        }

        return result;
    }

}

Test Class (Main) 测试班 (主要)

package onlyJava;

import com.amazon.speech.speechlet.Session;

public class Test {

    public static void main(String[] args) {

        Temp temp = new Temp();
        temp.getTheCurrentWeather(null);

    }

}

When I run this code with input as India Country , I get the below result. 当我运行此代码并将输入作为India Country时 ,得到以下结果。

India   null
Exception in thread "main" java.lang.NullPointerException
    at onlyJava.Temp.getCountryCode(Temp.java:30)
    at onlyJava.Temp.getTheCurrentWeather(Temp.java:16)
    at onlyJava.Test.main(Test.java:10)

When I enter it as Hyderabad City , I get the below result. 当我将其输入为海得拉巴 市时 ,得到以下结果。

Hyderabad   null
Exception in thread "main" java.lang.NullPointerException
    at onlyJava.Temp.getTheCurrentWeather(Temp.java:17)
    at onlyJava.Test.main(Test.java:10)

Please let me know where am I going wrong and how can I fix this. 请让我知道我要去哪里错了,我该如何解决。

Thanks 谢谢

In both cases the NPE is thrown due to the fact that you are giving a null as a parameter in the call to the method getWeather from the main. 在这两种情况下,都将抛出NPE,这是因为您在从主方法调用getWeather方法时给了空值作为参数。

The main class is expecting to receive a non null Session instance, and it is never checked wether it is null or not. 主类期望接收一个非null的Session实例,无论它是否为null,都不会对其进行检查。

In the India test case, the NullPointerException is thrown from the getCountry () method, as India is in fact a valid country, so this method tries to write down it's corresponding country code in the session object. India测试用例中,由于印度实际上是一个有效的国家,因此从getCountry()方法引发NullPointerException,因此该方法尝试在会话对象中写下其对应的国家/地区代码。 It just in this moment when it falls in the NPE. 就在这时,它落入NPE中。

In the second case, as Hyderabad is not a country, nothing is written in the Session, and the NPE is thrown while it tries to recover the session attribute from a null Session instance. 在第二种情况下,由于海得拉巴不是一个国家,因此会话中不会写入任何内容,并且NPE在尝试从空会话实例中恢复会话属性时会引发NPE。

Both errors would be fixed if you pass a valid Session instance in the main method of the test class 如果您在测试类的main方法中传递了有效的Session实例,则两种错误都将得到修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM