问题: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-parsingshare|improve thisquestion | askedSep 11'11at7:17
Scott Biggs 503617 |
|
| | use Long.parseLong(str);insteadofLong.getLong(str);–user370305Sep 11'11at7:31 |
|
4Answers
activeoldestvotesup vote12down voteaccepted | You aremissingthefactthatLong.getLong(String
str) isnotsupposedtoparseaStringtoalong,butrathertoreturnalongvalueofasystem propertyrepresentedbythatstring.Asothershavesuggested,whatyouactuallyneedisLong.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 canuseLong.parsLong(String) instead ofgetLong(String) it willsolvetheproblemshare|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 |
|