★★★★★ 小白又来请教了:未将对象引用设置到对象实例 ★★★★★

lanytin 2009-01-01 11:01:45
数据结构,使用栈链进行十进制、八进制转换。
编译没问题,运行也能得到正确结果,但运行时出现如题异常。
望高手指点……
class Program
{
static void Main(string[] args)
{
Console.Write("请输入一个大于0的整数\n-->:");
int m = Convert.ToInt32(Console.ReadLine());
Conversion(m);此处
}

//十进制转八进制
public static void Conversion(int n)
{
LinkStack<int> s = new LinkStack<int>();
while (n > 0)
{
s.Push(n % 8);
n = n / 8;
}

while (!s.IsEmpty())
{
n = s.Pop();//取出栈中的值,此处
Console.WriteLine("{0}",n);
}
}
...全文
487 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
youlitai0116 2009-01-01
  • 打赏
  • 举报
回复
设断点 追踪下 看看,保证是有一个对象的值为null.编译只检查了语法错误!
lanytin 2009-01-01
  • 打赏
  • 举报
回复
还有一个问题,为什么有时编译时没有问题,运行时会出现异常呢?
他们的过程有什么不同?
lanytin 2009-01-01
  • 打赏
  • 举报
回复
链栈类如下

namespace LinkedStack
{
public class LinkStack<T> : IStack<T>
{
private Node<T> top;//栈顶指示器
private int num;//栈中结点的个数,也就是元素的个数

public Node<T> Top
{
get
{
return top;
}
set
{
top = value;
}
}

public int Num
{
get
{
return num;
}
set
{
num = value;
}
}

//构造器
public LinkStack()
{
top = null;
num = 0;
}

#region IStack<T> 成员

public int GetLength()
{
return num;
}

public bool IsEmpty()
{
if ((Top == null) && (num == 0))
{
return true;
}
else
{
return false;
}
}

public void Clear()
{
top=null;
num = 0;
}

public void Push(T item)
{
Node<T> q = new Node<T>(item);

if (top == null)
{
top = q;
}
else
{
q.Next = top;
top = q;
}
}
//出栈
public T Pop()
{
if (IsEmpty())
{
Console.WriteLine("Stack is empty!");
return default(T);
}

Node<T> p = top;
top = top.Next;
--num;

return p.Data;
}
//取栈顶结点的值
public T GetTop()
{
if (IsEmpty())
{
Console.WriteLine("Stack is Empty!");
return default(T);
}
return top.Data;
}

#endregion
}
}

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