Java.NullPointerException null(再次) [英] Java.NullPointerException null (again)
问题描述
我遇到了 NullPointerException 的另一个问题.这次它突出显示了tellen = telno.length()
.比你问:
I am having another problem with NullPointerException. This time it is highlighting tellen = telno.length()
. Beore you ask:
- 文本文件中有要读取到该变量的数据.
- 所有变量都已初始化.
- 缓冲读取器也已初始化.
这是我的代码片段:
- There is data in the text file to be read to that variable.
- All variables were initialized.
- Buffered Reader is also initialized.
Here is a snippet of my code:
while((telno = br.readLine()) != null){
name = br.readLine();
surname = br.readLine();
company = br.readLine();
house = br.readLine();
street = br.readLine();
locality = br.readLine();
postcode = br.readLine();
telno = br.readLine();
tellen = telno.length();
mobno = br.readLine();
sep = br.readLine();
if(tellenx > tellen) tellen = tellenx;
}
请帮忙.谢谢.文本文件:
Please help. Thanks. Text file:
- 姓名
- 姓氏
- 公司
- 房子
- 街道
- 地区
- 电话号码
- 手机号码
问题出在电话上(tellen)所有这些名字都是虚构的,这个程序是一个电话簿
Problem is in the telephone (tellen) All these names are fictional, and this program is a telephone directory
George
Farrugia
Some Company
9, Flowers
Quail Street
Bubaqra
BBQ 1569
21369854
79825643
--------------------------------------------
Paul
Zammit
9
Bird Street
St. Julians
STJ 0000
21545796
79745247
--------------------------------------------
Peter
Spiteri
Oak Company
Trees
Birch Street
Oakgrove
TRE 3333
21323323
99323323
--------------------------------------------
Zammit
后面的空格是一个空格.如果没有数据就放置,以避免出现这样的问题.
The blank after Zammit
is a space. That is placed if there is no data to avoid a problem like this.
推荐答案
这个错误是因为你在一个 while loop
中读取 11
行而不是你必须仅读取 10
行.
This error is getting caused because you are reading 11
lines in one while loop
instead you have to read 10
lines only.
所以在某些时候 br.readLine()
将返回 null
.
So at some point br.readLine()
will return null
.
您需要根据需要读取文件,这意味着一次性(您的 while 循环)读取 10 行,然后是接下来的 10 行,依此类推.
You need to read file according to your need that means in one go (your while loop) read 10 lines, then next 10 lines and so on.
while((telno = br.readLine()) != null){ // first line
name = br.readLine(); // secondline I think this should be first line
surname = br.readLine();
company = br.readLine();
house = br.readLine();
street = br.readLine();
locality = br.readLine();
postcode = br.readLine();
telno = br.readLine();
tellen = telno.length();
mobno = br.readLine();
sep = br.readLine(); // eleventh line
if(tellenx > tellen) tellen = tellenx;
}
这篇关于Java.NullPointerException null(再次)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!