C
C#3mo ago
yummy_bee

✅ null reference

How do I fix possible null reference return do I add = null! after the errormessage or why does that come up as a warning? ```cs using System; using System.IO; namespace Calculator.View { public class CalculatorView { public string ErrorMessage { get; set; } public double ResultString { get; set; } public string InputString { get; set; } public string GetRPNString() { Console.WriteLine(); return Console.ReadLine(); } public void DisplayResult(double result) { Console.WriteLine("Result: " + result); } public void DisplayError(string errorMessage) { Console.WriteLine("Error: " + errorMessage); } public string ReadInputFromFile(string filePath) { using (StreamReader reader = new StreamReader(filePath)) { return reader.ReadLine(); } } public void WriteOutputToFile(string filePath, double result) { try { using (StreamWriter writer = new StreamWriter(filePath, false)) { writer.WriteLine("Result: " + result); } Console.WriteLine($"Wrote result to file: {filePath}"); } catch (Exception ex) { Console.WriteLine($"Error writing to file: {ex.Message}"); } } } } ```
19 Replies
canton7
canton73mo ago
$code
MODiX
MODiX3mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
canton7
canton73mo ago
And please post the error message (including line numbers etc).
yummy_bee
yummy_beeOP3mo ago
No description
canton7
canton73mo ago
(and please format your code as C#, by editing your previous messages and surrounding your code with backticks, see the message above) To be honest, if you're just starting out, I'd probably recommend turning off the nullable reference types warnings. Edit your .csproj, and change <Nullable>enable</Nullable> to <Nullable>disable</Nullable> $codegif
yummy_bee
yummy_beeOP3mo ago
I am trying but it’s not working Okay but I can’t have any warnings when I turn it in
Angius
Angius3mo ago
One way would be to make your properties required The error happens because the properties might be uninitialized when you access them
yummy_bee
yummy_beeOP3mo ago
Yes okay so do you mean by doing InputString = inputstring and the same with the other ones inside the calculatorview class?
canton7
canton73mo ago
Did you do this?
yummy_bee
yummy_beeOP3mo ago
No I did not but I want to be able to see the warnings that my teacher will get as well
Angius
Angius3mo ago
They will have to be set in the constructor specifically Or set to default values Or made required
canton7
canton73mo ago
Huh? I'm assuming your teacher is going to be opening your solution? Or are they going to be copy/pasting code code into their own solution?
Angius
Angius3mo ago
Warning:
class Foo
{
public string Bar { get; set; }
}
class Foo
{
public string Bar { get; set; }
}
Good:
class Foo
{
public string Bar { get; set; } = "";
}
class Foo
{
public string Bar { get; set; } = "";
}
Good:
class Foo
{
public string Bar { get; set; } = default;
}
class Foo
{
public string Bar { get; set; } = default;
}
Bad, but fixes the warning:
class Foo
{
public string Bar { get; set; } = null!;
}
class Foo
{
public string Bar { get; set; } = null!;
}
Good:
class Foo
{
public required string Bar { get; set; }
}
class Foo
{
public required string Bar { get; set; }
}
Good:
class Foo
{
public string Bar { get; set; }
public Foo(string bar)
{
Bar = bar;
}
}
class Foo
{
public string Bar { get; set; }
public Foo(string bar)
{
Bar = bar;
}
}
Good:
class Foo
{
public string Bar { get; set; }
public Foo()
{
Bar = "";
}
}
class Foo
{
public string Bar { get; set; }
public Foo()
{
Bar = "";
}
}
Good:
class Foo(string bar)
{
public string Bar { get; set; } = bar;
}
class Foo(string bar)
{
public string Bar { get; set; } = bar;
}
yummy_bee
yummy_beeOP3mo ago
Ohh okay I see what you mean using System; using System.IO; namespace Calculator.View { public class CalculatorView { public string ErrorMessage { get; set; } public double ResultString { get; set; } public string InputString { get; set; } public CalculatorView(string errormessage, string inputstring) { ErrorMessage = errormessage; InputMessage = inputmessage; } public string GetRPNString() { Console.WriteLine(); return Console.ReadLine(); } public void DisplayResult(double result) { Console.WriteLine("Result: " + result); } public void DisplayError(string errorMessage) { Console.WriteLine("Error: " + errorMessage); } public string ReadInputFromFile(string filePath) { using (StreamReader reader = new StreamReader(filePath)) { return reader.ReadLine(); } } public void WriteOutputToFile(string filePath, double result) { try { using (StreamWriter writer = new StreamWriter(filePath, false)) { writer.WriteLine("Result: " + result); } Console.WriteLine($"Wrote result to file: {filePath}"); } catch (Exception ex) { Console.WriteLine($"Error writing to file: {ex.Message}"); } } } } ``` I added that now as it got rid of two warnings but there are still two warnings in GrtRPNString and ReadFromInput It’s the return Console.ReadLine() that has a possible null reference return
canton7
canton73mo ago
You need the ```cs at the start of your code
Angius
Angius3mo ago
Yep, ReadLine() returns string? So a possible null You'll have to handle that, for example with a null-coalescing operator
string s = Console.ReadLine() ?? "";
string s = Console.ReadLine() ?? "";
yummy_bee
yummy_beeOP3mo ago
Hmm yes okay Can I not just add a ? after public string? Cuz that removes the warnings It seems to work now I just need to make sure that I am writing to a file correctly Actually I ran into another problem when writing to a file it only writes the latest value so do I need like a loop to fix that? !close
Accord
Accord3mo ago
Closed!
Want results from more Discord servers?
Add your server