6 Replies - 199 Views - Last Post: 26 May 2019 - 08:25 PM

#1 awthz   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 76
  • Joined: 13-February 17

Attempt to invoke virtual method 'boolean java.lang.String.equals

Posted 26 May 2019 - 07:01 AM

Hi guys.. I am trying to figure out the whole week on how to fix this bug, however I did not find any answers in internet.
Im trying to access my database in firebase and to be shown in the recyclerview.

I hope the experts here can save me with this.


ListStudent.class/main
package com.example.deped;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.[color="#FF0000"]RecyclerView[/color];
//import android.widget.SearchView;
import android.support.v7.widget.SearchView;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class ListStudents extends AppCompatActivity {


    ArrayList <ListStudentModel> listStudentModels;
    DatabaseReference databaseReference;
    [color="#FF0000"]RecyclerView[/color] recyclerView;
    SearchView searchView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_students);


        databaseReference = FirebaseDatabase.getInstance().getReference().child("Student");
        recyclerView = findViewById(R.id.studentListRecyclerView);
        searchView = findViewById(R.id.studentListSearchView);
    }

    @Override
    protected void onstart() {
        super.onstart();

        if(databaseReference != null){

            databaseReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()){

                        listStudentModels = new ArrayList<>();
                        for(DataSnapshot dataSnapshot1 : dataSnapshot.getChildren())
                        {
                            listStudentModels.add(dataSnapshot1.getValue(ListStudentModel.class));

                        }
                        ListStudentViewHolder listStudentViewHolder = new ListStudentViewHolder(listStudentModels);
                        recyclerView.[color="#FF0000"]setAdapter[/color](listStudentViewHolder);
                    }
                    else
                    {
                        Toast.makeText(ListStudents.this, "ERROR1", Toast.LENGTH_LONG).show();
                    }

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    Toast.makeText(ListStudents.this, "ERROR 2", Toast.LENGTH_LONG).show();
                }
            });
        }

        if (searchView != null){
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String s) {
                    search(s);
                    return true;
                }
            });
        }
        else
        {
            Toast.makeText(ListStudents.this, "ERROR3", Toast.LENGTH_LONG).show();
        }

    }


    private void search(String str){

        ArrayList<ListStudentModel> listStudentModels1 = new ArrayList<>();
        for (ListStudentModel object: listStudentModels1)
        {
            if (object.getFname().toLowerCase().contains(str.toLowerCase()))
            {
                listStudentModels1.add(object);
            }
            else
            {
                Toast.makeText(ListStudents.this, "ERROR4", Toast.LENGTH_LONG).show();
            }
        }

        ListStudentViewHolder listStudentViewHolder = new ListStudentViewHolder(listStudentModels1);
        recyclerView.[color="#8B0000"]setAdapter[/color](listStudentViewHolder);

    }

}




for my Model
package com.example.deped;

public class ListStudentModel {
    private String Age, DOB, Fname, Lname, Sex;


    //Empty Constructor
    public ListStudentModel() {
    }

    //Constructor
    public ListStudentModel(String age, String DOB, String fname, String lname, String sex) {
        Age = age;
        this.DOB = DOB;
        Fname = fname;
        Lname = lname;
        Sex = sex;
    }



   // Getter & Setter
    public String getAge() {
        return Age;
    }

    public void setAge(String age) {
        Age = age;
    }

    public String getDOB() {
        return DOB;
    }

    public void setDOB(String DOB) {
        this.DOB = DOB;
    }

    public String getFname() {
        return Fname;
    }

    public void setFname(String fname) {
        Fname = fname;
    }

    public String getLname() {
        return Lname;
    }

    public void setLname(String lname) {
        Lname = lname;
    }

    public String getSex() {
        return Sex;
    }

    public void setSex(String sex) {
        Sex = sex;
    }
}




Adapter
package com.example.deped;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class ListStudentViewHolder extends RecyclerView.Adapter<ListStudentViewHolder.mViewHolder>{


    ArrayList <ListStudentModel> listStudentModels;
    public ListStudentViewHolder(ArrayList <ListStudentModel> listStudentModels){
        this.listStudentModels = listStudentModels;
    }

    @NonNull
    @Override
    public mViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_layout,viewGroup, false);
        return new mViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull mViewHolder mViewHolder, int i) {
        //bind the data to list_layout
        mViewHolder.mFname.setText(listStudentModels.get(i).getFname());
        mViewHolder.mLname.setText(listStudentModels.get(i).getLname());
        mViewHolder.mAge.setText(listStudentModels.get(i).getAge());
        mViewHolder.mDOB.setText(listStudentModels.get(i).getDOB());
        mViewHolder.mSex.setText(listStudentModels.get(i).getSex());

    }

    @Override
    public int getItemCount() {
        return listStudentModels.size();
    }

    class mViewHolder extends RecyclerView.ViewHolder{

        TextView mFname, mLname, mAge, mDOB, mSex;
        public mViewHolder(@NonNull View itemView) {
            super(itemView);
            mFname = itemView.findViewById(R.id.fnameTextView);
            mLname = itemView.findViewById(R.id.lnameTextView);
            mAge = itemView.findViewById(R.id.ageTextView);
            mDOB = itemView.findViewById(R.id.dobTextView);
            mSex = itemView.findViewById(R.id.sexTextView);
        }
    }

}



another this is that my RecyclerView is turning red and coudnt figure out what happen,
I did restart and clear cache
implement the reclerview in the gradle

