R
rta_vix
Guest
I am getting the following error: System.NullReferenceException: 'Object reference not set to an instance of an object.'
When I try to go through the lines of my code, to read the bar code field it returns the error just above, it does not even pass the line InBarCodes = true;
This is my method for reading the fields from my file:
public static List<ComprovanteInfo> ParseTextFile(string FullPathFile)
{
List<ComprovanteInfo> returnValue = new List<ComprovanteInfo>();
String[] lines = File.ReadAllLines(FullPathFile);
ComprovanteInfo vi = null;
Boolean InBarCodes = false;
const String NEW_RECORD = "Pagamentos e Transferencias Eletronicas";
const String CodBars = "Cod. Bar:";
const String DatePayment = "Dt.Payment:";
const String DocumentValue = "Vlr.Document:";
const String Protocol = "Protocol:";
const String Register = "Register:";
const String Emission = "Emission.:";
foreach (String line in lines)
{
if (line.StartsWith(DatePayment))
{
String datePart = line.Split(new Char[] { ':' })[1].Trim();
DateTime result = DateTime.ParseExact(datePart, "dd/MM/yyyy", CultureInfo.InvariantCulture);
vi.DatePayment = result;
continue;
}
if (line.StartsWith(DocumentValue))
{
String valuePart = line.Split(new Char[] { ':' })[1].Trim();
Double result = Double.Parse(valuePart.Replace("R$", "").Replace(",", "."));
vi.DocumentValue = result;
continue;
}
if (line.StartsWith(CodBars))
{
String barCodePart = line.Split(new Char[] { ':' })[1].Trim();
vi.CodBars.Add(barCodePart);
InBarCodes = true;
continue;
}
if (InBarCodes)
{
vi.CodBars.Add(line.Trim());
}
if (line.StartsWith(Register))
{
String[] lineParts = line.Split(new Char[] { ' ' });
String datePart = lineParts[1] + " " + lineParts[2];
DateTime result = DateTime.ParseExact(datePart, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
vi.Register = result;
}
if (line.StartsWith(Emission))
{
String[] lineParts = line.Split(new Char[] { ' ' });
String datePart = lineParts[1] + " " + lineParts[2];
DateTime result = DateTime.ParseExact(datePart, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
vi.Emission = result;
}
if (line.StartsWith(Protocol))
{
String Protocol = line.Replace("Protocol:", "").Replace(" ", "");
}
}// fim foreach
//Search Bar Code
string CodProcInter = GetCodProcInter(CodBars);
if (!string.IsNullOrEmpty(CodProcInter))
{
if (!CodBarraJaInserido(CodBars))
{
//Insere Código de Barra
bool inserted = InsertDataBase(CodProcInter, CodBars, DatePayment, DocumentValue, Protocol, Register, Emission);
}
}
if (!(vi == null))
returnValue.Add(vi);
return returnValue;
}
public class VoucherInfo
{
public VoucherInfo()
{
CodBarras = new List<String>();
}
public DateTime DatePayment{ get; set; }
public Double DocumentValue{ get; set; }
public List<String> CodBars { get; set; }
public DateTime Register { get; set; }
public DateTime Emission { get; set; }
public int Protocol { get; set; }
}
StackTrace:
in ImportValue.ReleaseTxt.ParseTextFile (String FullPathFile) in D: \ SVN \ SDP \ SDJ \ branches \ SJ-Import-Proxy Import-v1.0.0 \ Importing Proxy \ Importing Proxy \ ReadingTxt.cs: line 116
() in D: \ SVN Repository \ SDP \ SDJ \ branches \ SJ-Provisional Import-v1.0.0 \ ProxyCount \ ProxyCount \ ReadingTxt.cs: line 58
in System.Threading.ThreadHelper.ThreadStart_Context (Object state)
in System.Threading.ExecutionContext.RunInternal (ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
in System.Threading.ExecutionContext.Run (ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
in System.Threading.ExecutionContext.Run (ExecutionContext executionContext, ContextCallback callback, Object state)
in System.Threading.ThreadHelper.ThreadStart ()
TargetSite
{System.Collections.Generic.List`1 [Provisible Import.ReadText + ProofInfo] ParseTextFile (System.String)}
System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
It is this information that I acquired when I was compiling my application, I do not know if there is a better way to read this file, since I need to read the following fields and get the value of each field:
CodBars Payday
Document Value
Protocol
Record
Emission
This data can come in several lines of the file randomly, because it is a voucher, I have to get and store the information of each field of these items I mentioned above, but it does not work at all: It also follows the structure of this voucher.
Maybe I'm complicating a way to be properly programmed, if someone has more experience and wants to contribute a better practice to take this reading, thank you:
Continue reading...
When I try to go through the lines of my code, to read the bar code field it returns the error just above, it does not even pass the line InBarCodes = true;
This is my method for reading the fields from my file:
public static List<ComprovanteInfo> ParseTextFile(string FullPathFile)
{
List<ComprovanteInfo> returnValue = new List<ComprovanteInfo>();
String[] lines = File.ReadAllLines(FullPathFile);
ComprovanteInfo vi = null;
Boolean InBarCodes = false;
const String NEW_RECORD = "Pagamentos e Transferencias Eletronicas";
const String CodBars = "Cod. Bar:";
const String DatePayment = "Dt.Payment:";
const String DocumentValue = "Vlr.Document:";
const String Protocol = "Protocol:";
const String Register = "Register:";
const String Emission = "Emission.:";
foreach (String line in lines)
{
if (line.StartsWith(DatePayment))
{
String datePart = line.Split(new Char[] { ':' })[1].Trim();
DateTime result = DateTime.ParseExact(datePart, "dd/MM/yyyy", CultureInfo.InvariantCulture);
vi.DatePayment = result;
continue;
}
if (line.StartsWith(DocumentValue))
{
String valuePart = line.Split(new Char[] { ':' })[1].Trim();
Double result = Double.Parse(valuePart.Replace("R$", "").Replace(",", "."));
vi.DocumentValue = result;
continue;
}
if (line.StartsWith(CodBars))
{
String barCodePart = line.Split(new Char[] { ':' })[1].Trim();
vi.CodBars.Add(barCodePart);
InBarCodes = true;
continue;
}
if (InBarCodes)
{
vi.CodBars.Add(line.Trim());
}
if (line.StartsWith(Register))
{
String[] lineParts = line.Split(new Char[] { ' ' });
String datePart = lineParts[1] + " " + lineParts[2];
DateTime result = DateTime.ParseExact(datePart, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
vi.Register = result;
}
if (line.StartsWith(Emission))
{
String[] lineParts = line.Split(new Char[] { ' ' });
String datePart = lineParts[1] + " " + lineParts[2];
DateTime result = DateTime.ParseExact(datePart, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
vi.Emission = result;
}
if (line.StartsWith(Protocol))
{
String Protocol = line.Replace("Protocol:", "").Replace(" ", "");
}
}// fim foreach
//Search Bar Code
string CodProcInter = GetCodProcInter(CodBars);
if (!string.IsNullOrEmpty(CodProcInter))
{
if (!CodBarraJaInserido(CodBars))
{
//Insere Código de Barra
bool inserted = InsertDataBase(CodProcInter, CodBars, DatePayment, DocumentValue, Protocol, Register, Emission);
}
}
if (!(vi == null))
returnValue.Add(vi);
return returnValue;
}
public class VoucherInfo
{
public VoucherInfo()
{
CodBarras = new List<String>();
}
public DateTime DatePayment{ get; set; }
public Double DocumentValue{ get; set; }
public List<String> CodBars { get; set; }
public DateTime Register { get; set; }
public DateTime Emission { get; set; }
public int Protocol { get; set; }
}
StackTrace:
in ImportValue.ReleaseTxt.ParseTextFile (String FullPathFile) in D: \ SVN \ SDP \ SDJ \ branches \ SJ-Import-Proxy Import-v1.0.0 \ Importing Proxy \ Importing Proxy \ ReadingTxt.cs: line 116
() in D: \ SVN Repository \ SDP \ SDJ \ branches \ SJ-Provisional Import-v1.0.0 \ ProxyCount \ ProxyCount \ ReadingTxt.cs: line 58
in System.Threading.ThreadHelper.ThreadStart_Context (Object state)
in System.Threading.ExecutionContext.RunInternal (ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
in System.Threading.ExecutionContext.Run (ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
in System.Threading.ExecutionContext.Run (ExecutionContext executionContext, ContextCallback callback, Object state)
in System.Threading.ThreadHelper.ThreadStart ()
TargetSite
{System.Collections.Generic.List`1 [Provisible Import.ReadText + ProofInfo] ParseTextFile (System.String)}
System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
It is this information that I acquired when I was compiling my application, I do not know if there is a better way to read this file, since I need to read the following fields and get the value of each field:
CodBars Payday
Document Value
Protocol
Record
Emission
This data can come in several lines of the file randomly, because it is a voucher, I have to get and store the information of each field of these items I mentioned above, but it does not work at all: It also follows the structure of this voucher.
Maybe I'm complicating a way to be properly programmed, if someone has more experience and wants to contribute a better practice to take this reading, thank you:
Continue reading...