-1

When I try to run this adapter code, I get java.lang.NullPointerException. I used the debugger checks the error. Also, I checked the variables currentbuildingnumber and BuildingNumber and the log statement printed

08-07 18:44:25.528 12998-12998/com.example.mustafa.progressupdate V/E1: Adapter Building Number is E1
Activity Building Number is E1

so I think the problem is in the if statement ... yet I dunno why it wont work and both variables are defined and the statement is true!

adapter code

public class ActivityAdapter extends ArrayAdapter<Activity> {

/**
 * Resource ID for the background color for this list of words
 */
public Activity currentActivity;
public String BuildingNumber;
public String currentbuildingnumber;

public ActivityAdapter(Context context, ArrayList<Activity> Activities, String adapterbuildingnumber) {
    super(context, 0, Activities);
    // This is the Building number of the Clicked Building
    currentbuildingnumber = adapterbuildingnumber;
    // This is the Building number of the Activity
    BuildingNumber = currentActivity.getBuildingNumber();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    Log.v("E1", "Adapter Building Number is " + currentbuildingnumber);
    Log.v("E1", "Activity Building Number is " + BuildingNumber);

    View v = convertView;

    if(BuildingNumber == currentbuildingnumber) {

    // Check if an existing view is being reused, otherwise inflate the view
    if (v == null) {
        v = LayoutInflater.from(getContext()).inflate(R.layout.project_actvitiy_list, parent, false);
    }

    // Get the {@link Word} object located at this position in the list
    currentActivity = getItem(position);

        TextView Act_ID = (TextView) v.findViewById(R.id.Act_ID);
        TextView Act_Name = (TextView) v.findViewById(R.id.Act_Name);
        TextView Act_Start = (TextView) v.findViewById(R.id.Act_Start);
        TextView Act_Prog = (TextView) v.findViewById(R.id.Act_Prog);
        TextView Act_Finish = (TextView) v.findViewById(R.id.Act_Finish);
        TextView Act_Note = (TextView) v.findViewById(R.id.Act_Note);

        Act_ID.setText(String.valueOf(currentActivity.getActivityID()));
        Act_Name.setText(String.valueOf(currentActivity.getActivityName()));
        Act_Start.setText(String.valueOf(currentActivity.getActualStart()));
        Act_Prog.setText(String.valueOf(currentActivity.getActivityProgress()));
        Act_Finish.setText(String.valueOf(currentActivity.getActualFinish()));
        Act_Note.setText(String.valueOf(currentActivity.getNotes()));

        Log.v("E1", "Activity ID is " + currentActivity.getActivityID());
        Log.v("E1", "Activity Name is " + currentActivity.getActivityName());
        Log.v("E1", "Activity Actual Start is " + currentActivity.getActualStart());
        Log.v("E1", "Activity Progress is " + currentActivity.getActivityProgress());
        Log.v("E1", "Activity Actual Finish is " + currentActivity.getActualFinish());
        Log.v("E1", "Activity Notes is " + currentActivity.getNotes());

}
    return v;
}
}

crash log

--------- beginning of crash
08-07 19:59:21.764 14227-14227/com.example.mustafa.progressupdate E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mustafa.progressupdate, PID: 14227
    java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference
        at android.widget.AbsListView.obtainView(AbsListView.java:2379)
        at android.widget.ListView.makeAndAddView(ListView.java:1970)
        at android.widget.ListView.fillDown(ListView.java:704)
        at android.widget.ListView.fillFromTop(ListView.java:765)
        at android.widget.ListView.layoutChildren(ListView.java:1744)
        at android.widget.AbsListView.onLayout(AbsListView.java:2161)
        at android.view.View.layout(View.java:17523)
        at android.view.ViewGroup.layout(ViewGroup.java:5612)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:17523)
        at android.view.ViewGroup.layout(ViewGroup.java:5612)
        at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
        at android.view.View.layout(View.java:17523)
        at android.view.ViewGroup.layout(ViewGroup.java:5612)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:17523)
        at android.view.ViewGroup.layout(ViewGroup.java:5612)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
        at android.view.View.layout(View.java:17523)
        at android.view.ViewGroup.layout(ViewGroup.java:5612)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
        at android.view.View.layout(View.java:17523)
        at android.view.ViewGroup.layout(ViewGroup.java:5612)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
        at android.view.Choreographer.doCallbacks(Choreographer.java:683)
        at android.view.Choreographer.doFrame(Choreographer.java:619)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
