NullpointerException onPostExecute - - java

I'm having a bit of trouble getting Async to work correctly. I have my doInBackground passing a string of a JSON response, which my onPostExecute will parse and then send to the view, but I keep getting a null pointer for my onPostExecute. I know that it's getting a proper response back because my Log prints the JSON string I'm expected.
Am I not passing something correctly?
Here's my stacktrace:
07-07 13:26:33.757: ERROR/AndroidRuntime(5714): FATAL EXCEPTION: main
java.lang.NullPointerException
at com.dsgunter.randompokemongenerator.Generator$LoadPokemon.onPostExecute(Generator.java:153)
at com.dsgunter.randompokemongenerator.Generator$LoadPokemon.onPostExecute(Generator.java:113)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5021)
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:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
and my code, which I'll post in a gist for brevity's sake:
https://gist.github.com/verkaufer/88eeb44a0997a4a49779

The problem is at progressDialog.dismiss(). That progressDialog variable is null. You created a pDialog local to the onPreExecute method above that, which is what I think you really want to be dismissing.

Your progressDialog variable looks to never be initialized, so it is null. And this line
progressDialog.dismiss();
then throws NPE because of that.

You never initialize progressDialog, so it is null. I guess you want to dismiss the dialog created in onPreExecute, so add this line:
progressDialog = pDialog;

Related

App crashes when restoring from background

I have static values that are deleted after my application is a long time in the background or after opening many different apps, causing that my app crashes.
I think that Android OS is removing data from my app or "killing" it.
Is there any way to keep my app's data to avoid this problem?
NOTE: I can't save this values on SharedPreferences or in a database because there are many HashMaps and ArrayLists, is too much information.
It is necessary that these values are static because I need them available in many classes o my app in any time.
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.proyect/com.proyect.gui.ActivityMenu}: java.lang.NullPointerException: println needs a message
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
at android.app.ActivityThread.access$600(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:5225)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.v(Log.java:117)
at com.proyect.gui.FragmentMenu.onActivityCreated(FragmentMenu.java:145)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1983)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1092)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1234)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2046)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:174)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
at android.app.Activity.performStart(Activity.java:5143)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2239)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) 
at android.app.ActivityThread.access$600(ActivityThread.java:150) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:213) 
at android.app.ActivityThread.main(ActivityThread.java:5225) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 
at dalvik.system.NativeStart.main(Native Method) 
Change this:
Log.v("errorTag", e.getMessage());
To this:
String error = e.getMessage();
if(error == null)
error = "It's crashed here";
Log.v("errorTag", error );
OR SIMPLER
Log.v("errorTag", e );
The point is that e.getMessage() is null. So, always check if e.getMessage() is not null before print it

Android detect crash

I am getting this crash log for my application:
java.lang.IllegalStateException
android.widget.ListView.layoutChildren(ListView.java:1544)
android.widget.AbsListView.onTouchModeChanged(AbsListView.java:3543)
android.view.ViewTreeObserver.dispatchOnTouchModeChanged(ViewTreeObserver.java:732)
android.view.ViewRootImpl.ensureTouchModeLocally(ViewRootImpl.java:3170)
android.view.ViewRootImpl.ensureTouchMode(ViewRootImpl.java:3154)
android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3397)
android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3347)
android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4456)
android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4434)
android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4538)
android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
android.os.MessageQueue.nativePollOnce(Native Method)
android.os.MessageQueue.next(MessageQueue.java:125)
android.os.Looper.loop(Looper.java:124)
android.app.ActivityThread.main(ActivityThread.java:4921)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
dalvik.system.NativeStart.main(Native Method)
But i can't see in this lines nothing that point to my code. any idea how i can detect the crash?
The way to get an IllegalStateException in ListView.layoutChildren() is to make the adapter item count differ from what the listview expected.
This can happen if you changed the data in your listview adapter but forgot to call notifyDataSetChanged(), or modified the data in a background thread.

Force close when app restarts

