您的位置:首页 > 编程语言 > Java开发

解决java的Long.getLong(str)的NullPointerException错误

2013-04-26 09:49 204 查看
问题:longtemp=Long.getLong("1366937075");出现空指针NullPointerException错误

解决:用Long.parseLong(Stringstr)
替换。[/code]
疑问:Longtmp=Long.getLong("1366937075");这个返回也为null,为什么?


参考:http://stackoverflow.com/questions/7376857/long-getlong-failing-returning-null-to-valid-string



tackOverflowisaquestionandanswersiteforprofessionalandenthusiastprogrammers.It's100%free,noregistrationrequired.

Long.getLong()failing,returningnulltovalidstring



up
vote2down
votefavorite1
I've
spentthepasttwohoursdebuggingwhatseemsextremelyunlikely.I'vestrippedthemethodofasecondaryAndroidActivitytoexactlythis:[code]publicvoidonClick(Viewv){
Stringstr="25";
longmy_long=Long.getLong(str);
}//onClick(v)And
yeah,Igetacrashwiththegoodol'NullPointerException:09-11
02:02:50.444:ERROR/AndroidRuntime(1588):Uncaughthandler:threadmainexitingduetouncaughtexception09-1102:02:50.464:ERROR/AndroidRuntime(1588):java.lang.NullPointerExceptionIt
lookslike(fromothertests)thatLong.getLong(str)returnsNULL,whichisdrivingmebonkers.WHATAMIMISSING?Thanks
inadvance.I'mokaywithstupidlymissingtheobvious,butmysanityisontheline.

androidstringnullpointerexceptionlong-integerstring-parsing
share|improve
thisquestion
askedSep
11'11at7:17

Scott
Biggs
503617
1
use
Long.parseLong(str);insteadofLong.getLong(str);–user370305Sep
11'11at7:31

4Answers

activeoldestvotes
up
vote12down
voteaccepted
You
aremissingthefactthat
Long.getLong(String
str)
isnotsupposedtoparseaStringtoalong,butrathertoreturnalongvalueofasystem
property
representedbythatstring.Asothershavesuggested,whatyouactuallyneedis
Long.parseLong(String
str)
.
share|improve
thisanswer
editedSep
11'11at18:36
answeredSep
11'11at8:28

MeLight
1,284820
Ahh,
thatmakesperfectsense.Thanks!–Scott
BiggsSep
11'11at16:57
Very
descriptivereply.+1forthat.–Android
KillerSep
11'11at17:30
Thanks
andwelcome:)–MeLightSep
11'11at18:35


up
vote3down
vote
I
thinkyouareusingwrongfunctionuseLong.parseLong(str)then
youcangettherightanswer.
share|improve
thisanswer
answeredSep
11'11at7:24

Android
Killer
4,41921035
You
areabsolutelyright.Thanksforthehelp!–Scott
BiggsSep
11'11at18:52
My
pleasuretohelpubuddy.–Android
KillerSep
12'11at2:26
up
vote3down
vote
you
canuse
Long.parsLong(String)
instead
of
getLong(String)
it
willsolvetheproblem
share|improve
thisanswer
editedSep
11'11at7:30
answeredSep
11'11at7:20

confucius
4,3663729
Thanks,
thechangeworked!–Scott
BiggsSep
11'11at18:52
up
vote3down
vote
Long.parseLong(someString)
approved.Don'tforgettocatchNumberFormatExceptionifthere'saprobabilityofunparsablestring.
share|improve
thisanswer
answeredSep
11'11at7:53

Y.A.P.
24716
Yes,
thankyou!Wasn'tthinkingalongthoselines.–Scott
BiggsSep
11'11at16:58
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