Home > Software engineering >  I am not correctly reading from the text file into an ArrayList after parsing each line into an obje
I am not correctly reading from the text file into an ArrayList after parsing each line into an obje

Time:10-07

I am trying to read from the txt file and then parse through it and make each line a new object in an ArrayList. I keeps telling me its null and I cannot figure out why. I have not used java in a long time so I'm sure its dumb.

 public class AccessibilityTest {
      private String cat;
      private String googErr;
      private String waveErr;
      private String sortErr;
      private String lintErr;
      private String desc;
    
      public AccessibilityTest(String cat,String googErr,String waveErr,String sortErr,String lintErr, String desc){
        this.cat = cat;
        this.googErr = googErr;
        this.waveErr = waveErr;
        this.sortErr = sortErr;
        this.lintErr = lintErr;
        this.desc = desc;
      }
    
      public static void main(String[] args) {
        AccessibilityResults.readTxtFile("a11yCheckersResults.txt");//this is one place im getting the error and its me trying to target that txt file
        System.out.println();
      }
    
      public String getCategory() {
        return cat;
      }
      public String getGoogleResult() {
        return googErr;
      }
      public String getWaveResult() {
        return waveErr;
      }
      public String getSortsiteResult() {
        return sortErr;
      }
      public String getAslintResult() {
        return lintErr;
      }
      public String getDescription() {
        return desc;
      }
    
      @Override
      public String toString(){
        return "fsdfse"  getCategory()   getGoogleResult()  getWaveResult()  getSortsiteResult()   getAslintResult()   getDescription();
      }
    }
    

This is the other file where I am actually parsing through the txt file and creating the objects.

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.*;
    
    public class AccessibilityResults {
      
      private static ArrayList<AccessibilityTest> list;
    
      public AccessibilityResults() {
        list = new ArrayList<>();
      }
    
      public static void readTxtFile(String fileName){
        try(Scanner reader = new Scanner(new File(fileName))){
          while(reader.hasNextLine()){
            String cat = reader.next();
            String err1 = reader.next();
            String err2 = reader.next();
            String err3 = reader.next();
            String err4 = reader.next();
            String desc = reader.nextLine();
            list.add(new AccessibilityTest(cat, err1, err2, err3, err4,desc));//this is one spot im getting the error
    
          }
        } catch(FileNotFoundException e){
          System.out.println("File not found: "   fileName);
        }`enter code here`
      }
    }
    
    //txt file
    
    //a11yCheckersResults.txt

CodePudding user response:

The reason is that list and readTxtFile is static method,but the readTxtFile() that init list is not static,and in java static properties and method will be inited before none static which cause it

To solve it,there are several options: one option is just to remove all the static in list and readTxtFile(),another options is just init list when declare it private static ArrayList<AccessibilityTest> list = new ArrayList<>();

  // static
  private static ArrayList<AccessibilityTest> list;

  // no static,so list will always be null
  public AccessibilityResults() {
    list = new ArrayList<>();
  }

 // static
  public static void readTxtFile(String fileName){
    try(Scanner reader = new Scanner(new File(fileName))){
      while(reader.hasNextLine()){
        String cat = reader.next();
        String err1 = reader.next();
        String err2 = reader.next();
        String err3 = reader.next();
        String err4 = reader.next();
        String desc = reader.nextLine();
        list.add(new AccessibilityTest(cat, err1, err2, err3, err4,desc));//this is one spot im getting the error

      }
    } catch(FileNotFoundException e){
      System.out.println("File not found: "   fileName);
    }
  }
}
  •  Tags:  
  • java
  • Related