My solution contain the app.config file which contains some key with corresponding
values.
When i am building the solution in the visual studio its runnning fine
when m running the same appliction(.exe) on other machine its not taking value
from config file giving JIITerror 'object reference not set to an instance of an object'
Its a Desktop Application

I have run both debug as well as release version
When i hardcode the string[] values which previously i was taking from config file
it works on user machine.
I am not getting why its not taking value from config file

I can't either since you've posted no code, nor shown what line is causing the error and what values the various variables have near that line.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PNB
{
    
public partial class Form1 : Form
{
public Form1()
{
    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{

    int y;        

    if (openFileDialog1.FileName == "")
        {
            MessageBox.Show("File Not Selected", "Error");
            return;

        } //if (openFileDialog1.FileName == "")

        StreamWriter SW;
        StreamReader SR;
                
        string bn = "";
        string SingleLine = "";
        string strpos2 = System.Configuration.ConfigurationManager.AppSettings["arraycolumn"];//[B]Here is the error[/B]
        char[] sep2 = { ',' };
        string[] number = strpos2.Split(sep2);
        int[] numbers = new int[number.Length];
        
    /*
        int[] numbers = {16,19,27,28,45,75,80,100,105,115,121,129,130,147,
                            150,155,170,180,185,191,197,199,200,212,222,242,247,277,
                            317,357,397,437,477,494,511,528,545,562,592,608,620,630,
                            640,649,653,909}; //int[] numbers
      */  
                
        SR = File.OpenText(openFileDialog1.FileName);

        if (rbPNB.Checked == false)   //From PNB to Ace
        {
            bn = "ACE";
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.FileName = System.DateTime.Now.Date.ToString("ddMMyyyy") + "_" + bn + ".txt";
            saveFileDialog1.ShowDialog();
            SW = File.CreateText(saveFileDialog1.FileName);
                      

            string[] arr = { };//string[] arr
            //int n = number.Length;
            int b = 0;
            int c = 0;
            
            //int numberslength = numbers.Length;

            while ((SingleLine = SR.ReadLine()) != null)
            {
                int index;
                string outputstring = "";
                outputstring = "";   
                for (int i = 0; i < number.Length; i++)
                {
                    if (Int32.TryParse(number[i], out numbers[i]) == true)
                    {
                        if (i == 0)
                        {
                            int a = numbers[i];
                            b = 0;
                            c = a;

                        }
                        else
                        {

                            int a = numbers[i];
                            b = numbers[i - 1];//start
                            c = (a - b);//End                     

                        }
                    }
                     outputstring = String.Concat(outputstring, SingleLine.Substring(b, c));
                     outputstring = String.Concat(outputstring, ",");

                }//for (int i = 0; i < n; i++)

                string st = outputstring;                
                arr = st.Split(',');
                outputstring = "";
                string strpos1 = System.Configuration.ConfigurationManager.AppSettings["array"];[B]Here is the error[/B]
                char[] sep1 = { ',' };
                string[] arraynum1 = strpos1.Split(sep1);
                for (int j = 0; j < arraynum1.Length; j++)
                {
                    index = Int32.Parse(arraynum1[j]);                    
                    outputstring += arr[index] + ",";


                }

                outputstring = outputstring.Trim(',');
                SW.WriteLine(outputstring);

            } // while ( !SR.EndOfStream ) 


            SR.Close();
            SW.Close();

            MessageBox.Show("Successful File Conversion to " + bn);
        }//End of if (rbPNB.Checked == false)

        else   //From Ace to PNB
        {
            int n = numbers.Length;
            int b = 0;
            int c = 0;
            
           
            bn = "PNB";
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.FileName = System.DateTime.Now.Date.ToString("ddMMyyyy") + "_" + bn + ".txt";
            saveFileDialog1.ShowDialog();
            SW = File.CreateText(saveFileDialog1.FileName);

            //SW = File.CreateText("D:\\PNB\\" + System.DateTime.Now.Date.ToString("ddMMyyyy") + "_" + bn + ".txt");
            string temp = "";//declared
            string LineToWrite = "";//declared
            
            

            while ((SingleLine = SR.ReadLine()) != null)
            {
                //MessageBox.Show("Line:" + SingleLine);
            
                char[] sep = { ',' };
                string[] arraynum1 = SingleLine.Split(sep); //ace 


                temp = "";

                for (int x = 0; x < numbers.Length; x++)
                {
                    if (Int32.TryParse(number[x], out numbers[x]) == true)
                    {

                        if (x == 0)
                        {
                            int a = numbers[x];
                            b = 0;
                            c = a;

                        }
                        else
                        {

                            int a = numbers[x];
                            b = numbers[x - 1];//start
                            c = (a - b);//End                     

                        }

                    }
                    if (x == 0 || x == 2 || x == 3 || x == 4 || x == 5 || x == 7 || x == 9 || x == 28 || x == 29 || x == 38)
                    {
                     


                        if (x == 0) 
                        {
                         // 1.        	Account Number	16	Account Number	16	Left	Yes    
                        y = 1;
                        LineToWrite = "";
                        LineToWrite = arraynum1[y] + "".PadRight(c);
                        temp += LineToWrite.Substring(0, c);
                        }

                        if (x == 2)
                        {
                            // 3.        	Service Outlet	Service Outlet
                            string ser_outlet = System.Configuration.ConfigurationManager.AppSettings["service-outlet"];[B]Here is the error[/B]
                            LineToWrite = "";
                            LineToWrite = ser_outlet + "".PadRight(c);
                            temp += LineToWrite.Substring(0, c);
                        }

                        if (x == 3)
                        {
                            //4.        	Part Tran Type	1	 Valid values are “D/C”, corresponding	28-28	NA

                            y = 2;
                            LineToWrite = "";
                            LineToWrite = arraynum1[y] + "".PadRight(c);
                            temp += LineToWrite.Substring(0, c);
                        }
                            
                        
                        if (x == 4)//3
                        {
                            //5.        	Transaction Amount     	17	This should be of NUMBER(14,2). i.e 14 digits before decimal point and 2 digits after decimal point. It should be right justified with spaces in the left. eg, if the amount to be uploaded is 528.35, then it should be ( Eleven spaces and  then    528.35 as 	29-45	Right	Yes
					
                            y = 3;
                            LineToWrite = "";
                            LineToWrite = arraynum1[y];
                            temp += LineToWrite.PadLeft(c - LineToWrite.Length);
                            
                        }
                        if (x == 5)//4
                        {
                            //6.        	Transaction Particulars	30	Transaction Particulars	46-75	Left	No

                            y = 8;
                            LineToWrite = "";                            
                            LineToWrite = arraynum1[y] + "".PadRight(c);
                            temp += LineToWrite.Substring(0, c);                           
                        }
                        if (x == 7)//5
                        {
                            //8.        	Reference Number	20	Reference Number	81-100	Left	No

                            y = 7;
                            LineToWrite = "";
                            LineToWrite = arraynum1[y];
                            temp += LineToWrite.PadLeft(c - LineToWrite.Length);
                        }
                        if (x == 9)//6
                        {
                            //Instrument Date DD-MM-yyyy	10	Mandatory if Instrument Type is present	106-115	Left	CM if Instrument Type is present
					        y = 0;
                            LineToWrite = "";
                            LineToWrite = arraynum1[y] + "".PadRight(c);
                            temp += LineToWrite.Substring(0, c);
                            
                        }
                        if (x == 28)//7
                        {
                            //29.    	Particulars1	40		278-317	Left	 No

                            y = 5;
                            LineToWrite = "";                            
                            LineToWrite = arraynum1[y];
                            temp += LineToWrite;
                            
                        }
                        if (x == 29)//8
                        {
                            //30.    	Particulars_2	40		318-357	Left	 No

                            y = 6;
                            LineToWrite = "";                            
                            LineToWrite = arraynum1[y] + "".PadRight(c);
                            temp += LineToWrite.Substring(0, c); 
                            
                        }
                        if (x == 38)//9
                        {
                            //39.    	Remarks	30		563-592	Left	No
                            
                            if (arraynum1.Length >= 10)
                            {
                                y = 9;
                                LineToWrite = "";
                                LineToWrite = arraynum1[y] + "".PadRight(c);
                                temp += LineToWrite.Substring(0, c);
                            }
                        }

                        


                    }//(x == 0 || x == 3 || x == 4 || x == 5 || x == 7 )
                    else if (x == 1 || x == 14) 
                    {
                        temp += "INR";
                    }
                    else if (x == 1)
                    {
                        temp += "".PadRight(16) + "1";

                    }
                    else
                    {
                        temp = temp + "".PadRight(c);
                    }
                }//for (int x = 1; x <= numbers.Length; x++)

                
                SW.WriteLine(temp);
            }//End Of while loop
            
            SR.Close();
            SW.Close();
            MessageBox.Show("Successful File Conversion to " + bn);

        }    //End Of Else of From Ace to PNB
            
} // Private Button1 click




private void button2_Click(object sender, EventArgs e)
{
            
            
                
                
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
        { 
                    
        }
        filenametextBox.Text = openFileDialog1.FileName;
            


}//End Of button2_click

private void rbPNB_CheckedChanged(object sender, EventArgs e)
{

}//End of rbPNB_checkedChanged

private void rbAce_CheckedChanged(object sender, EventArgs e)
{

}



private void button3_Click(object sender, EventArgs e)
{
    this.Close();
}//Form1_FormClosing

}//End Of the class Form1

}//End of Namespace PNB

Above is the code and m taking array values from config file its working fine on visual studio but when running the exe on user side it giving error

And what is in the app.config file? Not the one on your machine, the one on the machine that is having the problem. The one that is in the same directory as the executable file.

Here is the code in app.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<system.windows.forms jitDebugging="true"/>
	<appSettings>
		<add key="arraycolumn" value="16,19,27,28,45,75,80,100,105,115,121,129,130,147,
                            150,155,170,180,185,191,197,199,200,212,222,242,247,277,
                            317,357,397,437,477,494,511,528,545,562,592,608,620,630,
                            640,649,653,909"/>
		<add key="service-outlet" value="008200"/>
		<!-- <add key="array" value="10,1,4,5,5,29,30,8,6,39"/> -->
		<add key="array" value="9,0,3,4,4,28,29,7,5,38"/>
		<!-- the above array is for Reading File of ACE and Writing to PNB file-->
		<add key="array1" value="39,6,8,30,29,5,5,4,1,10"/>
		<!-- the above array is for Reading File of PNB and writing to ACE file-->		
	</appSettings>
	
</configuration>

If i dont use the config file and i hard code the above values in my code
its runs fine on users end and vice versa is not the same
Momerath thanks for your reply

Add this to your code right before the line that is giving you problems:

NameValueCollection appSettings = ConfigurationManager.AppSettings;
if (appSettings.Count == 0) {
    MessageBox.Show("Nothing in appsettings");
} else {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < appSettings.Count; i++) {
        sb.Append(String.Format("#{0} Key: {1} Value: {2}", 
            i, appSettings.GetKey(i), appSettings[i]));
        sb.Append(Environment.Newline);
    }
    MessageBox.Show(sb.ToString());
}

And check to see what the MessageBox tells you. I might have the case wrong on some of the calls because I just typed that in here :)

Edited 5 Years Ago by Momerath: n/a

Place that block of code in your file before this line:

string strpos2 = System.Configuration.ConfigurationManager.AppSettings["arraycolumn"];//Here is the error

When you run your code a message box will pop up. What does it say in the message box?

If i am using app.config
Its not giving error related to this
Its giving error related to controls exception like button, form
And if am not using config file
Its working fine on user end

This article has been dead for over six months. Start a new discussion instead.