【问题标题】:null values on integers and strings but they are already assigned整数和字符串上的空值,但它们已被分配
【发布时间】:2013-07-02 16:46:55
【问题描述】:

我遇到了一个小问题,VS 给了我 2 个关于两个值为 null 的警告(int[] 和 string[]),因为它们从未被分配,但我确定我分配了它们,这是代码的一部分这与我的问题有关:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using System.IO;
namespace DA_Story_Editor
{
public partial class Form1 : ComponentFactory.Krypton.Toolkit.KryptonForm
{
    public Form1()
    {
        InitializeComponent();
    }
    int[] pntrs;
    string[] Strs;

    private void ReadFile(string path1)
    {

        FileStream stream = new FileStream(path1, FileMode.Open, FileAccess.Read);

        for (int i = 0; i < Pntrnum; i++)
        {
            stream.Position = Pntrstrt;
            stream.Read(data, 0, data.Length);
            pntrs[i] = BitConverter.ToInt32(data, 0);
        }
        for (int i = 0; i < Pntrnum; i++)
        {
            byte[] sttrings = new byte[pntrs[i + 1] - pntrs[i]];
            stream.Position = pntrs[i];
            stream.Read(sttrings, 0, sttrings.Length);
            Strs[i] = Encoding.GetEncoding("SHIFT-JIS").GetString(sttrings);

            ListViewItem item = new ListViewItem(new string[]
                {
                    pntrs[i].ToString("X"),
                    Strs[i],
                    Strs[i],
                });
            listView1.Items.AddRange(new ListViewItem[] {item});
        }
    }
}
}

我不确定它为什么会给我这些警告,顺便说一句,它还给了我一个“NullReferenceException”就行了:pntrs[i] = BitConverter.ToInt32(data, 0);

【问题讨论】:

    标签: c# winforms krypton-toolkit


    【解决方案1】:

    您永远不会构造和分配 变量本身 - 但您尝试使用数组:

    // Need to add:
    pntrs = new int[Pntrnum];
    
    for (int i = 0; i < Pntrnum; i++)
    {
        stream.Position = Pntrstrt;
        stream.Read(data, 0, data.Length);
        pntrs[i] = BitConverter.ToInt32(data, 0);
    }
    
    // Need to add:
    Strs = new string[Pntrnum];
    
    for (int i = 0; i < Pntrnum; i++)
    {
        byte[] sttrings = new byte[pntrs[i + 1] - pntrs[i]];
        stream.Position = pntrs[i];
        stream.Read(sttrings, 0, sttrings.Length);
        Strs[i] = Encoding.GetEncoding("SHIFT-JIS").GetString(sttrings);
    

    【讨论】:

      猜你喜欢
      • 2022-10-15
      • 2017-01-26
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 2013-01-10
      • 2020-07-02
      • 1970-01-01
      相关资源
      最近更新 更多