08-07 19:59:21.765 1660-2992/system_process W/ActivityManager:   Force finishing activity com.example.mustafa.progressupdate/.E1
08-07 19:59:21.767 1785-2008/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xa0c38200: ver 3 1 (tinfo 0xa0c7f460)
08-07 19:59:21.772 1660-2992/system_process W/ActivityManager:   Force finishing activity com.example.mustafa.progressupdate/.SoleyaProjectSchedule
08-07 19:59:21.790 1660-1683/system_process I/WindowManager: Screen frozen for +190ms due to Window{36e1b87 u0 NavigationBar}
08-07 19:59:21.819 1785-2008/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xa0c38200: ver 3 1 (tinfo 0xa0c7f460)
08-07 19:59:21.832 1785-2008/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xa0c38200: ver 3 1 (tinfo 0xa0c7f460)
08-07 19:59:21.844 1660-1729/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
08-07 19:59:21.844 1660-1729/system_process D/OpenGLRenderer: Swap behavior 1
08-07 19:59:21.845 1660-1729/system_process W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
08-07 19:59:21.845 1660-1729/system_process D/OpenGLRenderer: Swap behavior 0
08-07 19:59:21.848 1660-1729/system_process D/EGL_emulation: eglCreateContext: 0x92fa0300: maj 3 min 1 rcv 4
08-07 19:59:21.856 1660-1729/system_process D/EGL_emulation: eglMakeCurrent: 0x92fa0300: ver 3 1 (tinfo 0x9257e590)
08-07 19:59:21.859 1660-1729/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
    glUtilsParamSize: unknow param 0x00008cdf
08-07 19:59:21.860 1660-1729/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
08-07 19:59:21.867 1660-1729/system_process D/EGL_emulation: eglMakeCurrent: 0x92fa0300: ver 3 1 (tinfo 0x9257e590)
08-07 19:59:22.274 1660-1676/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{96f635b u0 com.example.mustafa.progressupdate/.E1 t153 f}
08-07 19:59:24.800 1399-1422/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 14558875 , only wrote 14290560
08-07 19:59:27.831 1660-1683/system_process I/art: Starting a blocking GC Explicit
08-07 19:59:27.857 1660-1683/system_process I/art: Explicit concurrent mark sweep GC freed 20596(1621KB) AllocSpace objects, 6(120KB) LOS objects, 26% free, 11MB/15MB, paused 846us total 26.282ms
08-07 19:59:30.905 1660-1739/system_process I/WifiConnectivityManager: start a single scan from watchdogHandler
    scheduleWatchdogTimer
08-07 19:59:31.600 1660-1676/system_process W/ActivityManager: Launch timeout has expired, giving up wake lock!
08-07 19:59:31.777 1660-1676/system_process W/ActivityManager: Activity destroy timeout for ActivityRecord{52aa486 u0 com.example.mustafa.progressupdate/.SoleyaProjectSchedule t153 f}
08-07 20:00:01.040 1785-2008/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xa0c38200: ver 3 1 (tinfo 0xa0c7f460)
'
Mustafa
  • 31
  • 11

2 Answers2

1

The problem is that you have a huge if block that sometimes avoids most of your getView() method:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    Log.v("E1", "Adapter Building Number is " + currentbuildingnumber);
    Log.v("E1", "Activity Building Number is " + BuildingNumber);

    View v = convertView;

    if(BuildingNumber == currentbuildingnumber) {
        ...
    }
    return v;
}

This means that sometimes you are just returning the value of convertView, and that can sometimes be null. This is a problem; getView() should never return null!

You'll have to change your code to handle this case. Perhaps you could move this block of code outside the main if:

// Check if an existing view is being reused, otherwise inflate the view
if (v == null) {
    v = LayoutInflater.from(getContext()).inflate(R.layout.project_actvitiy_list, parent, false);
}

That way you'd know that v was never null when you returned it.

Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • if i do, and the if statement is false... the adapter will inflate an empty view – Mustafa Aug 07 '18 at 18:11
  • the below crash log is when it returns null ... im searching what to do the it returns null ... i want when the result is null so it would move to next item or dont display anything – Mustafa Aug 07 '18 at 18:12
  • You are not allowed to return null from `getView()`. You have to return **something** every time. You could solve it by returning a view with 0 height, maybe. Or you could change your adapter so that it didn't include these items you want to skip. – Ben P. Aug 07 '18 at 18:14
  • returning 0dp height is great but if i want to change the adapter to skip these items .. what should i do .. sorry for my lack of knowledge as i started coding few weeks ago :D – Mustafa Aug 07 '18 at 18:17
0

== tests for reference equality (whether they are the same object).

.equals() tests for value equality (whether they are logically "equal").

more info

change == to .equals

Abubakar
  • 1,022
  • 10
  • 20