Forum Stats

  • 3,854,962 Users
  • 2,264,441 Discussions
  • 7,905,847 Comments

Discussions

New to Java: Trying to create 2 dimensional string array but getting NullPointer

user580947
user580947 Member Posts: 2
edited Dec 9, 2019 12:21AM in Java Programming

Hi,

I'm a rookie at Java so please be kind.

I'm trying to create a 2 dimensional array where I'm putt in country codes in column 0 and country names in column 1 using the ISO-3166.

Below is my method but i keep getting a Null Pointer Exception and I can' figure out why.  I believe my issue is in the way I'm putting my values in the array but I'm not sure what I'm doing wrong.

public class BrowseISDO3166Test {

     public static void main(String[] args) {

          BrowseISDO3166Test object = new BrowseISDO3166Test();

          object.init();

     }

     String[][] countries;

     public void init() {

          String[] countryCodes = Locale.getISOCountries();

          int x = 0;

          for (String countryCode : countryCodes) {

               Locale obj = new Locale("", countryCode);

               String code = obj.getCountry();

               String name = obj.getDisplayCountry();

               countries[x][0] = code;

               countries[x][1] = name;

               x++;

           }

     }

}

Can someone explain to me what I'm doing wrong...

Thanks in Advance

JavaRookie

user580947

Best Answer

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Dec 8, 2019 1:45PM Answer ✓
    user580947 wrote:public class BrowseISDO3166Test { public static void main(String[] args) { BrowseISDO3166Test object = new BrowseISDO3166Test(); object.init(); } String[][] countries; //***1 public void init() { String[] countryCodes = Locale.getISOCountries(); int x = 0; for (String countryCode : countryCodes) { Locale obj = new Locale("", countryCode); String code = obj.getCountry(); String name = obj.getDisplayCountry(); countries[x][0] = code; //***2 countries[x][1] = name; x++; } }}

    The countries member variable is not initialized before the assignment (***2) of values. The declaration of the variable does not have an initialization, so the variable is null at ***1. Trying to access an index of the array for set/get will result in NPE just like any other java.lang.Object.

    You may do something like ... to overcome the NPE,

       String[][] countries;   public void init()
       {
          String[] countryCodes = Locale.getISOCountries();      countries = new String[countryCodes.length][2];
          int x = 0;
          for (String countryCode : countryCodes)
          {
             Locale obj = new Locale("", countryCode);
             String code = obj.getCountry();
             String name = obj.getDisplayCountry();
             countries[x][0] = code;
             countries[x][1] = name;
             x++;
          }
       }
    user580947

Answers

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Dec 8, 2019 1:45PM Answer ✓
    user580947 wrote:public class BrowseISDO3166Test { public static void main(String[] args) { BrowseISDO3166Test object = new BrowseISDO3166Test(); object.init(); } String[][] countries; //***1 public void init() { String[] countryCodes = Locale.getISOCountries(); int x = 0; for (String countryCode : countryCodes) { Locale obj = new Locale("", countryCode); String code = obj.getCountry(); String name = obj.getDisplayCountry(); countries[x][0] = code; //***2 countries[x][1] = name; x++; } }}

    The countries member variable is not initialized before the assignment (***2) of values. The declaration of the variable does not have an initialization, so the variable is null at ***1. Trying to access an index of the array for set/get will result in NPE just like any other java.lang.Object.

    You may do something like ... to overcome the NPE,

       String[][] countries;   public void init()
       {
          String[] countryCodes = Locale.getISOCountries();      countries = new String[countryCodes.length][2];
          int x = 0;
          for (String countryCode : countryCodes)
          {
             Locale obj = new Locale("", countryCode);
             String code = obj.getCountry();
             String name = obj.getDisplayCountry();
             countries[x][0] = code;
             countries[x][1] = name;
             x++;
          }
       }
    user580947
  • user580947
    user580947 Member Posts: 2
    edited Dec 8, 2019 7:55PM

    I knew I was missing something!  That worked...thanks!

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Dec 9, 2019 12:21AM

    Check out java.util.HashMap or TreeMap for key/value lookup.