0

I know this question ask many time here. Actually i also refer some of them but not able to get solution so decided to post here

I am simply trying to parse string value to integer at that time it gives Null pointer exception. When i am trying to print value of string i get 5 which is correct but when i print their value after parse it is 0. Following the code which i have tried

String xmldata = "";
int sql_proID;
xmldata = os.toString();
sql_proID = Integer.parseInt(xmldata);

I also tried by declaring sql_proID as long but it gives same error. following is exception message

01-04 17:06:14.166: W/System.err(398): java.lang.NumberFormatException: unable to parse  '
01-04 17:06:14.166: W/System.err(398): 5' as integer
01-04 17:06:14.166: W/System.err(398):  atjava.lang.Integer.parse(Integer.java:383)
01-04 17:06:14.176: W/System.err(398):  at java.lang.Integer.parseInt(Integer.java:372)
01-04 17:06:14.176: W/System.err(398):  at java.lang.Integer.parseInt(Integer.java:332)
01-04 17:06:14.176: W/System.err(398):  at com.example.anti_dui_app.Synchronization.onCreate(Synchronization.java:57)
01-04 17:06:14.176: W/System.err(398):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-04 17:06:14.176: W/System.err(398):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-04 17:06:14.176: W/System.err(398):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-04 17:06:14.176: W/System.err(398):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-04 17:06:14.176: W/System.err(398):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-04 17:06:14.176: W/System.err(398):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 17:06:14.185: W/System.err(398):  at android.os.Looper.loop(Looper.java:123)
01-04 17:06:14.185: W/System.err(398):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-04 17:06:14.185: W/System.err(398):  at java.lang.reflect.Method.invokeNative(Native Method)
01-04 17:06:14.185: W/System.err(398):  at java.lang.reflect.Method.invoke(Method.java:507)
01-04 17:06:14.185: W/System.err(398):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-04 17:06:14.185: W/System.err(398):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-04 17:06:14.185: W/System.err(398):  at dalvik.system.NativeStart.main(Native Method)

where i goes wrong please tell me...thank you

4 Answers 4

6

Looks like there may be a newline char in there.

Have you tried trimming the string to remove whitespace?

e.g:

sql_proID=Integer.parseInt(xmldata.trim());

Sign up to request clarification or add additional context in comments.

Comments

5

It looks like you are getting five with single quotes, which is the reason for NullPointerException.

Please also check that there is a new line existing as showing in logcat.

Comments

4

It is a NumberFormatException not a NullPointerException. It is caused by an ill-formated number string that seems to be "\n5" instead of just "5".

1 Comment

@karthi it was an independent answer given at the same time (just 24 seconds later if you look at the time stamp)
0

Your code is not clear and up to the mark as:

String **xmldata**="";
int sql_proID;
xmldata = os.toString();
sql_proID=Integer.parseInt(xmldata);

Here you are parsing xmldata which is having value '5'. It is not understandable for parser. So try to get value as only 5.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.