I really don't know how to start this, but I have searched through the whole of Google and stackoverflow.
I have this radio app which plays radio(duh) and display information about the song playing currently. But I have a problem here:
I have an Activity(MainActivity) and 3 Fragments(playerFragment, the rest are not important) and the fragments are tabs.
I parse the JSON data on MainActivity and display it on playerFragment. I am able to display it straight, no problems. But when I press the back the button and open the app again, it crashes immediately. Of course, I have a service playing the audio only and I ensure that the app loads the data when the user returns to the app(in this case the Activity and fragment is destroyed while service is still running). I checked the log it stated NullPointerException on the point when it starts to setText onCreate. I use the exact same code as it was to load the data at first (before destroying the Activity and Fragment).
I don't know how am I gonna post the code here because I do not know where went wrong and the code was long, please tell me which part of the code you would want to see to check what's wrong.
Code where it start to have error:
TextView titleTextView = (TextView) findViewById(R.id.titleTextView);
TextView artistTextView = (TextView) findViewById(R.id.artistTextView);
TextView lyricsTextView = (TextView) findViewById(R.id.lyricsTextView);
ImageView albumImageView = (ImageView) findViewById(R.id.albumImageView);
JSONObject jsonObject = new JSONObject(jsonData);
String title = jsonObject.getString("title");
String artist = jsonObject.getString("artist");
String lyrics = jsonObject.getString("lyrics");
String albumURL = jsonObject.getString("coverUrl");
STARTING HERE: ---> titleTextView.setText(title);
artistTextView.setText("\n" + artist);
lyricsTextView.setText("\n" + lyrics);
This code was able to display the data when I first open the app and parse it, the app the force close when I return/start to the app(while service is running).
The logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.brandon.sgpradio/com.brandon.sgpradio.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2453)
at android.app.ActivityThread.access$900(ActivityThread.java:173)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5579)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.brandon.sgpradio.MainActivity.parseJson(MainActivity.java:225)
at com.brandon.sgpradio.MainActivity.isMyServiceRunning(MainActivity.java:282)
at com.brandon.sgpradio.MainActivity.onCreate(MainActivity.java:97)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2453)
at android.app.ActivityThread.access$900(ActivityThread.java:173)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5579)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Thanks!
Update:
1. Add a tag to your fragment when you add it to the activity via ViewPager.
fragment = new playerFragment();
fragment.setTag("player_fragment");
Get the fragment via getFragmentManger().findFragmentByTag() method.
PlayerFragment pFragment =
getFragmentManager().findFragmentByTag("player_fragment");
if(pFragment != null)
{
pFragment.setTextViewText();
}

Trying to add a Facebook Friend Picker, but get null pointer exception

I'm trying to learn and add some basic Facebook functionality to my Android App (an address book). What I'm eventually going to do is import a friends profile picture. I've currently only copy/pasted what I need from the "FriendPickerSample" example project, which is just a button that brings up a friend picker fragment, and, if I remember correctly, it places the names you picked into a list. I've altered it to come up when an imageview is clicked, and it'll just Toast the single name that is chosen. When I try to bring up the friend picker, I get a null pointer exception, and I don't understand why. Here's the log:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.JDE.RAB/com.JDE.RAB.PickFriendsActivity}: java.lang.NullPointerException
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: java.lang.NullPointerException
at com.facebook.widget.PickerFragment.onCreateView(PickerFragment.java:152)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:523)
at com.JDE.RAB.PickFriendsActivity.onStart(PickFriendsActivity.java:112)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
at android.app.Activity.performStart(Activity.java:3781)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
... 11 more
This is the code in my onclick, which is supposed to open the PickFriendsActivity:
if (Session.getActiveSession() == null || Session.getActiveSession().isClosed())
{
Session.openActiveSession(AddNewTabLayoutActivity.this, true, null);
}//endif
FriendPickerApplication application = (FriendPickerApplication) GetMyApplication();
application.setSelectedUsers(null);
Intent intent = new Intent(AddNewTabLayoutActivity.this, PickFriendsActivity.class);
// Note: The following line is optional, as multi-select behavior is the default for
// FriendPickerFragment. It is here to demonstrate how parameters could be passed to the
// friend picker if single-select functionality was desired, or if a different user ID was
// desired (for instance, to see friends of a friend).
//multi-select is OFF
PickFriendsActivity.populateParameters(intent, null, false, true);
Debugger.SendNotification(getApplicationContext(), "Alert", "Alert", "Starting Activity For Result", 3009);
startActivityForResult(intent, 2);
PickFriendsActivity, line 112 is super.onStart(); Should I post the entirety of that activities source code?
Thanks for any help!
You should copy res/layout/pick_friends_activity.xml to your own project too.

Asynctask causing NullPointerException

I have an app on Google play. I got a few error reports from the developer page and It's quite hard to figure out the problem.
This is what I have:
java.lang.NullPointerException
at com.seb.example.free.MainActivity$ApplyFilter.onPostExecute(MainActivity.java:828)
at com.seb.example.free.MainActivity$ApplyFilter.onPostExecute(v.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:4987)
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:821)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)
As you can see I have an asynctask innerclass and the error seems to be in on postexecute.
I have checked my project in eclipse and, as the stacktrace says, the error is on line 828:
(iV.getVisibility() == ImageView.INVISIBLE)
My imageView, iV, is declared as private in MainActivity and I initiate it in oncreate.
I can't reproduce the problem on the three of my phones as i have tested on, so it's quite hard to understand what's causing it.
Anyone who has any ideas? Thanks!
I think that imageview object is becomes null in some where in your code..check whether it initialized or not..and if initialized check where it is used in your project.
It might have been derefferenced in some other place, initialize in onresume and see whether this persists or not.
please check have you initialized iv
try:
if(iv != null)
(iV.getVisibility() == View.INVISIBLE)
else
Log.e("CHECK","iv Null");
if you have not initialized iv then do that first.

Categories

Resources