Removing key value pairs from a HashTable

I have a HashTable - charges - and I want to remove a key value pair so I do the following:

remove_key = charges.remove(packages + " " + tariff_id + " 0");

remove_key is an Object and packages and tariff_id are Strings.

This compiles and the key value pairs are removed however I get a "Exception in main: null" when I try to create an instance of the class where I am executing this statement. If I comment out this line I do not get the exception.

moonlxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
Venci75Commented:
try something like this:
if (charges != null) {
  remove_key = charges.remove(packages + " " + tariff_id + " 0");
}
0
 
moonlxAuthor Commented:
no - this makes no difference ...
0
 
Venci75Commented:
What is the code of the constructor of this class?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
moonlxAuthor Commented:
This is within the constructor of the class:

###########################################################
supplier = s;
source_file = f;
if (supplier.equals("vodafone")){
     product_type = 1;
}
else {
     product_type = 2;
}
               
try {
  loadDriver(driver);
  // get a connection to the database
  myConn = DriverManager.getConnection(connectString, "", "");

  System.out.println("Connected to database.");

} catch (SQLException e) {

  System.out.println("Database login failed.");

}
setConfig();
loadGlobalConfig();
loadTariffTranslations();
buildTariffTable();
findDuplicateRegisteredTelephones();

###########################################################

When I call the buildTariffTable() method I am removing the key value pairs from a HashtTable previously declared and assigned.
0
 
Venci75Commented:
what's the code of buildTariffTable() then?
0
 
moonlxAuthor Commented:
you might be sorry you asked but here it is:

