dimanche 30 avril 2017

Java Singleton pattern creating NullPointException [duplicate]

This question already has an answer here:

I am writing a program to hold information from several Excel spreadsheets. I have a basic UI, a facade class that's a singleton, a personnel class to store objects from the spreadsheets, and a class for each spreadsheet to read the data that also follow the singleton pattern. I thought I had written the singleton pattern properly, but when I try to add a personnel object to a list stored in the facade class I get a NullPointException. I've tried using the enum method for singletons, but I have the same problem.

This is the GUI class:

public class TestGUI
{
    // Instance variables

    private static Base base;


    // Main method

    public static void main(String[] args)
    {
        // Initialise Base facade class
        base = Base.getInstance();
    }
}

This is the facade class, Base:

public class Base
{
    // Instance variables

    private static Base baseInstance;
    private ArrayList<Personnel> personnelList;
    DateTest dateTest;
    private PersonnelDatabase personnelDatabase;

    // Constructor

    private Base()
    {
        personnelList = new ArrayList<>();
        dateTest = new DateTest();
        personnelDatabase = PersonnelDatabase.getInstance();
    }

    // Methods

    public static Base getInstance()
    {
        if (baseInstance == null)
        {
            baseInstance = new Base();
        }
        return baseInstance;
    }


    public ArrayList<Personnel> getPersonnel()
    {
        return personnelList;
    }

    void addPersonnelToList(Personnel aPersonnel)
    {
        personnelList.add(aPersonnel);
    }

    public boolean addPersonnel(String aServiceNumber, String aFirstName, 
            String aSurname, Rank aRank, String aDateOfBirth)
    {
        if (!dateTest.dateTester(aDateOfBirth))
            return false;
        return (personnelList.add(new Personnel(aServiceNumber, aFirstName, 
            aSurname, aRank, aDateOfBirth)));
    }
}

This is one of the classes for reading a spreadsheet:

public class PersonnelDatabase 
{
    // Instance variables

    private static PersonnelDatabase pBInstance;
    private Base base;

    private FileInputStream file;
    private XSSFWorkbook workbook;
    private XSSFSheet sheet;
    private Cell cell;

    private String serviceNumber;
    private String firstName;
    private String surName;
    private Rank rank;
    private String dateOfBirth;

    // Constructor

    private PersonnelDatabase() 
    {
        try
        {
            file = new FileInputStream(new File
                ("./Personnel Database.xlsx"));

            workbook = new XSSFWorkbook(file);
            sheet = workbook.getSheetAt(0);
            cell = null;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        // Create personnel objects from personnel database

        retrieveAllPersonnel();
        closeFile();
    }

    // Private methods

    void closeFile()
    {
        try
        {
            file.close();

            FileOutputStream outFile = new FileOutputStream(new File
                ("./Personnel Database.xlsx"));
            workbook.write(outFile);
            outFile.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private Rank convertRank(String aRank)
    {
        String rank;
        rank = aRank.substring(5);
        rank = rank.trim();
        rank = rank.toUpperCase();
        return Rank.valueOf(rank);
    }

    private void retrieveAllPersonnel()
    {
        boolean result;
        int row = 1;

        // Iterate through rows containing NATO personnel number
        //while(sheet.getRow(row).getCell(1).
            getStringCellValue().substring(0, 3).equals("NII"))
        for (int i = 1; i < 17; i++)
        {
            serviceNumber = sheet.getRow(row).
                    getCell(4).getStringCellValue();
            firstName = sheet.getRow(row).
                    getCell(6).getStringCellValue();
            surName = sheet.getRow(row).
                    getCell(7).getStringCellValue();
            rank = convertRank(sheet.getRow(row).
                    getCell(5).getStringCellValue());
            dateOfBirth = sheet.getRow(row).
                    getCell(10).getStringCellValue();

            // Create personnel and add to base personnel list
            Personnel person = new Personnel(serviceNumber, firstName, 
                    surName, rank, dateOfBirth, row);
            System.out.println(person.getNameAndRank() + "\n");
            base.addPersonnelToList(person);
            //result = base.addPersonnel(serviceNumber, firstName, surName, 
                    rank, dateOfBirth);
            row++;
        }
    }

    public static PersonnelDatabase getInstance()
    {
        if (pBInstance == null)
        {
            pBInstance = new PersonnelDatabase();
        }
        return pBInstance;
    }
}

I am fairly new to programming, so it's probably something obvious, but I have spent hours looking for the cause.

Aucun commentaire:

Enregistrer un commentaire