gudii9
asked on
null pointer exception while reading array
I was trying above example as follows
http://www.go4expert.com/forums/showthread.php?t=1162
Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
http://www.go4expert.com/forums/showthread.php?t=1162
public class ArrayReading {When try to run getting null pointer exception.
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\tFirstnam e\tLocatio n\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");
}
}
}
Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
ASKER
changed. still getting null pointer exception.please advise
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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");
One more time with proper {} indention.
Please take a look at the Java Trails for Arrays Specifically, the "Creating, Initializing, and Accessing an Array" section. Hope that helps.
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
Please take a look at the Java Trails for Arrays Specifically, the "Creating, Initializing, and Accessing an Array" section. Hope that helps.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
See Java Tutorials: Arrays