###########################################################public void buildTariffTable()
{
      int packagelist[] = new int[40];
      int array_index = 0;
      int package_id;
      int char_test;
      int count;
      char char_value;
      Hashtable record = new Hashtable();
      String tariff_id = new String("0");
      String call_period = new String("0");
      String packages = new String("0");
      String key;
      String key_info;
      String charges_key_info;
      Vector parts;
      Object remove_key;
      Set def_key;
      Set charges_key;
      // get the default tariffs
      loadDefaultTariff();
      try      {
            // get a JDBC statement
            Statement myStat = myConn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            // execute the statement and get a resultset
            ResultSet myRes   = myStat.executeQuery("SELECT package_id FROM Billing.dbo.lookup_packages where product_type_id = " + product_type + " ORDER BY Package_ID");
            while (myRes.next()) {
                  packagelist[array_index] = myRes.getInt(1);
                  array_index++;
            }
                              
            for (int x=0; x < array_index; x++)
            {
                  package_id = packagelist[x];
                  // get the keys of the default charges hash table in the form of a set
                        def_key = def_charges.keySet();
                  // iterate through the set
                  Iterator i = def_key.iterator();
                  while (i.hasNext())
                  {
                        charges.put(package_id + " " + i.next(), def_charges.get(i.next()));
                  }
                  // get the keys of the default markups hash table in the form of a set
                  def_key = def_markups.keySet();
                  // iterate through the set
                  Iterator j = def_key.iterator();
                  while (j.hasNext())
                  {
                        markups.put(package_id + " " + j.next(), def_markups.get(j.next()));
                  }
                  // get the keys of the default fixed fees hash table in the form of a set
                  def_key = def_fixed_fees.keySet();
                  // iterate through the set
                  Iterator k = def_key.iterator();
                  while (k.hasNext())
                  {
                        fixed_fees.put(package_id + " " + k.next(), def_fixed_fees.get(k.next()));
                  }
                  // get the keys of the default connection fees hash table in the form of a set
                  def_key = def_connection_fees.keySet();
                  // iterate through the set
                  Iterator l = def_key.iterator();
                  while (l.hasNext())
                  {
                        connection_fees.put(package_id + " " + l.next(), def_connection_fees.get(l.next()));
                  }
                  // get the keys of the default minimums hash table in the form of a set
                  def_key = def_minimums.keySet();
                  // iterate through the set
                  Iterator m = def_key.iterator();
                  while (m.hasNext())
                  {
                        minimums.put(package_id + " " + m.next(), def_minimums.get(m.next()));
                  }
            }
            // Update table with overriding charges of each package listed in lookup_charges
            myRes   = myStat.executeQuery("SELECT * FROM Billing.dbo.Lookup_Charges ORDER BY Package_ID");
            ResultSetMetaData myMeta = myRes.getMetaData();
            // the number of columns returned by this query
            int numCols = myMeta.getColumnCount();
            while (myRes.next()) {
                  for (int x=1; x <= numCols; x++)
                  {
                        record.put(myMeta.getColumnName(x),myRes.getString(x));
                  }      
                  key = record.get("Package_ID") + " " + record.get("Tariff_ID");
                  charges.put(key + " " + record.get("Call_Period_ID"),record.get("Charge"));
                  markups.put(key,record.get("Markup"));
                  fixed_fees.put(key,record.get("Fixed_Fee"));
            }

            // Update table with overriding fees of each package listed in lookup_fees
            myRes   = myStat.executeQuery("SELECT * FROM Billing.dbo.Lookup_Fees");
            myMeta = myRes.getMetaData();
            // the number of columns returned by this query
            numCols = myMeta.getColumnCount();
            while (myRes.next()) {
                  for (int x=1; x <= numCols; x++)
                  {
                        record.put(myMeta.getColumnName(x),myRes.getString(x));
                  }      
                  key = record.get("Package_ID") + " " + record.get("Tariff_ID");
                  connection_fees.put(key,record.get("Connection_Fee"));
                  minimums.put(key,record.get("Minimum_Charge"));
            }
            // Now remove all tariffs with a call_period_id of 0 if one exists for different time periods

            // get the keys of the charges hash table in the form of a set
            charges_key = charges.keySet();
            // iterate through the set
            Iterator n = charges_key.iterator();
            while (n.hasNext())
            {
                  key_info = (String)n.next();
                  // need to split this string as it contains the package_id, tariff_id and call_period
                  StringTokenizer st = new StringTokenizer(key_info);
                  count = 0;
                  while (st.hasMoreTokens()) {
                        if (count == 0) {packages = st.nextToken();}
                        else if (count == 1) {tariff_id = st.nextToken();}
                        else {call_period = st.nextToken();}
                        count++;
                  }
                        
                  if (!(call_period.equals("0")) && def_charges.containsKey(tariff_id + " 0"))
                  {
                        // If there is a call period of > 0 but there is a DEFAULT of 0 then the 0 has been over-ridden
                        // with separate periods andmust be removed
                              
                        if (charges != null) {
                         remove_key = charges.remove(packages + " " + tariff_id + " 0");
                        }

                        //remove_key = charges.remove(packages + " " + tariff_id + " 0");
                  }
                  else if (call_period.equals("0") && !(def_charges.containsKey(tariff_id + " 0")))
                  {
                        // There is a call period of 0 but none in DEFAULTS - Remove call periods 1, 2 and 3 from charges
                        //remove_key = charges.remove(packages + " " + tariff_id + " 1");
                        //remove_key = charges.remove(packages + " " + tariff_id + " 2");
                        //remove_key = charges.remove(packages + " " + tariff_id + " 3");
                        //System.out.println("Call period 2  - " + call_period + "\n");
                  }
            }
            def_charges = new Hashtable();        
            def_markups = new Hashtable();        
            def_fixed_fees = new Hashtable();      
            def_connection_fees = new Hashtable();;
            def_minimums = new Hashtable();        
      }
      catch (SQLException e){
            System.out.println("Database access error: Message = " + e.getMessage());      
      }
}
###########################################################

Hope this makes sense ...
0
 
Venci75Commented:
ok - can you change your code alittle. Replace
...
} catch (SQLException e){
  System.out.println("Database access error: Message = " + e.getMessage());    
}
with:
} catch (SQLException e){
  e.printStackTrace();    
}

and post the output
0
 
moonlxAuthor Commented:
I replaced the exception message in the main method to  e.printStackTrace();  and received the following output:

###########################################################

