nullPointerException when replacing char sequence - android

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(" ")){

Related

Android. Strange crash in charAt (setSelection)

I have a very strange crash in application.
Stacktrace:
java.lang.IndexOutOfBoundsException: charAt: -1 < 0
at android.text.SpannableStringBuilder.charAt(SpannableStringBuilder.java:112)
at android.text.Selection.setSelection(Selection.java:84)
at android.text.Selection.setSelection(Selection.java:127)
at android.widget.Editor$InsertionHandleView.updateSelection(Editor.java:4971)
at android.widget.Editor$HandleView.positionAtCursorOffset(Editor.java:4647)
at android.widget.Editor$HandleView.updatePosition(Editor.java:4675)
at android.widget.Editor$PositionListener.onPreDraw(Editor.java:2714)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:711)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2097)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1179)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4859)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameHandler.handleMessage(Choreographer.java:664)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
I got the stack trace without any links on my application's packages (classes/methods).
Has anyone encountered this problem? Thx.
EDIT
Can you see, the stack trace has not any links on my application's packages/classes, but app anyway crashed.
UPD
I use Selection.setSelection. I don't use SpannableStringBuilder.
#Override
public void afterTextChanged(Editable s) {
if (!s.toString().isEmpty()) {
Selection.setSelection(s, s.length());
}
}
This seems to be an OS bug which require a combination of a certain OS version(e.g. 4.1.2) and a certain device(e.g. Galaxy S3 I9300). This is not your problem. Unfortunately I don't know how to get around this either.
I'm curious how you ran into this issue. Based on the callstack, my guess is you tap on a textview and tried to paste text from clipboard.
Read the first line... it says java.lang.IndexOutOfBoundsException: charAt: -1 < 0. The problem is your string does not have the character you are searching for. Post the code. Then we will see what's wrong...

java.lang.NumberFormatException (parse.Int)

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

i keep getting java.lang.NullPointerException in my developer console error report, but i can't reproduce it in the emulator or on my phone

java.lang.NullPointerException
at com.surreall.yacht.yatzee.achievementsCheck`(yatzee.java:2031)`
at com.surreall.yacht.yatzee.gameOver(yatzee.java:1929)
at com.surreall.yacht.yatzee$4.onClick(yatzee.java:1250)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
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:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(Native Method)
obviously it is happening in the function achievementsCheck(), but no matter what i do i can't reproduce the null pointer exception. what does the "2031" mean in that line? can that tell me anything?
As per your question "what does the "2031" mean in that line?" It is the line number of the code on which the NullpointerException is generating. So look at that line it might be because of an object which is not initialized and has a null reference.
So for example if you have
Line 1: Object obj; //Note obj is not initialized here.
Line 2:
Line 3: //and you are trying to call
Line 4:
Line 5: obj.toString()///NullPointer will be generated here
You will see a NullPointerException generated at line 5
To solve this issue you will have to initialize obj correctly so in this case it will
Object obj = new Object();

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

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