-
October 15th, 2012, 02:45 PM
#1
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++;
}
}
-
October 15th, 2012, 03:21 PM
#2
Re: trying to solve a NullReference not handled error(array trouble)
Originally Posted by ApacheOmega
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: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?
-
October 15th, 2012, 03:37 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|