java.util.ConcurrentModificationException
        at java.util.Hashtable$Enumerator.next(Unknown Source)
        at Cdr.buildTariffTable(torch_cdr.java:263)
        at Cdr.<init>(torch_cdr.java:79)
        at torch_cdr.main(torch_cdr.java:411)

###########################################################
0
 
mrowkaCommented:
you might want to initialize charges properly in
your constructor.
Also, it might be worth using more recent collection implementations such as ArrayList. see:

http://java.sun.com/docs/books/tutorial/collections/implementations/general.html
0
 
Venci75Commented:
aha - you are using threads. Synchronize the access to the charges then. Enclose the method calls of the charges as follows:
synchronized (charges) {
  remove_key = charges.remove(packages + " " + tariff_id + " 0");
}

but you have to synchronize all the usage of this object everywhere in your code
0
 
mrowkaCommented:
sorry, Hashmap it should read in the last post.
i can't see where you initialize charges
0
 
moonlxAuthor Commented:
I tried synchronizing access but this makes no difference. Here is all the code - maybe it will make more sense. I am trying to teach myself java -  I usually program in Perl so this won't be the best code you have seen:

###########################################################


import java.lang.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.text.*;


class Cdr
{
      private String supplier;                                                      // Supplier name
      private String source_file;                                                      // Name of CDR souece file
      private Hashtable config = new Hashtable();
      private Hashtable global_config = new Hashtable();                  // Global Configuration (directories, logfiles, etc)
      //private Object supplier_id = config.get("supplier_id");            // Supplier ID
      private Hashtable tariff_codes = new Hashtable();                  // Hash table keyed on supplier's tariff containing SpeechNet tariff                              
      private Hashtable charges = new Hashtable();                          // Hash table keyed on (Package_ID Tariff_ID Call_Period_ID) containing per minute Charge
      private Hashtable markups = new Hashtable();                          // Hash table keyed on (Package_ID Tariff_ID) containing wholesale mark-up multiplier
      private Hashtable minimums = new Hashtable();                        // Hash table keyed on (Package_ID Tariff_ID) containing minimum call charge in pence
      private Hashtable fixed_fees = new Hashtable();                        // Hash table keyed on (Package_ID Tariff_ID) containing fixed fee charge in pence
      private Hashtable connection_fees = new Hashtable();                        // Hash table keyed on (Package_ID Tariff_ID) containing connection charge in pence
      private Hashtable def_charges = new Hashtable();        
      private Hashtable def_markups = new Hashtable();         
      private Hashtable def_fixed_fees = new Hashtable();    
      private Hashtable def_connection_fees = new Hashtable();
      private Hashtable def_minimums = new Hashtable();      
      private Object logdir = global_config.get("log_dir");
      private Object logfile = global_config.get("update_log");
      private Hashtable phone_details_cache = new Hashtable();      // Store information per CLI to prevent too much database access
      private Hashtable duplicate_phone_details = new Hashtable(); // Information on phone numbers when they are listed in Reg Phones > Once
      private Hashtable product_details_cache = new Hashtable();      // Store information per Customer ID for internet products
      private Hashtable call_details = new Hashtable();                  // Hash table containing call prices, etc.
      private Hashtable competitors = new Hashtable();                  //competitors = LoadCompetitorPackages($self);                                          // Array of Package_IDs relating to Competitors
      private int product_type;
      private int wholesale = 0;                                                      // Wholesale call cost total in pence
      private int wholesale_imported = 0;                                          // Wholesale call cost total of imported records in pence
      private int retail = 0;                                                            // Retail call cost total of imported records in pence
      private int records = 0;                                                      // Number of CDRs in file
      private int imported = 0;                                                      // Number of records successfully imported
      private int total_duration = 0;                                                // Total Duration of all calls imported in seconds
      private Hashtable missing_tariff = new Hashtable();                  // hash table of missing tariffs, keyed on tariff
      private Hashtable missing_cli = new Hashtable();                  // Hash table of missing CLIs, keyed on CLI.
      private String old_cli = "";                                                // The CLI of the previous CDR
      private byte flag;
      private int source_file_id;
      private final String driver             = "sun.jdbc.odbc.JdbcOdbcDriver";
      private final String connectString    = "jdbc:odbc:qaCustomers";
      private static Connection myConn;
      
