javaandroidparse-platformnullpointerexceptionandroid-parsequeryadapter

Attempt to invoke virtual method 'void com.parse.ParseImageView.setParseFile(com.parse.ParseFile)' on a null object reference


I KNOW what a NullPointerException is, alright? But, what are the possible mistakes for this line of code:

mealImage.setParseFile(campaign.getImage());

to give me aNullPointerException?

 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.parse.ParseImageView.setParseFile(com.parse.ParseFile)' on a null object reference
                                                                                 at com.alibdeir.signupactivity.mainAdapter.getItemView(mainAdapter.java:41)
                                                                                 at com.alibdeir.signupactivity.mainAdapter.getItemView(mainAdapter.java:19)
                                                                                 at com.parse.ParseQueryAdapter.getView(ParseQueryAdapter.java:606)
                                                                                 at android.widget.AbsListView.obtainView(AbsListView.java:2823)
                                                                                 at android.widget.ListView.measureHeightOfChildren(ListView.java:1290)
                                                                                 at android.widget.ListView.onMeasure(ListView.java:1202)
                                                                                 at android.view.View.measure(View.java:18580)
                                                                                 at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
                                                                                 at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
                                                                                 at android.view.View.measure(View.java:18580)
                                                                                 at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
                                                                                 at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
                                                                                 at android.view.View.measure(View.java:18580)
                                                                                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
                                                                                 at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                                                                                 at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                                 at android.view.View.measure(View.java:18580)
                                                                                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
                                                                                 at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
                                                                                 at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
                                                                                 at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
                                                                                 at android.view.View.measure(View.java:18580)
                                                                                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
                                                                                 at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                                                                                 at android.view.View.measure(View.java:18580)
                                                                                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
                                                                                 at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
                                                                                 at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
                                                                                 at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
                                                                                 at android.view.View.measure(View.java:18580)
                                                                                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
                                                                                 at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                                                                                 at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2897)
                                                                                 at android.view.View.measure(View.java:18580)
                                                                                 at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2248)
                                                                                 at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1306)
                                                                                 at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1548)
                                                                                 at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1191)
                                                                                 at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6642)
                                                                                 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
                                                                                 at android.view.Choreographer.doCallbacks(Choreographer.java:590)
                                                                                 at android.view.Choreographer.doFrame(Choreographer.java:560)
                                                                                 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:145)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5942)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)

Line 41: is the first line of code I gave you. Line 19 is:

public class mainAdapter extends ParseQueryAdapter<Campaign> {

So, what can be null? My ParseImageView is correct, correct class and column name. How am I getting the image: Campaign.class:

@ParseClassName("Campaign")

public class Campaign extends ParseObject {
    public Campaign() {
        // default Constructor
    }

    public String getTitle() {
        return getString("Title");
    }

    public String getDescription(){
        return getString("Description");
    }

    public ParseUser getAuthor() {
        return getParseUser("Owner");
    }

    public ParseFile getImage(){
        return getParseFile("Picture");
    }



}

Need more info? Note: Don't go -1ing at me, I know what a NullPointerException is. So, heads up! Edit:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TableRow>
        <com.parse.ParseImageView
            android:id="@android:id/icon"
            android:layout_width="100dp"
            android:layout_height="100dp"
            />
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:textColor="#000000"
            />
        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10sp"
            android:textColor="#454343"
            />

    </TableRow>
</TableLayout>

getItemView():

   @Override
public View getItemView(Campaign campaign, View v, ViewGroup parent) {

    if (v == null) {
        v = View.inflate(getContext(), R.layout.list_single,null);
    }

    super.getItemView(campaign, v, parent);

    ParseImageView mealImage = (ParseImageView) v.findViewById(R.id.icon);
    ParseFile photoFile = campaign.getImage();
    if (photoFile != null) {
        mealImage.setParseFile(campaign.getImage());
        mealImage.loadInBackground(new GetDataCallback() {
            @Override
            public void done(byte[] data, ParseException e) {
                // nothing to do
            }
        });
    }

    TextView titleTextView = (TextView) v.findViewById(R.id.title);
    titleTextView.setText(campaign.getTitle());
    TextView descriptionTextView = (TextView) v
            .findViewById(R.id.description);
    descriptionTextView.setText(campaign.getDescription());
    return v;

}

Edit: This is not a duplicate, I know what a nullPointerException is. But I don't get what is null. I used to use mParseQueryAdapter.setImageKey("Picture") and it worked


Solution

  • When you attempt to findViewById if the referenced view is not found it returns null.

    You are getting android:id/icon and creating a new @+id/icon mixed up.

    This is your code:

     ParseImageView mealImage = (ParseImageView) v.findViewById(R.id.icon);
    
     <com.parse.ParseImageView
            android:id="@android:id/icon"
            android:layout_width="100dp"
            android:layout_height="100dp"
            />
    

    change

    v.findViewById(R.id.icon);

    to this

    v.findViewById(android.R.id.icon);

    or the opposite (in your xml)
    change

    android:id="@android:id/icon"

    to

    android:id="@+id/icon"


    Under the hood Android will change these id's to numbers lets say:

    android:id/icon = 123 and +id/icon = 456

    therefore

    <View
      android:id="android:id/icon"
      ... />
    

    and

    <View
      android:id="android:id/icon"
      ... />
    

    will resolve to two different numbers (meaning looking up one when you want the other will give you a NullPointerException)