import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.lang.Integer;
public class ExceptionDemo
{
public static int[][] matNodi() throws NumberFormatException, IOException
{
String file = "D:\\Backup\\a.txt"; //a.txt file is local file in that some test.
int matNodi[][] = null;
FileInputStream inputStream = new FileInputStream(file);
DataInputStream in = new DataInputStream(inputStream);
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
int lineCount = 0;
StringTokenizer str;
String line = "";
while ((line = bf.readLine()) != null)
{
str = new StringTokenizer(line);
for ( int j = 0 ; j < str.countTokens(); j++ )
{
matNodi[lineCount][j]= Integer.parseInt(str.nextToken()); // This point null pointer exception occured.
System.out.println(matNodi[lineCount][j]); // Here I try to print matNodi but before it print occured exception.
}
lineCount++;
}
bf.close();
return matNodi;
}
public static void main(String[] args) throws NumberFormatException, IOException
{
int [][] m = matNodi();
System.out.print(m[1][1]);
}
}
In above program , in for loop at "matNodi[lineCount][j]= Integer.parseInt(str.nextToken()); line NullPointerException occuured. And at line "str = new StringTokenizer(bf.readLine());" It should be as str = new StringTokenizer(line); otherwise in str goes from second line.