      public Cdr (String s, String f)
      {
            supplier = s;
            source_file = f;
            if (supplier.equals("vodafone"))
            {
                  product_type = 1;
            }
            else
            {
                  product_type = 2;
            }
                  
            try {
              loadDriver(driver);

              // get a connection to the database
              myConn = DriverManager.getConnection(connectString, "", "");

              System.out.println("Connected to database.");

            } catch (SQLException e) {

              System.out.println("Database login failed.");

            }
            setConfig();
            loadGlobalConfig();
            loadTariffTranslations();
            buildTariffTable();
            findDuplicateRegisteredTelephones();
            //source_file_id = findSourceFileID(source_file);
      }

      public void setConfig()
      {
            try      {
                  // get a JDBC statement
                  Statement myStat = myConn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                  // execute the statement and get a resultset
                  ResultSet myRes   = myStat.executeQuery("SELECT * FROM Billing.dbo.Lookup_Supplier WHERE Supplier_Name = \'" + supplier + "\'");
                  ResultSetMetaData myMeta = myRes.getMetaData();
                  // the number of columns returned by this query
                  int numCols = myMeta.getColumnCount();
                  // print out the column values
                  while (myRes.next()) {
                        for (int x=1;x<=numCols;x++) {
                              config.put(myMeta.getColumnName(x), myRes.getString(x));
                        }
                  }
            }
            catch (SQLException e){
                  System.out.println("Database access error: Message = " + e.getMessage());      
            }
      }

      public void loadGlobalConfig()
      {
            try      {
                  // get a JDBC statement
                  Statement myStat = myConn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                  // execute the statement and get a resultset
                  ResultSet myRes   = myStat.executeQuery("SELECT * FROM Billing.dbo.Config");
                  ResultSetMetaData myMeta = myRes.getMetaData();
                  // the number of columns returned by this query
                  int numCols = myMeta.getColumnCount();
                  // print out the column values
                  while (myRes.next()) {
                        for (int x=1;x<=numCols;x++) {
                              global_config.put(myMeta.getColumnName(x), myRes.getString(x));
                        }
                  }
            }
            catch (SQLException e){
                  System.out.println("Database access error: Message = " + e.getMessage());      
            }
      }

      public void loadTariffTranslations()
      {
            try      {
                  String key;
                  String value_1;
                  String value_2;
                  // get a JDBC statement
                  Statement myStat = myConn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                  // execute the statement and get a resultset
                  ResultSet myRes   = myStat.executeQuery("SELECT * FROM Billing.dbo.Translate_Tariffs WHERE Supplier_ID = " + config.get("Supplier_ID"));
                  while (myRes.next()) {
                        // must retrive the data from result set in same order as in select statement
                        value_2 = myRes.getString(1);
                        key = myRes.getString(3);
                        value_1 = myRes.getString(4);
                        global_config.put(key, value_1 + "|" + value_2);
                  }
            }
            catch (SQLException e){
                  System.out.println("Database access error: Message = " + e.getMessage());      
            }
      }

      public void buildTariffTable()
      {
            int packagelist[] = new int[40];
            int array_index = 0;
            int package_id;
            int char_test;
            int count;
            char char_value;
            Hashtable record = new Hashtable();
            String tariff_id = new String("0");
            String call_period = new String("0");
            String packages = new String("0");
            String key;
            String key_info;
            String charges_key_info;
            Vector parts;
            Object remove_key;
            Set def_key;
            Set charges_key;
            // get the default tariffs
            loadDefaultTariff();
            try      {
                  // get a JDBC statement
                  Statement myStat = myConn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                  // execute the statement and get a resultset
                  ResultSet myRes   = myStat.executeQuery("SELECT package_id FROM Billing.dbo.lookup_packages where product_type_id = " + product_type + " ORDER BY Package_ID");
                  while (myRes.next()) {
                        packagelist[array_index] = myRes.getInt(1);
                        array_index++;
                  }
                              
                  for (int x=0; x < array_index; x++)
                  {
                        package_id = packagelist[x];
                        // get the keys of the default charges hash table in the form of a set
                        def_key = def_charges.keySet();
                        // iterate through the set
                        Iterator i = def_key.iterator();
                        while (i.hasNext())
                        {
                              synchronized (charges) {
                                    charges.put(package_id + " " + i.next(), def_charges.get(i.next()));
                              }
                        }
                        // get the keys of the default markups hash table in the form of a set
                        def_key = def_markups.keySet();
                        // iterate through the set
                        Iterator j = def_key.iterator();
                        while (j.hasNext())
                        {
                              markups.put(package_id + " " + j.next(), def_markups.get(j.next()));
                        }
                        // get the keys of the default fixed fees hash table in the form of a set
                        def_key = def_fixed_fees.keySet();
                        // iterate through the set
                        Iterator k = def_key.iterator();
                        while (k.hasNext())
                        {
                              fixed_fees.put(package_id + " " + k.next(), def_fixed_fees.get(k.next()));
                        }
                        // get the keys of the default connection fees hash table in the form of a set
                        def_key = def_connection_fees.keySet();
                        // iterate through the set
                        Iterator l = def_key.iterator();
                        while (l.hasNext())
                        {
                              connection_fees.put(package_id + " " + l.next(), def_connection_fees.get(l.next()));
                        }
                        // get the keys of the default minimums hash table in the form of a set
                        def_key = def_minimums.keySet();
                        // iterate through the set
                        Iterator m = def_key.iterator();
                        while (m.hasNext())
                        {
                              minimums.put(package_id + " " + m.next(), def_minimums.get(m.next()));
                        }
                  }
                  // Update table with overriding charges of each package listed in lookup_charges
                  myRes   = myStat.executeQuery("SELECT * FROM Billing.dbo.Lookup_Charges ORDER BY Package_ID");
                  ResultSetMetaData myMeta = myRes.getMetaData();
                  // the number of columns returned by this query
                  int numCols = myMeta.getColumnCount();
                  while (myRes.next()) {
                        for (int x=1; x <= numCols; x++)
                        {
                              record.put(myMeta.getColumnName(x),myRes.getString(x));
                        }      
                        key = record.get("Package_ID") + " " + record.get("Tariff_ID");
                        synchronized (charges) {
                              charges.put(key + " " + record.get("Call_Period_ID"),record.get("Charge"));
                        }
                        markups.put(key,record.get("Markup"));
                        fixed_fees.put(key,record.get("Fixed_Fee"));
                  }

                  // Update table with overriding fees of each package listed in lookup_fees
                  myRes   = myStat.executeQuery("SELECT * FROM Billing.dbo.Lookup_Fees");
                  myMeta = myRes.getMetaData();
                  // the number of columns returned by this query
                  numCols = myMeta.getColumnCount();
                  while (myRes.next()) {
                        for (int x=1; x <= numCols; x++)
                        {
                              record.put(myMeta.getColumnName(x),myRes.getString(x));
                        }      
                        key = record.get("Package_ID") + " " + record.get("Tariff_ID");
                        connection_fees.put(key,record.get("Connection_Fee"));
                        minimums.put(key,record.get("Minimum_Charge"));
                  }
                  // Now remove all tariffs with a call_period_id of 0 if one exists for different time periods
                  // get the keys of the charges hash table in the form of a set
                  synchronized (charges) {
                        charges_key = charges.keySet();
                  }
                  // iterate through the set
                  Iterator n = charges_key.iterator();
                  while (n.hasNext())
                  {
                        key_info = (String)n.next();
                        // need to split this string as it contains the package_id, tariff_id and call_period
                        StringTokenizer st = new StringTokenizer(key_info);
                        count = 0;
                        while (st.hasMoreTokens()) {
                              if (count == 0) {packages = st.nextToken();}
                              else if (count == 1) {tariff_id = st.nextToken();}
                              else {call_period = st.nextToken();}
                              count++;
                        }
                        
                        if (!(call_period.equals("0")) && def_charges.containsKey(tariff_id + " 0"))
                        {
                              // If there is a call period of > 0 but there is a DEFAULT of 0 then the 0 has been over-ridden
                              // with separate periods andmust be removed
                              
                              synchronized (charges) {
                                    remove_key = charges.remove(packages + " " + tariff_id + " 0");
                              }



                              //remove_key = charges.remove(packages + " " + tariff_id + " 0");
                        }
                        else if (call_period.equals("0") && !(def_charges.containsKey(tariff_id + " 0")))
                        {
                              // There is a call period of 0 but none in DEFAULTS - Remove call periods 1, 2 and 3 from charges
                              //remove_key = charges.remove(packages + " " + tariff_id + " 1");
                              //remove_key = charges.remove(packages + " " + tariff_id + " 2");
                              //remove_key = charges.remove(packages + " " + tariff_id + " 3");
                              //System.out.println("Call period 2  - " + call_period + "\n");
                        }
                  }
                  def_charges = new Hashtable();        
                  def_markups = new Hashtable();        
                  def_fixed_fees = new Hashtable();      
                  def_connection_fees = new Hashtable();
                  def_minimums = new Hashtable();        
            }
            catch (SQLException e){
                  e.printStackTrace();      
            }
      }

      public void loadDefaultTariff()
      {
            Hashtable record = new Hashtable();
            try      {
                  // get a JDBC statement
                  Statement myStat = myConn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                  // execute the statement and get a resultset
                  ResultSet myRes   = myStat.executeQuery("SELECT * FROM Billing.dbo.Default_Charges");
                  ResultSetMetaData myMeta = myRes.getMetaData();
                  // the number of columns returned by this query
                  int numCols = myMeta.getColumnCount();
                  while (myRes.next()) {
                         for (int i=1; i <= numCols; i++)
                        {
                              record.put(myMeta.getColumnName(i),myRes.getString(i));
                        }
                        def_charges.put(record.get("Tariff_ID") + " " + record.get("Call_Period_ID"),record.get("Charge"));
                        def_markups.put(record.get("Tariff_ID"), record.get("Markup"));
                        def_fixed_fees.put(record.get("Tariff_ID"), record.get("Fixed_Fee"));
                        def_connection_fees.put(record.get("Tariff_ID"), record.get("Connection_Fee"));
                        def_minimums.put(record.get("Tariff_ID"), record.get("Minimum_Charge"));
                  }
            }
            catch (SQLException e){
                  System.out.println("Database access error: Message = " + e.getMessage());      
            }
      }

      public static void findDuplicateRegisteredTelephones()
      {
            Hashtable hash = new Hashtable();
            // Format the current time.
            SimpleDateFormat formatter = new SimpleDateFormat ("dd/mm/yyyy hh:mm:ss");
            java.util.Date currentTime_1 = new java.util.Date();
            Object value_array[] = new Object[5];
            String customer_id;
            String telephone_number;
            String activation;
            String deactivation = formatter.format(currentTime_1);
            String telephone_type_id;
            String package_id;
            String seconds_per_unit;
            
            try {
                  Statement myStat = myConn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                  String sql = "SELECT phone.Customer_ID, phone.Telephone_Number, phone.Activation_Date, phone.Deactivation_Date, " +
                                    "phone.Telephone_Type_ID, phone.Package_ID, package.Seconds_Per_Unit " +
                                    "FROM   Customers.dbo.Registered_Telephones phone, Billing.dbo.Lookup_Packages package " +
                                    "WHERE  phone.package_id = package.package_id AND package.product_type_id <> 8 AND phone.Telephone_Number IN " +
                                    "(SELECT phone.Telephone_Number " +
                                    "FROM   Customers.dbo.Registered_Telephones phone, Billing.dbo.Lookup_Packages package " +
                                    "WHERE  phone.Package_ID = package.Package_ID AND package.Product_Type_ID <> 8 " +
                                    "GROUP BY Telephone_Number HAVING COUNT(telephone_Number) > 1) " +
                                    "ORDER BY telephone_number";
                  ResultSet myRes = myStat.executeQuery(sql);
                  ResultSetMetaData myMeta = myRes.getMetaData();
                  int numCols = myMeta.getColumnCount();
                  while (myRes.next())
                  {
                        customer_id = myRes.getString(1);
                        telephone_number = myRes.getString(2);
                        activation = myRes.getString(2);
                        deactivation = myRes.getString(3);
                        telephone_type_id = myRes.getString(4);
                        package_id = myRes.getString(5);
                        seconds_per_unit = myRes.getString(6);
                        if (deactivation == null) {
                              deactivation = formatter.format(currentTime_1);
                        }
                         
                        value_array[0] = customer_id;
                        value_array[1] = activation;
                        value_array[2] = deactivation;
                        value_array[3] = package_id;
                        value_array[4] = seconds_per_unit;
                        
                        hash.put(telephone_number, value_array);
                        //@{$hash{$telephone_number}} = (@{$hash{$telephone_number}}, [$customer_id, $activation, $deactivation, $package_id, $seconds_per_unit]);
                  }

                  //value_array = hash.get("01189761770");
                  //System.out.println(value_array[0]);
   
            } catch (SQLException e){
                  System.out.println("Database access error: Message = " + e.getMessage());      
            }
      }

      public static void loadDriver(String driverToLoad)
      {
            try {
              // The newInstance() call is a work around for some broken Java implementations
              Class.forName(driverToLoad).newInstance();
            } catch (Exception E) {
              System.out.println("#" + E.getMessage());
            }
      }
}

public class torch_cdr
{
      public static void main(String[] args)  
      {
            String supplier = "torch";
            String in_file = args[0];
            try      {
                  Cdr c = new Cdr(supplier,in_file);      
            }
            catch (Exception e){
                  e.printStackTrace();
            }
            
      }
}

###########################################################

Thanks for your help - I have increased the points ...
0
 
Venci75Commented:
Actually - the best way is to use Hashtable instead of Hashmap. Then he won't need to synchronize anything. Hashtable has the same functionallity as Hashmap
0
 
Venci75Commented:
Now, I think that the problem is not with the synchronyzation.

Only for demonstration, try this code:
     Hashtable table = new Hashtable();
     table.put("1", "1");
     table.put("2", "2");

     Set set  = table.keySet().;
     Iterator n = set.iterator();
     while (n.hasNext()) {
        table.remove("2");
        Object key = n.next();
     }

and this:
     Hashtable table = new Hashtable();
     table.put("1", "1");
     table.put("2", "2");

     Set set = new HashSet();
     set.addAll(table.keySet());
     Iterator n = set.iterator();
     while (n.hasNext()) {
        table.remove("2");
        Object key = n.next();
     }

the first one have the same problem as yours.
So you can remove the synchronyzed bocks.
To resilve the problem, replace this:

charges_key = charges.keySet();

with this:

charges_key = new HashSet();
charges_key.addAll(charges.keySet());



0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
moonlxAuthor Commented:
That has removed the exception - thanks a lot for your help!
0
 
iartmirkoCommented:
As your are using Hashtable, you shouldn't get a problem with synchronization. I am not quite sure if you really get the NullpointerException in the line you said - even if you don't get it when you comment out the line.

remove_key = charges.remove(packages + " " + tariff_id + " 0");

If charges != null, packages != null and tariff_id != null, then there is no chance for a NullpointerException. And as I saw in your code, you have initialize all these objects.

I haven't seen it in your code, but if the object you want to remove is not in the Hashtable, null is returned. If you are calling any methods on remove_key or you cast it, than you will get a NullpointerException.

Try the following code and post the output please:

System.out.println("charges:" + charges != null);
System.out.println("packages:" + packages != null);
System.out.println("tariff_id:" + tariff_id != null);
System.out.println("before");
remove_key = charges.remove(packages + " " + tariff_id + " 0");
System.out.println("after");

Mirko
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.