Hi! I'm having a problem with my coding. I have initialized a 2d array (String), but when I try to print a value from it, it gives me null.
this is how I initialized my array.

String phone[][] = { {"A", "B","C"},
       {"D", "E", "F"},
       {"G","H", "I"},
       {"J", "K", "L"},
       {"M", "N", "O"},
       {"P", "R", "S"},
       {"T", "U", "V"},
       {"W", "X", "Y"} };

when I try to print a value like this

System.out.println("Element in [0][2]is: "+ phone[0][2]);

it gives an error... NullPointerException:null

What have I done wrong..??

Ishani

We need to see more code

This is the coding from the constructor.
I neede to generate all the words that can be created for a 7 digit telephone number.

public TelePhone_Number_Word_Generator()
    {
       String phone[][] = { {"A", "B","C"},
       {"D", "E", "F"},
       {"G","H", "I"},
       {"J", "K", "L"},
       {"M", "N", "O"},
       {"P", "R", "S"},
       {"T", "U", "V"},
       {"W", "X", "Y"} };
       
       //initialize the one dimentional array to ZERO
       intialArray();
       
       words = new ArrayList();
       in = new BufferedReader(new InputStreamReader(System.in));
       
       File filePhone;
       try
       {
           filePhone = new File ("Number_Word.txt");
           if(!filePhone.exists())
           {
               filePhone.createNewFile();
            }
        }
        catch(Exception e){
            System.out.println("Can't create the file.");
        }
        startGenerator();
    }

    /**
     * An example of a method - replace this comment with your own
     * 
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y 
     */
    
    public void startGenerator()
    {
       String inValue = null;
       boolean quit = false;
       while (!quit)
       {
       do
       {
           System.out.println("Enter the phone number");
           System.out.print("- ");
           inValue = getInput();
           System.out.println("test");
        }
        while(!(isValidInteger(inValue)));
        System.out.println("test1");
        genWords();
        System.out.println("Finish task");
        quit = true;
    }
    }
    
    /**
     * Checks if the user input is an integer.
     * @param value Value user have entered. String type
     * @return returns true or false
     */
     private boolean isValidInteger(String value)
  {
     //return false, if the input is empty.
     if(value.length() == 0)
     {
         System.out.println("You need to enter a value.");
         return false;
      }
      
      //ensure all the characters are digits
      int i = 0;
      while (i < value.length())
      {
          if(!Character.isDigit(value.charAt(i)))
          {
              System.out.println("testv##");
              //if ((value.charAt(i) == 0) || (value.charAt(i) == 1))
              //{
                  System.out.println("Your entry is not valid.");
                  break;
              // }
            }
              else
              {
                  char num = value.charAt(i);
                number[i] = Integer.parseInt(Character.toString(num));
                System.out.println(" Nimber: "+ number[i]);
                i++;  
               }
        }
        
        // if reached the end of the line. all characters are OK!
        System.out.println("Length of string" + value.length());
        System.out.println("Size of i is: " + i);
        
        if (i == value.length())
        {
            return true;
        }
        else
        {
            //intialArray();
            return false;
        }
    }
  /**
   * Get the user input from the screen
   * @return Input the value user entered
   */  
    private String getInput()
    {
        String Input = "";         
        try{
            Input = in.readLine().trim();
        }
        catch(IOException e){
            System.out.println(e);
        }
        return Input;
     }
     
     /**
      * Initialize the one dimentional array to ZERO
      */
     private void intialArray()
     {
        for(int i = 0; i < number.length; i++)
        number[i] = 0;
    }
    
    /** 
     * Save the array to the file
     */
    public void savePhoneWords()
    {
          try{
            WRITER = new  BufferedWriter(new FileWriter("Number_Word.txt"));
            Iterator it = words.iterator();
            String wordDetails;
            while(it.hasNext()) {
                //theWord = (Number_Words) it.next();
                //wordDetails = theWord.getWord();
                wordDetails = (it.next()).toString();
                WRITER.write(wordDetails);
            }
            WRITER.close();
        }
        catch(IOException e){
            System.out.println(e);
        }          
    }
  
    /**
     * Generate the words for the number
     */
    private void genWords()
    {
        String genWord = null;
      int num1 = 0;
      num1 = number[0] - 2;
      System.out.println("Num1 = " + num1);
      int num2 = 0;
      num2 = number[1] - 2;
      int num3 = 0;
      num3 = number[2] - 2;
      int num4 = 0;
      num4 = number[3] - 2;
      int num5 = 0;
      num5 = number[4] - 2;
      int num6 = 0;
      num6 = number[5] - 2;
      int num7 = 0;
      num7 = number[6] - 2;
     
      int a, b, c, d, e, f, g = 0;
      int COLS = 3;
      
      
System.out.println("Element at [0][2] is: " + phone[0][2]);     
      do{
          for (a = 0; a <COLS; a++)
          {
          do{
              for ( b = 0; b <COLS; b++)
              {
              do{
                 for ( c = 0; c <COLS; c++)
                 {
                 do{
                     for (d = 0; d <COLS; d++)
                     {
                     do{
                         for ( e = 0; e <COLS; e++)
                         {
                         do{
                            for ( f = 0; f <COLS; f++)
                            {
                            do{
                               for (g = 0; g < COLS; g++)
                               {
                                 String letter1 = phone[num1][a];
                                 System.out.println("letter :"+letter1);
                                 String letter2 = phone[num2][b];
                                 String letter3 = phone[num3][c];
                                 String letter4 = phone[num4][d];
                                 String letter5 = phone[num5][e];
                                 String letter6 = phone[num6][f];
                                 String letter7 = phone[num7][g];
                                 genWord = ((((((letter1.concat(letter2)).concat(letter3)).concat(letter4)).concat(letter5)).concat(letter6)).concat(letter7));
                                 //System.out.println("Generated Word " + genWord);
                                 words.add(genWord);//letter1+ letter2 + letter3  + letter4+ letter5+ letter6 + letter7);
                                savePhoneWords(); 
                                 
                                }
                            }while (g <= 2);
                            }}while (f <=2);
                        }}while (e <=2);
                    }}while (d <= 2);
                }}while (c <= 2);
            }}while (b <= 2);
        }}while (a <= 2);          
           
      savePhoneWords();      
        
    }
}

The phone[][] is null. That is why you are getting the exception.

And no this:

String phone[][] = { {"A", "B","C"},
       {"D", "E", "F"},
       {"G","H", "I"},
       {"J", "K", "L"},
       {"M", "N", "O"},
       {"P", "R", "S"},
       {"T", "U", "V"},
       {"W", "X", "Y"} };

Is not the same phone array as the one you are trying to access. The one you created is locally in the constructor. Your other methods can't see it.
when you are calling it form your code:

System.out.println("Element in [0][2]is: "+ phone[0][2]);

You are calling the one globally declared. You need to create that one in your constructor:

String [][] phone = null;

public TelePhone_Number_Word_Generator()
    {
       phone  = new String[8][3];
    
      phone[0][0] = "A";
      ...
      ...
    }

Thank you very much. It worked...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.