2

Please excuse me, i appreciate this is somewhat elementary.

XAML.CS

Double D;
D = Convert.ToInt32(Diameter.Text);
D = int.Parse(Diameter.Text);

Double R;
R = (D / 2);

Double A;
A = (R * R * (Math.PI));

Double M;
M = Convert.ToInt32(Mass.Text);
M = int.Parse(Mass.Text);

float g;
g = 9.81f;

float P;
P = Convert.ToInt32(Pressure.Text);
P = int.Parse(Pressure.Text);

float Ol1;
Ol1 = Convert.ToInt32(OilLevel.Text);
Ol1 = int.Parse(OilLevel.Text);

Double V1;
V1 = ((Math.PI) * R * R * Ol1);

float K1;
K1 = Convert.ToInt32(SpringRate.Text);
K1 = int.Parse(SpringRate.Text);

Double Pr1;
Pr1 = Convert.ToInt32(InitialPreload.Text);
Pr1 = Convert.ToInt32(Preload.Text);
Pr1 = (Convert.ToInt32(InitialPreload.Text) + Convert.ToInt32(Preload.Text));

InitializeComponent();

Line two (D = Convert.ToInt32(Diameter.Text)) throws the error.

Below is the xaml code which holds the text box's

XAML

<StackPanel>
    <TextBlock Margin = "10 10" Text="Spring Force Calculator" HorizontalAlignment="Center" FontWeight="Bold" FontSize="22"/>
    <TextBlock Margin = "10 10" Text="Mass - Kg" FontSize="18"/>

    <TextBox x:Name="Mass" Margin="10 10" Text="165"/>
    <TextBlock Margin = "10 10" Text="Initial Pressure - Bar" FontSize="18"/>

    <TextBox x:Name="Pressure" Margin="10 10" Text="1"/>
    <TextBlock Margin = "10 10" Text="Internal Fork Diameter - mm" FontSize="18"/>

    <TextBox x:Name="Diameter" Margin="10 10" Text="46"/>
    <TextBlock Margin = "10 10" Text="Internal Fork Area - mm²" FontSize="18"/>

    <TextBox x:Name="Area" Margin="10 10"/>
    <TextBlock Margin = "10 10" Text="Spring Rate - N/m" FontSize="18"/>

    <TextBox x:Name="SpringRate" Margin="10 10" Text="9"/>
    <TextBlock Margin = "10 10" Text="Installed Preload - mm" FontSize="18"/>

    <TextBox x:Name="InitialPreload" Margin="10 10" Text="10"/>
    <TextBlock Margin = "10 10" Text="Preload - mm" FontSize="18"/>

    <TextBox x:Name="Preload" Margin="10 10" Text="6"/>
    <TextBlock Margin = "10 10" Text="Oil Level - mm" FontSize="18"/>

    <TextBox x:Name="OilLevel" Margin="10 10" Text=".133"/>
    <TextBlock Margin = "10 10" Text="Maximum Allowable Travel - mm" FontSize="18"/>

    <TextBox x:Name="MaxATravel" Margin="10 10" Text="115"/>
    <TextBlock Margin = "10 10" Text="Maximum Calculated Travel - mm" FontSize="18"/>

    <TextBox x:Name="MaxCTravel" Margin="10 10"/>
</StackPanel>

I know i must be missing something basic and rather obvious to the trained eye. I should note that this is my first attempt at programming and trying to learn as i build (maybe not the best idea).

Sorry again for the beginner question, please help!

Thanks Jord

Petter Friberg
  • 19,652
  • 9
  • 51
  • 94
  • 1
    Welcome to SO. Which line throws the null reference exception? Furthermore, please provide us with some more information. What is this textbox? Is this a textbox in a windows form application, a WPF, an ASP.NET web forms? Where exactly this code is executed, show the full code of the method that this code leaves in. Why do you use both `Convert.ToInt32` and `int.Parse` and not one of them? Thanks – Christos Jul 01 '17 at 18:27
  • Sorry, the second line throws the exception. The textbox is in a WPF. Here's a pastebin link to my code (if you can call it that!) https://pastebin.com/Aj5wUv5X - The specific error reads as follows: System.NullReferenceException: 'Object reference not set to an instance of an object.' –  Jul 01 '17 at 18:44
  • Not a problem at all ! Please include your code in your post and not provide it through links, in order to be visible by the readers of your post. As it is, someone should run through the comments, read them, click the link... – Christos Jul 01 '17 at 18:56
  • Certainly, i have now updated my post with my code. –  Jul 01 '17 at 19:18
  • please do not deface your post – Petter Friberg Oct 03 '17 at 12:34

1 Answers1

1

The problem is that your component has not been initialized and you access the controls that reside in this component, prior to it's initialization. The solution is to bring this line:

InitializeComponent();

as the first line in the MainWindow method. As a side note, as I have already done, I don't see any reason why you use both Convert.ToInt32 and Int.Parse. The parsing should be done only once. Furthermore, I would suggest you look for int.TryParse method, since this way you avoid any exceptions during the parsing. You could start from here.

Christos
  • 50,311
  • 8
  • 62
  • 97