Forum Xamarin Xamarin.Forms

Xamarin Forms "Value cannot be null parameter:stream" on StreamReader

I've been trying to utilize a file that I have on my PC in Xamarin.Forms and after some error fixing and researching I learnt that you cannot reference it locally but have to embed it into Xamarin as a resource. I've done so by accessing the file with Stream and now I would like to convert that Stream into a string using StreamReader. Now I get a Value cannot be null parameter:stream error.

namespace AimlTest
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(true)]
    public partial class MainPage : ContentPage
    {
        public SimlBot chatbot;
        public MainPage()
        {
            InitializeComponent();

            chatbot = new SimlBot();
            var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MainPage)).Assembly;
            Stream stream = assembly.GetManifestResourceStream("AimlTest.Knowledge.simplk");
            StreamReader reader = new StreamReader(stream);
            string file = reader.ReadToEnd();

            var packageString = File.ReadAllText(file);
            chatbot.PackageManager.LoadFromString(packageString);
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            var result = chatbot.Chat(UserEntry.Text);
            BotEntry.Text = string.Format("User: {0}\nBot: {1}\n{2}", UserEntry.Text, result.BotMessage, BotEntry.Text);
            UserEntry.Text = string.Empty;

        }
    }
}

And yes, I have set my file to an EmbeddedResouce
At this point, I believe I may have a bug.

Best Answer

Answers

  • BobWhittenBobWhitten USMember ✭✭

    I think the issue is not with StreamReader, but with the line above,
    Stream stream = assembly.GetManifestResourceStream("AimlTest.Knowledge.simplk");

    I'm not familiar with the call you're using, but it may not be that the file you're looking for is in a place that call can find it, so returns null.

  • jojo1234jojo1234 Member ✭✭
    > @BobWhitten said:
    > I think the issue is not with StreamReader, but with the line above,
    > Stream stream = assembly.GetManifestResourceStream("AimlTest.Knowledge.simplk");
    >
    > I'm not familiar with the call you're using, but it may not be that the file you're looking for is in a place that call can find it, so returns null.

    So what would be the better call to use?
  • BobWhittenBobWhitten USMember ✭✭

    I messed around with a test app, which as a folder "Assets". I added a text file to that, then, read with File.ReadAllText():

                var fname = "Assets/TextFile.txt";
                MyLabel.Text = File.ReadAllText(fname);
    

    I see that you are already using that call... so maybe it's just a matter of where the file is located.

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    I cannot reproduce this issue following your code sample.

    My guess is that assembly.GetManifestResourceStream("AimlTest.Knowledge.simplk"); is returning null for some reason, most likely you are not matching the exact file name, including the extension, of the embedded resource, or you have it in a folder and you did not add the folder name.

    For example, if I have a text file named "textresource.txt" and it is in my .NET Standard project, that uses default namespace of MyApp, in the root folder with build action of EmbeddedResource, then I can access it with assembly.GetManifestResourceStream("MyApp.textresource.txt");. However if the file is in a folder, e.g. a folder named "EmbeddedResources", then I need to add that to the resource name, e.g. assembly.GetManifestResourceStream("MyApp.EmbeddedResources.textresource.txt");.

    In addition, it is a good idea to wrap StreamReader instances in a using block, e.g.:

    string text = "";
    using (StreamReader reader = new System.IO.StreamReader(stream))
    {   
        text = reader.ReadToEnd();
    }
    

    The above is so the stream will be properly disposed when you are done with it.

    Also this is odd:

    var packageString = File.ReadAllText(file);
    

    Unless of course the embedded resource only contained the string for a file path you want to use to read a text file on the system. Not sure what you are really trying to do here.

  • jojo1234jojo1234 Member ✭✭

    At this point I've tried everything and i honestly cant find any answer. I dont know if this is the right way to ask but I've put my project in a rar, if anyone is willing to check it out to see if it is a problem on my system or not. For what I'm trying to do, I'm trying to build a simple chatbot and I've found a platform called SimlBot to do so, I've been following their tutorials and this is where it led me.
    So anyways, the project: https://www51.zippyshare.com/v/dsq2U89O/file.html

  • jojo1234jojo1234 Member ✭✭

    Thank you for sparing the time, so I removed the var packageString = File.ReadAllText(file); and replaced the chatbot.PackageManager.LoadFromString(file); and everything ended up working! I guess the packageString was the only problem. For the responses thats exactly how I programmed it, to say "Goodbye" only when "Hi" is typed in and give that fallback when anything else is input. Again thanks

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    Haha. Funny, I only tried "Hi" because I figured a chatbot ought to know how to answer that at the very least. :smile:

Sign In or Register to comment.