0

I learned about ArrayList this week and made a small program. I have a runtime error on line 13 of my HouseListTester Class. I plan on taking input through a text file (houses.txt) and using the values on each line to create an object, and add that object to an array.

How do I properly create an object from a .txt file input, and add that object to an ArrayList?

HouseListTester Class

package RealEstateListings;
import java.util.*;

public class HouseListTester {
    
    public static void main(String[] args) {
        //create scanner for user input via console
        Scanner input = new Scanner(System.in);
        
        System.out.println("Welcome to Mike's House Listing");
        System.out.println("Please enter the file name of the house list: ");
        String sourceFolder = "C:\\Users\\micha\\Documents\\eclipse-workspace\\Real Estate 
Listings\\src\\RealEstateListings\\";
        HouseList fileName = new HouseList((sourceFolder+input.next()));
        
        System.out.println();
        
        System.out.println("Please enter your search criteria");
        
        System.out.print("Minimum price: ");
        int minPrice = input.nextInt();
        System.out.print("Maximum price: ");
        int maxPrice = input.nextInt();
        System.out.print("Minimum area: ");
        int minArea = input.nextInt();
        System.out.print("Maximum area: ");
        int maxArea = input.nextInt();
        System.out.print("Minimum number of bedrooms: ");
        int minBedrooms = input.nextInt();
        System.out.print("Maximum number of bedrooms: ");
        int maxBedrooms = input.nextInt();
        
        Criteria userCriteria = new Criteria(minPrice, maxPrice, minArea, maxArea, minBedrooms, maxBedrooms);   
    }
}

Here is my HouseList class

package RealEstateListings;
import java.util.*;
import java.io.*;
public class HouseList {
    ArrayList<House>houseList;
    public HouseList(String fileName) {
        try {
            //create scanner to read input
            Scanner sc = new Scanner(new File(fileName));
        
            while(sc.hasNextLine()) {
                //input reads to parameters    address     price         area          numBedrooms
                House newListing = new House(sc.next(), sc.nextInt(), sc.nextInt(), sc.nextInt());
                //add newListing to houseList array
                houseList.add(newListing);
            }
        }
        catch(FileNotFoundException e)
        {
            System.out.println("File was not found.");
        }
        }
    public void printHouses(Criteria c) {}
    public String getHouse(Criteria C) {
        return "";
    }   
}

The House Class

package RealEstateListings;

public class House {
    String address;
    int price,area,numberOfBedrooms;
    
    public House(String addr, int salePrice, int saleArea, int numBedrooms) {
        this.address = addr;
        this.price = salePrice;
        this.area = saleArea;
        this.numberOfBedrooms = numBedrooms;
    }
    
    
    public int getPrice() {return this.price;}
    public int getArea() {return this.area;}
    public int getNumberOfBedrooms() {return this.numberOfBedrooms;}
    
    public boolean satisfies(Criteria C) {return true;}
    
    public String toString() {return address;}
}

The Criteria Class and Criteria Constructor have not been finished, and I do not believe it is affecting the ouput, but in case someone needs it for reference to find the solution somehow, here you go:

package RealEstateListings;

public class House {
    String address;
    int price,area,numberOfBedrooms;
    
    public House(String addr, int salePrice, int saleArea, int numBedrooms) {
        this.address = addr;
        this.price = salePrice;
        this.area = saleArea;
        this.numberOfBedrooms = numBedrooms;
    }
    
    public int getPrice() {return this.price;}
    public int getArea() {return this.area;}
    public int getNumberOfBedrooms() {return this.numberOfBedrooms;}
    
    public boolean satisfies(Criteria C) {return true;}
    
    public String toString() {return address;}
}

Here is my console output:

Welcome to Mike's House Listing

Please enter the file name of the house list: 
houses.txt
Exception in thread "main" java.lang.NullPointerException
at RealEstateListings.HouseList.<init>(HouseList.java:18)
at RealEstateListings.HouseListTester.main(HouseListTester.java:13)
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Memzi
  • 27
  • 6
  • To start with, you will need to initialize `houseList` before attempting to read from the file: the first line of the `HouseList` constructor should probably be `houseList = new ArrayList<>();` – Louis Wasserman Oct 02 '20 at 23:55
  • @LouisWasserman that did it! Thank you! Please copy and paste to answer section so I can credit you – Memzi Oct 02 '20 at 23:58

1 Answers1

0

The problem has to do with the HouseList class. The array declared was never initialized. To initialize it houseList = new ArrayList<>();must be in the first line of the HouseList constructor.

More on NullPointException: What is a NullPointerException, and how do I fix it?

Memzi
  • 27
  • 6