]> git.mxchange.org Git - jaddressbook-lib.git/commitdiff
Added null reference checks (NPE and asserts) + debug lines + moved variable declaration
authorRoland Haeder <roland@mxchange.org>
Mon, 27 Jul 2015 11:30:57 +0000 (13:30 +0200)
committerRoland Haeder <roland@mxchange.org>
Mon, 27 Jul 2015 11:32:36 +0000 (13:32 +0200)
Signed-off-by:Roland Häder <roland@mxchange.org>

Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java
Addressbook/src/org/mxchange/addressbook/contact/Gender.java
Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java

index aa82551e7b7fbf62a73382ed5acd107c4313afa0..2db18d27525b9c524b5d62bea208542a313d90ec 100644 (file)
@@ -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();
index 96bce2c17f9cfda41de47915ae14358e53f11ff8..a2740595432101b6f7e19c4eacc81531e3c6701e 100644 (file)
@@ -76,6 +76,7 @@ public enum Gender {
                }
 
                // Return it
+               //* NOISY-DEBUG: */ System.out.println("gender=" + g.getClass().getName());
                return g;
        }
 
index c5f41e0204a90055b1c426a076c2a3188ee4f858..b109ad4cea4ec3ae214e81cb293dd66018e9c54d 100644 (file)
@@ -182,6 +182,15 @@ public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBacken
         * @param list List instance
         */
        private void addContactToList (final Contact contact, final List<Contact> 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);
                }