trying to solve a NullReference not handled error(array trouble)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: trying to solve a NullReference not handled error(array trouble)

  1. #1
    Join Date
    Oct 2012
    Posts
    14

    trying to solve a NullReference not handled error(array trouble)

    I have an error that says Object reference not set to an instance of an object
    the program takes a string of DNA characters from a text file and converts them to RNA by way of a for loop and a series of if statements which do the actual conversion. the program doesnt show any syntax errors but when I try to Run the program it hangs up on the first if statement which gives me the error. I originally wrote this code In C++ and the program looks a bit different and the array is a char array instead of a string array. I was thinking that had something to do with it but when I change it back to char i have to give the array size about [500] just so it will be in bounds and this time nothing will print out in my text box. I guess what I'm asking is how do I make this array dynamic and how do I solve the problem of my results not printing when I put a large enough value of elements.

    Basically what would you do different here?

    here's my code

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    
    namespace bioinformaticsCal
    {
    
        public partial class Form1 : Form
        {
            int numberOfCodons;
            int rnaLength;
            int beginIndex;
            int endIndex;
            int i;
            int j;
            int k;
            string[] codons = { "UUU", "UUC", "UUA", "UUG", "UCU", "UCC", "UCA", "UCG", "UAU", "UAC", "UAA", "UAG", "UGU", "UGC", "UGA", "UGG", "CUU", "CUC", "CUA", "CUG", "CCU", "CCC", "CCA", "CCG", "CAU", "CAC", "CAA", "CAG", "CGU", "CGC", "CGA", "CGG", "AUU", "AUC", "AUA", "AUG", "ACU", "ACC", "ACA", "ACG", "AAU", "AAC", "AAA", "AAG", "AGU", "AGC", "AGA", "AGG", "GUU", "GUC", "GUA", "GUG", "GCU", "GCC", "GCA", "GCG", "GAU", "GAC", "GAA", "GAG", "GGU", "GGC", "GGA", "GGG" };
            string[] aminoAcids = { "Phenylalanine", "Phenylalanine", "Leucine", "Leucine", "Serine", "Serine", "Serine", "Serine", "Tyrosine", "Tyrosine", "Stop", "Stop", "Cysteine", "Cysteine", "Stop", "Tryptophan", "Leucine", "Leucine", "Leucine", "Leucine", "Proline", "Proline", "Proline", "Proline", "Histidine", "Histidine", "Glutamine", "Glutamine", "Arginine", "Arginine", "Arginine", "Arginine", "Isoleucine", "Isoleucine", "Isoleucine", "Methionine", "Threonine", "Threonine", "Threonine", "Threonine", "Asparagine", "Asparagine", "Lysine", "Lysine", "Serineine", "Serineine", "Arginine", "Arginine", "Valine", "Valine", "Valine", "Valine", "Alanine", "Alanine", "Alanine", "Alanine", "Aspartate", "Aspartate", "Glutamate", "Glutamate", "Glycine", "Glycine", "Glycine", "Glycine" };
            string codon = "";
            string protein = "";
            string [] DNA;
            
            string RNA;
            string SEQ;
            int len;
           
            
            public Form1()
            {
                InitializeComponent();
            }
           
           
            private void Form1_Load(object sender, EventArgs e)
            {
                string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                if (!Directory.Exists(path + "\\Bioinformatics Data Files - Jason"))
                    Directory.CreateDirectory(path + "\\Bioinformatics Data Files - Jason");
                if (!File.Exists(path + "\\Bioinformatics Data Files - Jason\\settings.xml"))
                    File.Create((path + "\\Bioinformatics Data Files - Jason\\settings.xml"));
                File.Create((path + "\\Bioinformatics Data Files - Jason\\TextFiles.txt"));
    
            }
    
    
    
            private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog Fd = new OpenFileDialog();
                if (Fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    StreamReader Sr = new StreamReader(File.OpenRead(Fd.FileName));
                    //RNA = Sr.ReadToEnd();
                   SEQ = Sr.ReadToEnd();
    
                   string strCompare = SEQ;
                   string strStringContains = "T";
                   var matchingString = strCompare.IndexOfAny(strStringContains.ToCharArray()) != -1;
                   if (matchingString) 
                   {
                       
         
    for(int i=0; i<=SEQ.Length; i++){
        
    	if (DNA[i]=="A")
    	{richTextBox1.AppendText("U");
    	 len++;
    	 }
    		if (DNA[i] == "G")
    		{richTextBox1.AppendText("C");
    		len++;
    		}
    			if (DNA[i] == "C")
    			{richTextBox1.AppendText("G");
    			len++;
    			}
    	
    				if (DNA[i] == "T")
                    {richTextBox1.AppendText("A");
    				len++;
    				}
    				
             }
    This is where the trouble begins
    right at the first if statement
    Code:
    for(int i=0; i<=SEQ.Length; i++){
        
    	if (DNA[i]=="A")
    	{richTextBox1.AppendText("U");
    	 len++;
    	 }
    		if (DNA[i] == "G")
    		{richTextBox1.AppendText("C");
    		len++;
    		}
    			if (DNA[i] == "C")
    			{richTextBox1.AppendText("G");
    			len++;
    			}
    	
    				if (DNA[i] == "T")
                    {richTextBox1.AppendText("A");
    				len++;
    				}
    				
             }

  2. #2
    Join Date
    Jan 2009
    Posts
    596

    Re: trying to solve a NullReference not handled error(array trouble)

    Quote Originally Posted by ApacheOmega View Post
    I originally wrote this code In C++ and the program looks a bit different and the array is a char array instead of a string array.
    A string is effectively a char array, so you need a string for your DNA sequence.

    In your code:
    Code:
    namespace bioinformaticsCal
    {
    
        public partial class Form1 : Form
        {
            int numberOfCodons;
            int rnaLength;
            int beginIndex;
            int endIndex;
            int i;
            int j;
            int k;
            string[] codons = { "UUU", "UUC", "UUA", "UUG", "UCU", "UCC", "UCA", "UCG", "UAU", "UAC", "UAA", "UAG", "UGU", "UGC", "UGA", "UGG", "CUU", "CUC", "CUA", "CUG", "CCU", "CCC", "CCA", "CCG", "CAU", "CAC", "CAA", "CAG", "CGU", "CGC", "CGA", "CGG", "AUU", "AUC", "AUA", "AUG", "ACU", "ACC", "ACA", "ACG", "AAU", "AAC", "AAA", "AAG", "AGU", "AGC", "AGA", "AGG", "GUU", "GUC", "GUA", "GUG", "GCU", "GCC", "GCA", "GCG", "GAU", "GAC", "GAA", "GAG", "GGU", "GGC", "GGA", "GGG" };
            string[] aminoAcids = { "Phenylalanine", "Phenylalanine", "Leucine", "Leucine", "Serine", "Serine", "Serine", "Serine", "Tyrosine", "Tyrosine", "Stop", "Stop", "Cysteine", "Cysteine", "Stop", "Tryptophan", "Leucine", "Leucine", "Leucine", "Leucine", "Proline", "Proline", "Proline", "Proline", "Histidine", "Histidine", "Glutamine", "Glutamine", "Arginine", "Arginine", "Arginine", "Arginine", "Isoleucine", "Isoleucine", "Isoleucine", "Methionine", "Threonine", "Threonine", "Threonine", "Threonine", "Asparagine", "Asparagine", "Lysine", "Lysine", "Serineine", "Serineine", "Arginine", "Arginine", "Valine", "Valine", "Valine", "Valine", "Alanine", "Alanine", "Alanine", "Alanine", "Aspartate", "Aspartate", "Glutamate", "Glutamate", "Glycine", "Glycine", "Glycine", "Glycine" };
            string codon = "";
            string protein = "";
            string [] DNA;
            
            string RNA;
            string SEQ;
            int len;
           ...
    You are declaring 'DNA' to be an array of strings, but you are not assigning any strings to this array. So when you come to reference them:
    Code:
    	if (DNA[i]=="A")
    DNA[I] will be null.

    But you are reading your text file into the SEQ variable anyway - so why aren't you comparing SEQ with the codons?

  3. #3
    Join Date
    Oct 2012
    Posts
    14

    Re: trying to solve a NullReference not handled error(array trouble)

    If the sequence is DNA I have to convert it to RNA first then I guess i compare then. Im not sure. Ill tweek it some more when I get back Home

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center