NullPointerException

  • Replies:6
Khasan Mukhitdinov
  • Forum posts: 4

Feb 8, 2016, 6:25:46 AM via Website

Good day for all programmers.
I have a problem NullPointerException when i call the method showAtLocation of PopupWindow. As in many forums was written, this exception happens because first parameter of method showAtLocation is null. So i check it this way:
showAtLocation (View parent, int gravity, int x, int y)
1. parent.equals(null) - it returns false
2. LinearLayout lout = (LinearLayout) parent;
lout.getChildCount() - it returns true count of child elements
3. ((TextView) lout.getChildAt(1)).getText() - it returns a text which i write in android:text field

Please, i need your help

Reply
Prabhu
  • Forum posts: 2

Feb 8, 2016, 2:20:27 PM via Website

need to see your code so only can able to find out what mistake u have made

Reply
Khasan Mukhitdinov
  • Forum posts: 4

Feb 8, 2016, 3:06:18 PM via Website

I have a gridview and its adapter from class CustomAdapter (extends BaseAdapter). In this class (CustomAdapter) has OnClickListener in getView method. I want to set popupwindow for each item of this gridview. So in OnClickListener i call a method showPopup:

private void showPopup(final Activity context, Point p) {
int popupWidth = 200;
int popupHeight = 150;

// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//LayoutInflater layoutInflater = prnt.getLayoutInflater();
View parent = layoutInflater.inflate(R.layout.popup_layout, viewGroup);

final PopupWindow popup = new PopupWindow(context);
popup.setContentView(parent);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);

int OFFSET_X = 30;
int OFFSET_Y = 30;

popup.setBackgroundDrawable(new BitmapDrawable());

/*
LinearLayout lout = (LinearLayout) parent;
showMsg(parent.equals(null) + " : type " + lout.getChildCount() + " - " + ((TextView) lout.getChildAt(1)).getText());
*/
popup.showAtLocation(parent, 0, p.x + OFFSET_X, p.y + OFFSET_Y); //error occurs here
}

Reply
Khasan Mukhitdinov
  • Forum posts: 4

Feb 10, 2016, 3:05:28 PM via Website

Here is full logcat dump:

02-10 13:49:34.148 2376-2376/com.iyb.wi.mobi I/art: Not late-enabling -Xcheck:jni (already on)
02-10 13:49:34.255 2376-2376/com.iyb.wi.mobi W/System: ClassLoader referenced unknown path: /data/app/com.iyb.wi.mobi-2/lib/x86
02-10 13:49:34.331 2376-2376/com.iyb.wi.mobi I/GMPM: App measurement is starting up, version: 8487
02-10 13:49:34.331 2376-2376/com.iyb.wi.mobi I/GMPM: To enable debug logging run: adb shell setprop log.tag.GMPM VERBOSE
02-10 13:49:34.501 2376-2392/com.iyb.wi.mobi D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-10 13:49:34.582 2376-2392/com.iyb.wi.mobi I/OpenGLRenderer: Initialized EGL, version 1.4
02-10 13:49:34.639 2376-2392/com.iyb.wi.mobi W/EGL_emulation: eglSurfaceAttrib not implemented
02-10 13:49:34.639 2376-2392/com.iyb.wi.mobi W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad6dfbc0, error=EGL_SUCCESS
02-10 13:49:45.041 2376-2390/com.iyb.wi.mobi I/GMPM: Tag Manager is not found and thus will not be used
02-10 13:50:05.590 2376-2386/com.iyb.wi.mobi I/art: Background sticky concurrent mark sweep GC freed 11567(906KB) AllocSpace objects, 10(200KB) LOS objects, 22% free, 2MB/3MB, paused 25.010ms total 151.250ms
02-10 13:50:05.670 2376-2392/com.iyb.wi.mobi W/EGL_emulation: eglSurfaceAttrib not implemented
02-10 13:50:05.670 2376-2392/com.iyb.wi.mobi W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad6e5d00, error=EGL_SUCCESS
02-10 13:50:08.094 2376-2376/com.iyb.wi.mobi I/Choreographer: Skipped 141 frames!  The application may be doing too much work on its main thread.
02-10 13:50:08.165 2376-2392/com.iyb.wi.mobi E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab793b90
02-10 13:50:34.728 2376-2392/com.iyb.wi.mobi W/EGL_emulation: eglSurfaceAttrib not implemented
02-10 13:50:34.728 2376-2392/com.iyb.wi.mobi W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa1c51100, error=EGL_SUCCESS
02-10 13:50:35.582 2376-2392/com.iyb.wi.mobi E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab793c00
02-10 13:50:35.885 2376-2392/com.iyb.wi.mobi W/EGL_emulation: eglSurfaceAttrib not implemented
02-10 13:50:35.885 2376-2392/com.iyb.wi.mobi W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa1c51d00, error=EGL_SUCCESS
02-10 13:50:38.395 2376-2392/com.iyb.wi.mobi E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab793c70
02-10 13:50:45.007 2376-2392/com.iyb.wi.mobi W/EGL_emulation: eglSurfaceAttrib not implemented
02-10 13:50:45.007 2376-2392/com.iyb.wi.mobi W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa17bad20, error=EGL_SUCCESS
02-10 13:50:45.947 2376-2392/com.iyb.wi.mobi E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb3fd6830
02-10 13:50:46.023 2376-2386/com.iyb.wi.mobi I/art: Background sticky concurrent mark sweep GC freed 3519(315KB) AllocSpace objects, 2(40KB) LOS objects, 0% free, 4MB/4MB, paused 12.041ms total 40.080ms
02-10 13:50:50.583 2376-2376/com.iyb.wi.mobi D/AndroidRuntime: Shutting down VM
02-10 13:50:50.583 2376-2376/com.iyb.wi.mobi E/AndroidRuntime: FATAL EXCEPTION: main
                                                              Process: com.iyb.wi.mobi, PID: 2376
                                                              java.lang.NullPointerException: Attempt to read from field 'int android.graphics.Point.x' on a null object reference
                                                                  at com.iyb.wi.mobi.CustomAdapter.showPopup(CustomAdapter.java:136)
                                                                  at com.iyb.wi.mobi.CustomAdapter.access$000(CustomAdapter.java:23)
                                                                  at com.iyb.wi.mobi.CustomAdapter$1.onClick(CustomAdapter.java:84)
                                                                  at android.view.View.performClick(View.java:5198)
                                                                  at android.view.View$PerformClick.run(View.java:21147)
                                                                  at android.os.Handler.handleCallback(Handler.java:739)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                  at android.os.Looper.loop(Looper.java:148)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Reply
Miran Kirn
  • Forum posts: 25

Feb 11, 2016, 6:28:00 PM via Website

Hello,

According to dump, there is a problem with initialization of Point p.

You should check the values for p.x and p.y.

Regards,

Khasan Mukhitdinov

Reply
Khasan Mukhitdinov
  • Forum posts: 4

Feb 12, 2016, 6:16:11 AM via Website

Thanks for Miran Kirin

Reply
David Smith
  • Forum posts: 32

Feb 17, 2016, 1:47:17 PM via Website

Hi,

There is a problem with initialization of p.Check for value of p.

Reply