Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

null pointer exception while reading array

I was trying above example as follows
http://www.go4expert.com/forums/showthread.php?t=1162

public class ArrayReading {
      public static void main( String [] args ) {
      String[][] Data = null;  
      //Assign the values, do it either dynamically or statically
      //For first fow
      Data[0][0] = "S"; //lastname
      Data[0][1] = "Pradeep"; //firstname
      Data[0][2] = "Kolkata"; //location  //Second row
      Data[1][0] = "Bhimani"; //lastname  
      Data[1][1] = "Shabbir"; //firstname  
      Data[1][2] = "Kolkata"; //location
      //Add as many rows you want  
      //printing
      System.out.print("Lastname\tFirstname\tLocation\n");
      for(int i = 0;i<2;i++) {  
             for(int j = 0;j<3;j++)  
            {     System.out.print(Data[j]+"\t");  
             }  
            //move to new line  
            System.out.print("\n");
            }

      }
      

}



When  try to run getting null pointer exception.

Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
Avatar of Gregg
Gregg
Flag of United States of America image

Initialize the array. String[][] Data = new String[2][3]; //two rows with 3 cols

See Java Tutorials: Arrays
Avatar of gudii9

ASKER

changed. still getting null pointer exception.please advise
SOLUTION
Avatar of Gregg
Gregg
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Be sure you account for both the row position and column position in the array.

//This is missing the [i] increment. 
//Also remember toString() will print the String value.
System.out.print(Data[j]+"\t");

//Above should look like this.
System.out.print(Data[i][j].toString()+"\t");

Open in new window

One more time with proper {} indention.

public class ArrayReading {
    public static void main( String [] args ) {
        String[][] Data = new String[2][3];  
        //Assign the values, do it either dynamically or statically
        //For first fow
        Data[0][0] = "S"; //lastname
        Data[0][1] = "Pradeep"; //firstname
        Data[0][2] = "Kolkata"; //location  
        //Second row
        Data[1][0] = "Bhimani"; //lastname  
        Data[1][1] = "Shabbir"; //firstname  
        Data[1][2] = "Kolkata"; //location
        //Add as many rows you want  
        //printing
        System.out.print("Lastname\tFirstname\tLocation\n");
        for(int i = 0;i<2;i++) {  
            for(int j = 0;j<3;j++) {
                System.out.print(Data[i][j].toString()+"\t");
            } //end j
            //move to new line  
            System.out.print("\n");
        }//end i
    }//end main
}//end class

Open in new window


Please take a look at the Java Trails for Arrays Specifically, the "Creating, Initializing, and Accessing an Array" section. Hope that helps.
SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial