Search code examples
javaapache-commons-config

Problems reading an ini file in Java


I am having trouble using Apache commons configuration to read an ini file. I attached the imports incase I am missing something. Below is an example I found on stackoverflow, and as far as I can find, there are no other examples to look at. The problem is iniObj. Using Eclipse it is highlighted in red.

If I initialize the variable, new "HierarchicalINIConfiguration(iniFile); gets angry and wants to add a try/catch or throws... which should be no problem... but then the try/catch or throws gets angry and says "No exception of type ConfigurationException can be thrown; an exception type must be a subclass of Throwable."

Which then brought me to this question. I added the commons lang 3.1. I have commons config 1.9, commons collections 3.2.1. commons logging 1.1.1 as well. I have also tried this with commons config 1.8 and lang 2.6. Now I get a new error "Exception in thread "main" java.lang.NullPointerException at com.toolbox.dev.ReadIni.main(ReadIni.java:28)" You can see the new code below after the adjustments I made to try and resolve the errors.

My code:

import java.util.Iterator;
import java.util.Set;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalINIConfiguration;
import org.apache.commons.configuration.SubnodeConfiguration;

public static void main(String[] args) throws ConfigurationException {
    String iniFile = "file.ini";
    HierarchicalINIConfiguration iniConfObj = new HierarchicalINIConfiguration(iniFile);

    // Get Section names in ini file
    Set setOfSections = iniConfObj.getSections();
    Iterator sectionNames = setOfSections.iterator();

    while(sectionNames.hasNext()) {
        String sectionName = sectionNames.next().toString();

        HierarchicalINIConfiguration iniObj = null;
        SubnodeConfiguration sObj = iniObj.getSection(sectionName);
        Iterator it1 = sObj.getKeys();

        while (it1.hasNext()) {
            // Get element
            Object key = it1.next();
            System.out.print("Key " + key.toString() +  " Value " +
              sObj.getString(key.toString()) + "\n");
        }
    }
}

Original code from Stack Overflow:

import java.util.Iterator;
import java.util.Set;

import org.apache.commons.configuration.HierarchicalINIConfiguration;
import org.apache.commons.configuration.SubnodeConfiguration;


public class ReadIni {

    public static void main(String[] args) {
        String iniFile = "";
        HierarchicalINIConfiguration iniConfObj = new HierarchicalINIConfiguration(iniFile);

        // Get Section names in ini file
        Set setOfSections = iniConfObj.getSections(); 
        Iterator sectionNames = setOfSections.iterator(); 

        while(sectionNames.hasNext()) {

            String sectionName = sectionNames.next().toString();
            SubnodeConfiguration sObj = iniObj.getSection(sectionName);
            Iterator it1 = sObj.getKeys();

            while (it1.hasNext()) {
                // Get element 
                Object key = it1.next(); 
                System.out.print("Key " + key.toString() +  " Value " +
                  sObj.getString(key.toString()) + "\n");
            }
        }

Solution

  • Since you have already initialized the HierarchicalINIConfiguration (second line in "main") as :

       HierarchicalINIConfiguration iniConfObj = new HierarchicalINIConfiguration(iniFile);
    

    I believe you want to remove HierarchicalINIConfiguration iniObj = null; (around 5 lines down) from your code and change

      SubnodeConfiguration sObj = iniObj.getSection(sectionName); 
    

    to (use iniConfObj in place of iniObj)

       SubnodeConfiguration sObj = iniConfObj.getSection(sectionName);