From: Roland Haeder Date: Mon, 27 Jul 2015 11:30:57 +0000 (+0200) Subject: Added null reference checks (NPE and asserts) + debug lines + moved variable declaration X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=861b505389e559f7f9e5e07943c66aced4e22e27;p=jfinancials-swing.git Added null reference checks (NPE and asserts) + debug lines + moved variable declaration Signed-off-by:Roland Häder --- diff --git a/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java b/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java index aa82551..2db18d2 100644 --- a/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java +++ b/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java @@ -432,6 +432,9 @@ public class BaseContact extends BaseFrameworkSystem { @Override public int hashCode () { + // Validate gender instance + assert(this.getGender() instanceof Gender) : "gender is not set."; + int hash = 7; hash = 79 * hash + Objects.hashCode(this.getFamilyName()); hash = 79 * hash + this.getGender().hashCode(); diff --git a/Addressbook/src/org/mxchange/addressbook/contact/Gender.java b/Addressbook/src/org/mxchange/addressbook/contact/Gender.java index 96bce2c..a274059 100644 --- a/Addressbook/src/org/mxchange/addressbook/contact/Gender.java +++ b/Addressbook/src/org/mxchange/addressbook/contact/Gender.java @@ -76,6 +76,7 @@ public enum Gender { } // Return it + //* NOISY-DEBUG: */ System.out.println("gender=" + g.getClass().getName()); return g; } diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java index c5f41e0..b109ad4 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java @@ -182,6 +182,15 @@ public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBacken * @param list List instance */ private void addContactToList (final Contact contact, final List list) { + // No null here + if (contact == null) { + // Throw exception + throw new NullPointerException("contact is null"); + } else if (list == null) { + // Throw exception + throw new NullPointerException("list is null"); + } + // Debug message this.getLogger().debug(MessageFormat.format("contact={0}", contact)); @@ -257,6 +266,11 @@ public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBacken StringTokenizer tokenizer; String line; + // Init number/string data values + Long num = null; + Boolean bool = null; + Gender gender = null; + // Read all lines while (!this.isEndOfFile()) { // Then read a line @@ -265,7 +279,7 @@ public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBacken // Debug message this.getLogger().debug(MessageFormat.format("line={0}", line)); - // Then tokenize it + // Then tokenize it // @TODO Move this into separate method tokenizer = new StringTokenizer(line, ";"); @@ -277,6 +291,10 @@ public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBacken // The tokens are now available, so get all while (tokenizer.hasMoreElements()) { + // Reset variables + num = null; + bool = null; + // Get next token String token = tokenizer.nextToken(); @@ -304,12 +322,6 @@ public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBacken // Debug message this.getLogger().debug(MessageFormat.format("strippedToken={0}", strippedToken)); - // Init number/string data values - String strData = strippedToken; - Long num = null; - Boolean bool = null; - Gender gender = null; - // Now, let's try a number check, if no null if (strippedToken != null) { // Okay, no null, maybe the string bears a decimal number? @@ -333,10 +345,19 @@ public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBacken bool = Boolean.valueOf(strippedToken); } - // Now, let's try a boolean check, if no null + // Debug message + this.getLogger().debug(MessageFormat.format("strippedToken={0},num={1},bool={2}", strippedToken, num, bool)); + + // Now, let's try a gender check, if no null if ((strippedToken != null) && (num == null) && (bool == null) && ((strippedToken.equals("M")) || (strippedToken.equals("F")) || (strippedToken.equals("C")))) { // Get first character gender = Gender.fromChar(strippedToken.charAt(0)); + + // Debug message + this.getLogger().debug(MessageFormat.format("strippedToken={0},gender={1}", strippedToken, gender)); + + // This instance must be there + assert (gender instanceof Gender) : "gender is not set by Gender.fromChar(" + strippedToken + ")"; } // Now it depends on the counter which position we need to check @@ -372,6 +393,7 @@ public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBacken case 2: // Surname assert (contact instanceof Contact) : "First token was not boolean"; + assert (gender instanceof Gender) : "gender instance is not set"; // Update data contact.updateNameData(gender, strippedToken, null, null); @@ -379,6 +401,7 @@ public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBacken case 3: // Family name assert (contact instanceof Contact) : "First token was not boolean"; + assert (gender instanceof Gender) : "gender instance is not set"; // Update data contact.updateNameData(gender, null, strippedToken, null); @@ -386,6 +409,7 @@ public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBacken case 4: // Company name assert (contact instanceof Contact) : "First token was not boolean"; + assert (gender instanceof Gender) : "gender instance is not set"; // Update data contact.updateNameData(gender, null, null, strippedToken); @@ -470,6 +494,9 @@ public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBacken count++; } + // The contact instance should be there now + assert(contact instanceof Contact) : "contact is not set: " + contact; + // Add contact this.addContactToList(contact, list); }