2

im getting the Object reference not set to an instance of an object for line 43 and i cant figure out why, i have searched the web and cant seem to find the answer. i am new to C# and programming in general and trying to learn. if some one can help me out it would be great

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace test
{
    public partial class Form1 : Form
    {
        [Serializable]
        public class ore 
        {
            public float Titan;
            public float Eperton;
        }

        ore b1 = null;
        ore b2 = null;

        public Form1()
        {
            InitializeComponent();

            ore b2 = new ore();
            ore b1 = new ore();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            float tempFloat;

            if (float.TryParse(textBox1.Text, out tempFloat))
            {
                b1.Titan = tempFloat; //line 43; where error happens
            }
            else
                MessageBox.Show("uh oh");
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            float tempFloat;
            if (float.TryParse(textBox1.Text, out tempFloat))
            {
                b2.Eperton = tempFloat;
            }
            else
                MessageBox.Show("uh oh");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<ore> oreData = new List<ore>();
            oreData.Add(b1);
            oreData.Add(b2);

            FileStream fs = new FileStream("ore.dat", FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs, oreData);
            fs.Close();
        }
    }
}
7
  • 1
    b1.Titan = tempFloat; is line 43 Commented Jun 26, 2011 at 23:11
  • This isn't the full code... It should work. Edit: Damn, missed that. Commented Jun 26, 2011 at 23:12
  • Vercas: it is the full code, see Rob's answer. The mistake is very subtle. Commented Jun 26, 2011 at 23:13
  • pastebin.com/y7xCACbn this is a pastebin link of the entire code just in case it didnt copy correctly Commented Jun 26, 2011 at 23:14
  • it compiles fine but when and number is added to the text box the error shows Commented Jun 26, 2011 at 23:14

3 Answers 3

6

I'm assuming it's failing on any of the b1/b2 references.

ore b1 = null;
ore b2 = null;

Here you're declaring two private variables for your class

ore b2 = new ore();
ore b1 = new ore();

Here you're declaring two local variables for that method call. You're not altering the original variables. Change it to:

b2 = new ore();
b1 = new ore();
Sign up to request clarification or add additional context in comments.

1 Comment

AWESOME okay that error is gone. i have a new issue however, the button is supposed to list ore and write it to a file called ore.dat in the programs directory. i ran the program and hit the save button BUT no new file in my project directory??
5

You never assign the field b1. The b1 you assign in the constructor is a local variable. Change the code in the constructor to this:

b2 = new ore();
b1 = new ore();

Comments

3

change your constructor to this:

public Form1()
    {
        InitializeComponent();

        b2 = new ore();
        b1 = new ore();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.