java.lang.NumberFormatException (parse.Int) - android

An app published on market has an error:
java.lang.NumberFormatException
below the stack.
Is the cause this code?
fade = Integer.parseInt(ListPreference);
I see "OTHER" devices has affected by this crash. Any idea?
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.Notify}: java.lang.NumberFormatException: unable to parse '' as integer
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3768)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NumberFormatException: unable to parse '' as integer
at java.lang.Integer.parseInt(Integer.java:362)
at java.lang.Integer.parseInt(Integer.java:332)
at com.example.app.Notify.onCreate(Notify.java:33)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
... 11 more

java.lang.NumberFormatException: unable to parse '' as integer
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
==> it seems you are trying to parse an empty string. You should include the parsing operation in a try/catch block and deal with malformed inputs.

as in log :
NumberFormatException: unable to parse '' as integer
you are trying to parse space to integer . before parsing string value to Integer check for null or empty

Actually, There is any symbol or character happen in String ListPreference,
Its better to Handle this exception.
try {
fade = Integer.parseInt(ListPreference);
} catch (NumberFormatException e) {
Log.e("Exception",e.toString());
}
Update:
Look at the Jakarta Commons :
NumberUtils.isNumber()
This can be used to check most whether a given String is a number.
Also before using String ListPreference just removed white space from it using trim().
Using ListPreference.trim() and also check the length of ListPreference whether length is greater than 1 or not.
like, ListPreference.length() > 1

Related

Gridview onClickListerener nullpointer

The code: http://pastebin.com/5bhKULGd
the problem:
When you click an item in the gridview, OCLGenre (OnItemClickListener for Genre view)
fires an nullPointer at line 123. I'm trying to understand where the nullPointer comes from but can't find it.
The stack:
ERROR/AndroidRuntime(10511): FATAL EXCEPTION: main
java.lang.NullPointerException
at com.MMWeb.MMWeb$browse$1.onItemClick(MMWeb.java:123)
at android.widget.AdapterView.performItemClick(AdapterView.java:284)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Anyone got a pointer to where to find the source of this nullPointer?
If you try to access an object which is null. Like the below example.
ClassA objA;
At this time you have just declared this object but you have not initialized or instantiated it.
When you try to access any property or method in it, it will throw
NullPointerException.
Oke i found my problem By moving the ImageAdapter out of the "browse" class, the "browse" class did not have any reference to the ArrayList 'items'. I could find the problem because, after all, i defined the ArrayList at line 112. However, showGenres() uses a local ArrayList, and the ImageAdapter would put it in "browser.items". Fixed it by removing the local decleration of items on line 145, 154 and 162

nullPointerException when replacing char sequence

I developed a very simple android app and then I obfuscated the code. A nullPointerException is throwed when, probably, I replace a char sequence.
Here is my stack trace:
java.lang.NullPointerException
at com.xpandit.pemobile.android.loadactivity.ak.d(Unknown Source)
at com.xpandit.pemobile.android.loadactivity.g.onTextChanged(Unknown Source)
at android.widget.TextView.sendOnTextChanged(TextView.java:6335)
at android.widget.TextView.handleTextChanged(TextView.java:6376)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6520)
at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:583)
at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:174)
at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:120)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:257)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
As i said the code is obfuscated and by the mapping file I've found the right method (at least I think). The question is: In what situation can a replace method throw a null pointer?
Maybe the problem is not the replace method! Can anyone tell me where is the problem and how to resolve it? Thanks in advance...
EDIT:
I think the problem must be on that simple if instruction.
if (aux.contains(" ")){
aux = aux.replace(" ", "%20");
}
if this instruction throws NPE
if (aux.contains(" "))
So it's clear that aux is null, so you can not call contanis method on null.
Add some check on null state
if ( aux!=null && aux.contains(" "))
Use replaceAll() instead of replace()
if (aux.contains(" ")){
aux.replaceAll(" ", "%20");
}
Reference:
String replace(CharSequence target, CharSequence replacement)
Copies this string replacing occurrences of the specified target sequence with another sequence.
String replaceAll(String regularExpression, String replacement)
Replaces all matches for regularExpression within this string with the given replacement.
if (aux.contains(" ")){
throws an NPE if aux is null. It should be
if (anx != null && aux.contains(" ")){

Problem: "Extracting" JSON using lift-json in an Android (Scala) application

I want to deserialize a JSON-String using the lift-json library. In my Android application I'm using Scala 2.9.0 and lift-json_2.9.0_2.4-M1.
I took a simple example from lift-json readme, but everytime I try to extract values from the JSON-String I get a net.liftweb.json.MappingException when calling the Activity. It seems there are no args delivered to "extract".
Here is my Activity:
import _root_.android.app.Activity
import _root_.android.os.Bundle
import net.liftweb.json._
class JsonTest extends Activity {
override def onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.mainactivity)
implicit val formats = DefaultFormats
case class NumList(numbers: List[Int])
val json = parse(""" { "numbers" : [1, 2, 3, 4] } """)
json.extract[NumList]
}
}
and here the exception I get:
06-29 12:09:31.548: ERROR/AndroidRuntime(405): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.fhs.spirit/de.fhs.spirit.JsonTest}: net.liftweb.json.MappingException: Parsed JSON values do not match with class constructor
args=
arg types=
constructor=public de.fhs.spirit.JsonTest$NumList$2(de.fhs.spirit.JsonTest,scala.collection.immutable.List)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: net.liftweb.json.MappingException: Parsed JSON values do not match with class constructor
args=
arg types=
constructor=public de.fhs.spirit.JsonTest$NumList$2(de.fhs.spirit.JsonTest,scala.collection.immutable.List)
at net.liftweb.json.Meta$.fail(Meta.scala:185)
at net.liftweb.json.Extraction$.instantiate$1(Extraction.scala:257)
at net.liftweb.json.Extraction$.newInstance$1(Extraction.scala:280)
at net.liftweb.json.Extraction$.build$1(Extraction.scala:298)
at net.liftweb.json.Extraction$.extract0(Extraction.scala:345)
at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$extract0(Extraction.scala:194)
at net.liftweb.json.Extraction$.extract(Extraction.scala:42)
at net.liftweb.json.JsonAST$JValue.extract(JsonAST.scala:290)
at de.fhs.spirit.JsonTest.onCreate(JsonTest.scala:16)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more
Would be great if you have an idea how to solve my problem.
Thank you!
Greetings,
Illaz
I suspect Lift uses reflection in this mode. It might not work at all in Android, or it may require that some stuff that got removed not be removed. I suggest you just try to use some of other alternatives instead.
See also this question -- my suspicion comes from the remark that it doesn't work on REPL. Not that REPL doesn't have reflection, but its package structure might be confusing to the Lift library.