but still coundt find the reason.
i will attached my gradle just in case there is something wrong with it.

Gradle app
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.deped"
        minSdkVersion 18
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.firebase:firebase-core:16.0.9'
    implementation 'com.google.firebase:firebase-database:17.0.0'
    implementation 'com.google.firebase:firebase-auth:17.0.0'
    implementation 'com.firebase:firebase-client-android:2.5.2+'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
    //implementation 'com.firebaseui:firebase-ui-database:3.2.1'
    //implementation 'com.squareup.picasso:picasso:picasso:2.71828'
}
apply plugin: 'com.google.gms.google-services'



This post has been edited by awthz: 26 May 2019 - 08:16 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Attempt to invoke virtual method 'boolean java.lang.String.equals

#2 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 55
  • View blog
  • Posts: 706
  • Joined: 23-September 15

Re: Attempt to invoke virtual method 'boolean java.lang.String.equals

Posted 26 May 2019 - 07:04 AM

Quote

another this is that my RecyclerView is turning red

Which line is red? Point to the red line, it should displays the error message.

This post has been edited by wseng92: 26 May 2019 - 07:05 AM

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12648
  • View blog
  • Posts: 45,822
  • Joined: 27-December 08

Re: Attempt to invoke virtual method 'boolean java.lang.String.equals

Posted 26 May 2019 - 07:37 AM

Nowhere in your code did you attempt to invoke the String equals() method...
Was This Post Helpful? 0
  • +
  • -

#4 awthz   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 76
  • Joined: 13-February 17

Re: Attempt to invoke virtual method 'boolean java.lang.String.equals

Posted 26 May 2019 - 08:20 AM

I was just calling the data in the Firebase to the recycler view.
Should I need to use the String.equals()? if yes, where should I put it?

Im just a novice in comes to programming.

And I really appreciate the help guys.



Sorry, was not able to post the error message earlier when run.

2019-05-27 03:05:24.937 21914-21914/com.example.deped E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.deped, PID: 21914
    android.view.InflateException: Binary XML file line #88: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at com.example.deped.ListStudentViewHolder.onCreateViewHolder(ListStudentViewHolder.java:23)
        at com.example.deped.ListStudentViewHolder.onCreateViewHolder(ListStudentViewHolder.java:12)
        at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6794)
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
        at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
        at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3336)
        at android.view.View.measure(View.java:22071)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
        at android.view.View.measure(View.java:22071)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
        at android.view.View.measure(View.java:22071)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
        at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:401)
        at android.view.View.measure(View.java:22071)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:22071)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
        at android.view.View.measure(View.java:22071)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:724)
        at android.view.View.measure(View.java:22071)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2422)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1504)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1761)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1392)
2019-05-27 03:05:24.937 21914-21914/com.example.deped E/AndroidRuntime:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6752)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:658)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)


Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12648
  • View blog
  • Posts: 45,822
  • Joined: 27-December 08

Re: Attempt to invoke virtual method 'boolean java.lang.String.equals

Posted 26 May 2019 - 08:40 AM

Your issue is that the Object in question is null.

Quote

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference


The error message points you to the specific line in your XML file, which you did not post. I would suggest checking there to make sure you set everything up correctly.

Quote

android.view.InflateException: Binary XML file line #88: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

Was This Post Helpful? 0
  • +
  • -

#6 awthz   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 76
  • Joined: 13-February 17

Re: Attempt to invoke virtual method 'boolean java.lang.String.equals

Posted 26 May 2019 - 03:35 PM

The Xml file that is involve in this are these

these is for the recyclerview:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <android.support.v7.widget.SearchView
        android:id="@+id/studentListSearchView"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:padding="10dp"
        android:layout_margin="5dp"
        app:queryHint="Search"
        ></android.support.v7.widget.SearchView>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/studentListRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        android:layout_margin="5dp"
        ></android.support.v7.widget.RecyclerView>
</LinearLayout>



These is for the Cardview:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:orientation="vertical">


    <android.support.v7.widget.CardView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        app:cardElevation="5dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="8dp">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="120dp"
                android:layout_height="90dp"
                android:padding="4dp" />

            <TextView
                android:id="@+id/fnameTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@id/imageView"
                android:text="First Name"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
                android:textColor="#000000" />

            <TextView
                android:id="@+id/lnameTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/fnameTextView"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@id/imageView"
                android:text="13.3 Inch, 256 GB"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Small" />

            <TextView
                android:id="@+id/ageTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/lnameTextView"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@id/imageView"
                android:background="@color/colorPrimary"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:text="4.7"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Small.Inverse"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/sexTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/ageTextView"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@id/imageView"
                android:text="INR 56990"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/dobTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/sexTextView"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@id/imageView"
                android:text="INR 56990"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
                android:textStyle="bold" />

        </RelativeLayout>
        <view
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#000"/>

    </android.support.v7.widget.CardView>

</LinearLayout>

Was This Post Helpful? 0
  • +
  • -

#7 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 55
  • View blog
  • Posts: 706
  • Joined: 23-September 15

Re: Attempt to invoke virtual method 'boolean java.lang.String.equals

Posted 26 May 2019 - 08:25 PM

This is wrong
<view
     android:layout_width="match_parent"
     android:layout_height="1dp"
     android:background="#000"/>

Use View instead of view.

This post has been edited by wseng92: 26 May 2019 - 08:25 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1