Object reference not set to an instance of an htmlcollection [duplicate] - c#

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 10 years ago.
I need kind favor to signoff the task. I want to count the tag using htmlaglitypack. I tried to count the tag by using htmlcollection node. But getting
"Object reference not set to an instance of an object"
In the line foreach condition. Can anyone of them rectify the issue why I'm getting like that?
My code is posted below:
public void XmlPPC(string rights)
{
int count = 0;
try
{
MessageBox.Show(rights);
using (FileStream fs = File.Open(rights,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite))
using (BufferedStream bs = new BufferedStream(fs))
using (StreamReader sr = new StreamReader(bs))
{
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(sr);
HtmlNodeCollection right = doc.DocumentNode.SelectNodes("//copyrightLine");
foreach (HtmlNode logan in right)
{
count = count + 1;
MessageBox.Show("cnt" + count.ToString());
}
// snip...
}
}
catch (Exception f)
{
log = log + "\r\n" + f.ToString();
}
}

You're getting the error:
Object reference not set to an instance of an object.
because this line:
HtmlNodeCollection right = doc.DocumentNode.SelectNodes("//copyrightLine");
is returning null. That can only happen because there is no element named copyrightLine. Please consider the following specification for the // operation:
Selects nodes in the document from the current node that match the selection no matter where they are.
Now, the fix is one of a few things:
Get an element in there named copyrightLine.
Fix the misspelling as it could be misspelled.
Search for what you need in a different way if it doesn't fall into those two.

Related

Index was outside the bounds of the array while using Lucene.NET

I am using Lucene.net version 3 library to index the data and search on them but for some reason, sometimes it throws a weird error message internally and I can not even understand why this happens. Below is my code when I try to search on the index:
using (var analyzer = new StandardAnalyzer(Version.LUCENE_30))
{
using (var reader = IndexReader.Open(directory, true))
{
using (var searcher = new IndexSearcher(reader))
{
term = QueryParser.Escape(term);
var parsedQuery =
new MultiFieldQueryParser(Version.LUCENE_30, includedFields, analyzer)
.Parse(term);
var result = searcher.Search(parsedQuery, reader.MaxDoc); //Here happens the error
totalCount = result.TotalHits;
var matches = result.ScoreDocs.OrderByDescending(x => x.Score).ToPaginated(page, size);
foreach (var match in matches)
{
int docId = match.Doc;
var document = searcher.Doc(docId);
var item = //...
list.Add(item);
}
}
}
}
This error message does not always happen, it only happens when some random text string are being set as a searching term, so sometimes it works well and sometimes crashes.
I am trying to escape the term but still no luck. Does anybody have any idea what I might be doing wrong in here ?
Here is the error message:
at Lucene.Net.Search.TermScorer.Score() in d:\Lucene.Net\FullRepo\trunk\src\core\Search\TermScorer.cs:line 136 at Lucene.Net.Search.DisjunctionSumScorer.AdvanceAfterCurrent() in d:\Lucene.Net\FullRepo\trunk\src\core\Search\DisjunctionSumScorer.cs:line 187 at Lucene.Net.Search.DisjunctionSumScorer.NextDoc() in d:\Lucene.Net\FullRepo\trunk\src\core\Search\DisjunctionSumScorer.cs:line 155 at Lucene.Net.Search.BooleanScorer2.NextDoc() in d:\Lucene.Net\FullRepo\trunk\src\core\Search\BooleanScorer2.cs:line 397 at Lucene.Net.Search.BooleanScorer.NextDoc() in d:\Lucene.Net\FullRepo\trunk\src\core\Search\BooleanScorer.cs:line 369 at Lucene.Net.Search.BooleanScorer.Score(Collector collector) in d:\Lucene.Net\FullRepo\trunk\src\core\Search\BooleanScorer.cs:line 389 at Lucene.Net.Search.IndexSearcher.Search(Weight weight, Filter filter, Collector collector) in d:\Lucene.Net\FullRepo\trunk\src\core\Search\IndexSearcher.cs:line 221 at Lucene.Net.Search.IndexSearcher.Search(Weight weight, Filter filter, Int32 nDocs) in d:\Lucene.Net\FullRepo\trunk\src\core\Search\IndexSearcher.cs:line 188 at Blog.Services.SearchService.<>c__DisplayClass3_0.b__0() in E:\Blog\Blog\Blog.Services\SearchService.cs:line 98 at
Here is a random string which makes it crash: 212 i think webapp using ef db first i am trying to use lazy. The string does not have any meaning but still strange to me why it should make lucene crash...

XmlReader skipping adjoined elements

Whilst trying to minimise the memory footprint of an XML parsing program, specifically avoiding the loading hundreds of megabytes using XElement.Load(), I came across articles suggesting using the older XmlReader e.g. here.
I need to internally reconstruct each major element as an XElement to avoid major refactoring. However, I discovered that if my source elements are directly adjoining, this approach skips every 2nd element.
I've torn down the problem to this unit-test (MSTest2 with FluentAssertions):
[DataTestMethod]
[DataRow("<data><entry>1</entry><entry>2</entry><entry>3</entry><entry>4</entry></data>")]
[DataRow("<data><entry>1</entry> <entry>2</entry> <entry>3</entry> <entry>4</entry></data>")]
public void XmlReaderCount(string input)
{
var sr = new StringReader(input);
var xml = XmlReader.Create(sr);
xml.MoveToContent();
var data = new List<string>();
while (xml.Read())
{
if (xml.LocalName == "entry" && xml.NodeType == XmlNodeType.Element)
{
var element = (XElement)System.Xml.Linq.XNode.ReadFrom(xml);
data.Add(element.Value);
}
}
data.Should()
.HaveCount(4);
}
The first (data-driven) test fails with:
Expected collection to contain 4 item(s), but found 2.
As it puts 1 and 3 into the data collection. It does loop 4 times, but every other element has an xml.NodeType of Text, not Element. The second test (with spaces between the </entry> and <entry> passes by processing all 4.
In my real world example, I can't easily change the source. I do already have a solution, inspired by another StackOverflow question so I can do the following, but it seems strange - is something wrong?
[DataTestMethod]
[DataRow("<data><entry>1</entry><entry>2</entry><entry>3</entry><entry>4</entry></data>")]
[DataRow("<data><entry>1</entry> <entry>2</entry> <entry>3</entry> <entry>4</entry></data>")]
public void XmlReaderCountSubtree(string input)
{
var data = new List<string>();
var sr = new StringReader(input);
var xml = XmlReader.Create(sr);
xml.MoveToContent();
while (xml.Read())
{
if (xml.LocalName == "entry" && xml.NodeType == XmlNodeType.Element)
{
using (var subtree = xml.ReadSubtree())
{
subtree.MoveToContent();
var content = subtree.ReadOuterXml();
var element = XElement.Parse(content);
data.Add(element.Value);
}
}
}
data.Should()
.HaveCount(4);
}
When you call ReadFrom(xml) , the state of xml is changed. Its cursor is moved forward to the next element. Your code then moves on to while (xml.Read()) and so ignores that new element completely.
With the second data set, the ignored (and uninspected) elements are the whitespace nodes so you get away with it. But basically, you reading algorithm is wrong.
A fix for your first approach, not pretty but it works:
xml.Read();
while (! xml.EOF)
{
if (xml.LocalName == "entry" && xml.NodeType == XmlNodeType.Element)
{
//using (var subtree = xml.ReadSubtree())
{
var element = (XElement)XNode.ReadFrom(xml);
data.Add(element.Value);
}
}
else
{
xml.Read();
}
}

C# How do I write a variable to a file? [duplicate]

This question already has answers here:
How to save a List<string> on Settings.Default?
(4 answers)
Saving from List<T> to txt
(7 answers)
Closed 7 years ago.
I'm new to C# and I'm trying to make a List persist when relaunching the application. Everywhere I go I can't seem to find a simple way to do this, something like Python's Pickle. Any help would be appreciated thank you.
The answer to this really depends on what exactly you want to save. Is it an actual List, as in List<> obejct? What does it contain? If it's something simple such as a List< string >, then do
var list = new List<string>();
list.Add("HELLO");
list.Add("hi");
// save
using (var fs = new FileStream(#"F:\test.xml", FileMode.Create))
{
var serializer = new XmlSerializer(typeof(List<string>));
serializer.Serialize(fs, list);
}
// read
using (var s = new FileStream(#"F:\test.xml", FileMode.Open))
{
var serializer = new XmlSerializer(typeof(List<string>));
List<string> result = (List<string>)serializer.Deserialize(s);
}

Adding elements to List<> doesn't work [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 years ago.
I have a List<> declared, which is accessable in the whole class
List<article> data;
Now I'm using a method to fill the List<>:
StreamReader sr = new StreamReader(filePath);
while (!sr.EndOfStream)
{
string[] Line = sr.ReadLine().Split(';');
article newArticle = new article();
newArticle.articleNumber = Line[0];
newArticle.description = Line[1];
newArticle.articleId = Line[2];
try
{
data.Add(newArticle);
}
catch(NullReferenceException ex)
{
// Nothing to do here
}
}
Each time, the loop repeats, the newArticle-Object contains all his elements, so it is definetely not null.
But it doesn't add to the data-List<>.
What am I missing?
In order to add items to the list, you must first initialise it.
Replace:
List<article> data;
with:
List<article> data = new List<article>();

How to Clone POCO entity and add to context

I am using EF4 and I have create POCO objects with proxies from my database structure . I have a POCO (object) which has lots of relationships to other entities.
I created a deep copy of the object using DataContractSerializer and BinaryFormatter and lets call it clonedObject.
function used for cloning is:
public T CloneProxy<T>(T source)
{
var dcs = new System.Runtime.Serialization
.DataContractSerializer(typeof(T));
string filePath = "Initiative.txt";
using (FileStream file = new FileStream(filePath, FileMode.Create))
{
(new BinaryFormatter()).Serialize(file, source);
}
context.CreateProxyTypes(new Type[] { typeof(Initiative) });
using (FileStream file = new FileStream(filePath, FileMode.Open))
{
return (T)(new BinaryFormatter()).Deserialize(file);
}
}
Now that I have clonedObject, how do I add it to the context? how do I add it to the database?
my object (just giving you an Idea of the POCO Initiative):
Initiative
{
InitI
InitName
<collection>Comments
}
Comments
{
CommentI
<FK>InitI
}
Here are some of the way I have done and errors I have received.
cloneInit.InitI = 0;
Data_Business.RQRMComment[] arr = new Data_Business.RQRMComment[1];
arr = cloneInit.RQRMComments.ToArray();
for (int x = 0; x < arr.Length; x++) //each (var x in cloneInit.RQRMComments)
{
RQRMComment thisC = arr[x];
int y = thisC.InitI;
thisC.InitI = 0;
thisC.ID = 0;
}
Context.AddObject("Initiatives", cloneInit);
Context.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
Error:
ex = {"The object could not be added or attached because its EntityReference has an EntityKey property value that does not match the EntityKey for this object."}
Please help, I have spent too much time on this. Thank you.
I have had a need to clone my Entities for the purpose of re-displaying the data on a form so that a user can choose to "Create & Add Similar" in an effort to reduce the amount of effort a user needs to expend in order to add a range of similar items to my DB.
I checked out a few options including reflection & serialization but they are messy for what I am trying to achieve, I then discovered that I can overcome the "XYZ is part of the object's key information and cannot be modified" issue - i.e. set my entities primary key to 0 after insert (Save Changes) - with the following code:
MyDbEntities bb = new MyDbEntities();
//Add & Save new entry
db.Product.AddObject(product);
db.SaveChanges();
//Reset entity
db.ObjectStateManager.ChangeObjectState(product, System.Data.EntityState.Added);
product.ProductId = 0;

Categories