NumberFormatException - Android

I've uploaded my Android app to the Market today, and I received 4 errors regarding NumberFormatException,Double.parseDouble and Double.valueOf.
Here's the Logcat I see in the Developer Console:
java.lang.RuntimeException: Unable to start activity ComponentInfo{can.you.drive/can.you.drive.do_drive}: java.lang.NumberFormatException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
at android.app.ActivityThread.access$1500(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4196)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NumberFormatException
at org.apache.harmony.luni.util.FloatingPointParser.parseDblImpl(Native Method)
at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:283)
at java.lang.Double.parseDouble(Double.java:318)
at java.lang.Double.valueOf(Double.java:356)
at can.you.drive.dont_drive.roundTwoDecimals(dont_drive.java:132)
at can.you.drive.do_drive.onCreate(do_drive.java:125)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
Here's the function in which the error occurs(according to the Log) -
public static double roundTwoDecimals(double d) {
DecimalFormat twoDForm = new DecimalFormat("#.###");
return Double.valueOf(twoDForm.format(d));
}
I know that d is double.
Also, this works fine on my phone(Nexus S running 2.3.4). I'm getting the error from other users. That's why I can't really debug it.
I really don't know what my cause this on some phones.
I'd say it's got to do with localization, with . not being the decimal separator in the platforms where the error occurs. Why don't you use a less expensive method of rounding?
public double round(double d, int nDecimals)
{
for(int i=0; i<nDecimals; ++i) d *= 10d;
d=Math.round(d);
for(int i=0; i<nDecimals; ++i) d /= 10d;
return d;
}
use try catch block to handle it.
Perhaps the error occurs when someone from the Deutsch language uses your application. I've had this problem. They use a comma in place of a decimal (',' instead of '.'). This has thrown me off multiple times
To debug it, add a ton of System.out.println()'s all over your code that spit out a variable when it is changed. These are easier than tools like gdb.

Arrayadapter.getcount null point exception

I am getting the following stack trace (below is a complete copy) , this gives me little or no indication as to where in a massive application it is when going wrong and user feedback is nothing beyond "It crashed".
Is there anything I can do to pinpoint this more ?
java.lang.NullPointerException at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:291)
at android.widget.AdapterView.checkFocus(AdapterView.java:689)
at android.widget.AdapterView$AdapterDataSetObserver.onInvalidated(AdapterView.java:813)
at android.database.DataSetObservable.notifyInvalidated(DataSetObservable.java:43)
at android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:54)
at android.widget.ArrayAdapter$ArrayFilter.publishResults(ArrayAdapter.java:469)
at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Look at the code for ArrayAdapter.getCount():
/**
* {#inheritDoc}
*/
public int getCount() {
return mObjects.size();
}
Clearly mObjects is null, i.e. your ListView or other AdapterView-derived type is trying to use the adapter before you've initialized it with it's data.
What you could do is putting debug information before every call to getCount() using the Log class
Log.d("tag", "Trying to get count on line 50 class Test");
then open logcat and find the last entry of this log before the error occures. Then you have the right location of the crash.

Categories

Resources