Click here to Skip to main content
15,891,833 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
in the DnaSequence class I have a method, to operate two DnaSequence instance (seq1 and seq2) and then generate the 3rd seq3, but got failed.

class info:
C#
public byte[] mySites;
// Length.
public int myLength;
// Score.
public int myScore;
// Name.
public string myName;


C#
public DnaSequence (int N)
{
    this.myLength = N;
    this.mySites = new byte[N + PAD];
    this.myScore = 0;
    this.myName = null;
}



in seq1 and seq2, there is byte[170] in the class, the method first set byte[] ancestor as this class byte[] ancestor = this.mySites, and operate the byte[i] in seq1 and seq2, and set the result to ancestor[i].


please help on this. the whole file link is https://github.com/wxiguang/PhylogenyTreeM/blob/master/phyl/DnaSequence.cs[^]

C#
public void setFitchAncestor (DnaSequence seq1,DnaSequence seq2)
           {
               // Get references to sites.
               byte[] ancestor = this.mySites;
               int N = myLength;
               byte[] descendent1 = seq1.mySites;
               byte[] descendent2 = seq2.mySites;

               // Process all sites. Count state changes.
               int nChanges = 0;
               for (int i = 0; i < N; ++i)
               {
                   // Compute intersection of states.
                   int state1 = descendent1[i];
                   int state2 = descendent2[i];
                   int state3 = state1 & state2;
                   // If intersection is not empty, record intersection, otherwise
                   // record union and note one state change.
                   if (state3 == 0)
                   {
                       state3 = state1 | state2;
                       ++nChanges;
                   }
                   // Update site.

                       ancestor[i] = (byte)state3;  ////////////  here cause error 

                   //ancestor[i] = (byte)state3;
                   //this.mySites[i] = (byte)state3;
               }
               // Record number of state changes.
               this.myScore = seq1.myScore + seq2.myScore + nChanges;
           }
Posted
Updated 16-Feb-15 14:06pm
v2
Comments
Sergey Alexandrovich Kryukov 16-Feb-15 20:08pm    
In what line?
—SA
Matt T Heffron 17-Feb-15 13:57pm    
So...did either of the Solutions below help?

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Good luck,
—SA
 
Share this answer
 
Comments
Wang Xiguang 16-Feb-15 20:13pm    
ancestor[i] = (byte)state3; //////////// here cause error

here I got the error message and I run the debug, but don't konw how to fix it. can you give me an example?
Sergey Alexandrovich Kryukov 16-Feb-15 20:38pm    
Why typecast? What is ancestor[i] before assignment. Look with the debugger.
MyLength comes seemingly from nowhere. What makes it equal to the length of descendent1 and descendent2? Writing code in this way is a bad idea.
—SA
I had a suspicion that you had a DnaSequence that didn't initialize the mySites field.
I found it when I looked at the linked code file...
Most of your constructors do not initialize the mySites field!!!
I'd suggest changing them to this:
C#
public DnaSequence()
 : this(0, 0, null)
{
}
public DnaSequence (int N)
 : this(N, 0, null)
{
}
public DnaSequence(int N,int score)
 : this(N, score, null)
{
}
/**
* Construct a new DNA sequence with the given length, score, and name.
*
* @param N Length (number of sites).
* @param score Score.
* @param name Name. May be null.
*
* @exception IllegalArgumentException
* (unchecked exception) Thrown if <TT>N</TT> &lt; 0.
*/
public DnaSequence (int N, int score, string name)
{
    if (N < 0)
    {
        throw new ArgumentException ("DnaSequence(): N (= " + N + ") < 0, illegal)");
    }
    this.mySites = new byte[N + PAD];
    this.myLength = N;
    this.myScore = score;
    this.myName = name;
}

This will ensure that all of the constructor forms will initialize mySites and additionally it ensures they all do the validity check on N.

Also, another way to accomplish the same thing is to use optional arguments:
C#
public DnaSequence (int N = 0, int score = 0, string name = null)
{
    if (N < 0)
    {
        throw new ArgumentException ("DnaSequence(): N (= " + N + ") < 0, illegal)");
    }
    this.mySites = new byte[N + PAD];
    this.myLength = N;
    this.myScore = score;
    this.myName = name;
}

in which case the other 3 forms of constructor should be removed entirely!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900