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 {
        try{
            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];
              }
              row++;
            }
            //Close the input stream
            in.close();
            } 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++)
                System.out.println(userdata[i][j]);
           }
    }

Thanks!

Where is userdata[][] defined and initialised?

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!

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.