Hi Everyone!

I have a problem with reading data from the txt file into the 2d array. In general, the above code is workable. The only thing that doesn't work is: userdata[row][j] = tokens[j]; The problem is that my array "userdata" gets values "null". However, if I replace this code with "System.out.println(tokens[j]);", then I can see the values from the file "UserDataFile.txt". What is wrong in my code?

public void readuserdatafromfile() throws IOException {
            FileInputStream fstream = new FileInputStream("src/filetree/UserDataFile.txt");
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine = "";
            String[] tokens = strLine.split("\\s");
            //Read File Line By Line
            int row = 0;
            while ((strLine = br.readLine()) != null)   {
              // Copy the content into the array
              tokens = strLine.split("\\s");
              for(int j = 0; j < tokens.length; j++) {
                 userdata[row][j] = tokens[j];
            //Close the input stream
            } catch (Exception e){//Catch exception if any
              System.err.println("Error: " + e.getMessage());

           for(int i = 0; i < userdata.length; i++) {
               for(int j=0; j < userdata[i].length; j++)


I see that I am able to read from "userdata" to JTable. Don't know why System.out.println didn't output the values. So, I gues the problem is fixed. Thanks!

This question has already been answered. Start a new discussion instead.