写一个电话本程序,只存取姓名和号码
写好后,编译可以通过,但当执行这个功能函数时,却遇到一点麻烦
这是整个程序清单:
调用这个输出清单时,可以完成功能,但是后面还出现了这几行提示错误
import java.io.*;
import java.lang.String;
class IndexIterm
{
String name;
String TelNum;
void print()
{
System.out.println(name+" "+TelNum);
}
}
class TelNumList
{
IndexIterm[] tel=new IndexIterm[50];
void add()throws IOException
{
for(int i=0;i<50;i++)
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入电话号码信息");
System.out.print("姓名:");
String Jname = br.readLine();
tel[i]=new IndexIterm();
tel[i].name = Jname;
System.out.print("号码:");
String JTelNum = br.readLine();
tel[i].TelNum = JTelNum;
System.out.print("继续输入吗?(y/n):");
String c = br.readLine();
if(c.equals("y"))
continue;
else if(c.equals("n"))
{
System.out.println("输入成功!请继续使用其他功能!");
return;
}
else
{
System.out.println("必须输入'y'或者'n',请重新输入:");
}
}
}
void IndexOf()throws IOException
{
System.out.print("请输入要查询的姓名:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = br.readLine();
for(int i=0;i<50;i++)
{
if(name.equals(tel[i].name))
{
int r=i+1;
System.out.println("查询结果:");
System.out.println("这是第"+ r +"项信息:"+tel[i].name+" "+tel[i].TelNum);
}
}
}
void IndexOfs()throws IOException
{
System.out.print("请输入要查询的号码:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String Num = br.readLine();
for(int i=0;i<50;i++)
{
if(Num.equals(tel[i].TelNum))
{
int r=i+1;
System.out.println("查询结果:");
System.out.println("这是第"+ r +"项信息:"+tel[i].name+" "+tel[i].TelNum);
}
}
}
void printList()//throws IOException
{
System.out.println("以下是所有号码信息的清单:");
if(tel[0].name.equals(""))
System.out.println("数据库里暂时没有信息,请按'8'输入信息!");
else
for(int i=0;i<50;i++)
{
if(!tel[i].name.equals(""))
{
tel[i].print();
}
else
return;
}
}
void remove()throws IOException
{
System.out.print("请输入要删除的项:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int i = Integer.parseInt(br.readLine());
System.out.println("正在处理,请稍后...");
for(int j=i-1;j<50-1;j++)
{
tel[j]=tel[j+1];
}
System.out.println("删除成功!");
}
void SortOfName()throws IOException
{
for(int i=0;i<50-1;i++)
{
int q = i;
for(int j=i+1;j<50;j++)
{
if(!tel[j].name.equals(""))
{
if(tel[q].name.compareTo(tel[j].name)<0)
{
q=j;
}
}
else
{
break;
}
}
if(q!=i)
{
String Lname;
Lname=tel[q].name;
tel[q].name=tel[i].name;
tel[i].name=Lname;
}
}
System.out.println("已排序完毕!");
}
void SortOfNum()throws IOException
{
for(int i=0;i<50-1;i++)
{
int q = i;
for(int j=i+1;j<50;j++)
{
if(!tel[j].TelNum.equals(""))
{
if(tel[q].TelNum.compareTo(tel[j].TelNum)<0)
{
q=j;
}
}
else
{
break;
}
}
if(q!=i)
{
String LNum;
LNum=tel[q].TelNum;
tel[q].TelNum=tel[i].TelNum;
tel[i].TelNum=LNum;
}
}
System.out.println("已排序完毕!");
}
void pringf()throws IOException
{
System.out.print("请输入要查询的序号:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int i =Integer.parseInt(br.readLine());
if(!tel[0].name.equals(""))
tel[i-1].print();
else
return;
}
void program()
{
System.out.println(" "+"******************************");
System.out.println(" "+" "+"本程序由李跃制作");
System.out.println(" "+" "+"Emal:daxia841@163.com");
System.out.println(" "+" "+"欢迎交流");
System.out.println(" "+"******************************");
}
int gotos()throws IOException
{
System.out.print("请选择:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(br.readLine());
return a;
};
}
public class TelBook
{
public static void main(String[] args)throws IOException
{
TelNumList Q = new TelNumList();
System.out.println("*******************************");
System.out.println(" "+"按1:按姓名查找");
System.out.println(" "+"按2:按号码查找");
System.out.println(" "+"按3:删除号码项");
System.out.println(" "+"按4:查找指定位置的号码");
System.out.println(" "+"按5:按姓名排序");
System.out.println(" "+"按6:按号码排序");
System.out.println(" "+"按7:输出所有号码信息");
System.out.println(" "+"按8:添加新号码信息");
System.out.println(" "+"按9:显示本程序信息");
System.out.println(" "+"按0:退出本程序");
System.out.println("*******************************");
System.out.println();
int b=Q.gotos();
while(true)
{
switch(b)
{
case 1:
Q.IndexOf();
b=Q.gotos();
break;
case 2:
Q.IndexOfs();
b=Q.gotos();
break;
case 3:
Q.remove();
b=Q.gotos();
break;
case 4:
Q.pringf();
b=Q.gotos();
break;
case 5:
Q.SortOfName();
b=Q.gotos();
break;
case 6:
Q.SortOfNum();
b=Q.gotos();
break;
case 7:
Q.printList();
b=Q.gotos();
break;
case 8:
Q.add();
b=Q.gotos();
break;
case 9:
Q.program();
b=Q.gotos();
break;
case 0:
System.out.println("谢谢使用");
return;
}
}
}
}
比如说使用排序功能调用SortOfName();时,会出现这个提示
Exception in thread"main" java.lang.NullPointerException
at TelNumList.printList<TelBook.java:105>
at TelBook.main<Tel.Book.java:287>
在打印清单时,调用pringf();时也会出现同样的提示
请问这是什么问题
帮我下
谢谢~`
[此贴子已经被作者于2006-10-19 19:33:07编辑过]