• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

I get this error trying to customize a school android app to show student address

Dronzzy

Lurker
Oct 8, 2018
7
0
I get this error trying to customize a school android app to show student address

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.schoolappniftysol, PID: 9689
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.schoolappniftysol.Adapter.StudentList_adapter.getView(StudentList_adapter.java:76)
at android.widget.AbsListView.obtainView(AbsListView.java:249



Below are my files.



StudentList_adapter.java

package com.schoolappniftysol.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.schoolappniftysol.Bean.StudentList;
import com.schoolappniftysol.Fragment.Fragment_student;
import com.schoolappniftysol.R;
import com.schoolappniftysol.SessionManager;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

/**
* Created by admin on 3/8/2017.
*/

public class StudentList_adapter extends BaseAdapter {
Context context;
LayoutInflater inflater;
ArrayList<StudentList> save;
Fragment_student fragment;
public StudentList_adapter(Context context, ArrayList<StudentList> st,Fragment_student fragment) {
this.context=context;
inflater=LayoutInflater.from(context);
save=st;
this.fragment=fragment;
}
@override
public int getCount() {
// Log.d("SIZE",save.size()+"");
return save.size();
}
@override
public long getItemId(int i) {
return 0;
}
@override
public View getView(int i, View view, ViewGroup viewGroup) {
final ViewHolder viewHolder;


if(view==null)
{
view = inflater.inflate(R.layout.studentlist1, null);
viewHolder=new ViewHolder();
viewHolder.tv_Image=(CircleImageView) view.findViewById(R.id.image_student);
// viewHolder.progressBar=(ProgressBar)view.findViewById(R.id.progressBar);
viewHolder.tv_name=(TextView)view.findViewById(R.id.name_student);
// viewHolder.tv_class=(TextView)view.findViewById(R.id.class_student);
viewHolder.tv_Roll=(TextView)view.findViewById(R.id.roll_student);
viewHolder.tv_Email=(TextView)view.findViewById(R.id.email_student);
viewHolder.tv_address=(TextView)view.findViewById(R.id.address);
// viewHolder.tv_section=(TextView)view.findViewById(R.id.section_student);
viewHolder.button = (ImageButton) view.findViewById(R.id.pp);

view.setTag(viewHolder);
}
else {

viewHolder=(ViewHolder)view.getTag();

}

viewHolder.tv_name.setText(save.get(i).getName());
// Log.d("Name ssdsds",save.get(i).getName());
// viewHolder.tv_class.setText(save.get(i).getJsonMemberClass()+" ");
viewHolder.tv_Roll.setText(save.get(i).getRollno());
viewHolder.tv_address.setText(save.get(i).getAddress());
//viewHolder.tv_a.setText(save.get(i).getAddresses());
// viewHolder.tv_section.setText(save.get(i).getSection());
// viewHolder.progressBar.setVisibility(View.VISIBLE);
viewHolder.tv_Image.setVisibility(View.VISIBLE);

Picasso.with(context)
.load(save.get(i).getImage())
.placeholder(context.getResources().getDrawable(R.drawable.ic_school))
.fit()
.centerCrop()
.into(viewHolder.tv_Image);

// if(a==new SessionManager(fragment.getActivity()).getUserDetails().get(SessionManager.KEY_ID)){}
if (new SessionManager(fragment.getContext()).getUserDetails()
.get(SessionManager.KEY_EMAIL).equals(save.get(i).getEmail())) {
viewHolder.button.setVisibility(View.VISIBLE);

}
else {
viewHolder.button.setVisibility(View.INVISIBLE);
}




return view;
}

@override
public Object getItem(int i) {
return save.get(i);
}
private static class ViewHolder {
CircleImageView tv_Image;
TextView tv_Roll;
TextView tv_name;
TextView tv_Email;
TextView tv_class;
TextView tv_address;
TextView tv_section;
TextView tv_phone;
ImageButton button;
// ProgressBar progressBar ;
}



}



studentlist1.xml

<?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="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:eek:rientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_marginTop="@dimen/_10sdp"
android:layout_marginBottom="@dimen/_10sdp"
android:weightSum="1"
android:background="@color/ColorWhite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:eek:rientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/image_student"
android:layout_width="@dimen/staff"
android:layout_height="@dimen/staff"
android:layout_gravity="center"
android:layout_marginStart="@dimen/_1sdp"
android:src="@drawable/ic_school"
android:layout_alignParentLeft="true">
</de.hdodenhof.circleimageview.CircleImageView>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:eek:rientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:eek:rientation="horizontal"
android:weightSum="1">

<com.schoolappniftysol.UI.OpenSenseTextView
android:id="@+id/name_student"
style="@Style/fp_studentdetail"
fontPath="fonts/OpenSans-Regular.ttf"
android:layout_width="match_parent"
android:layout_height="@dimen/_30sdp"
android:layout_marginTop="@dimen/_5sdp"
android:layout_weight="1"
android:singleLine="true"
android:background="@drawable/patti"
android:paddingStart="@dimen/_20sdp"
android:paddingEnd="@dimen/_5sdp"
android:paddingTop="@dimen/_5sdp"
android:text="Hello"
android:textColor="@color/ColorDarkGray"
android:textSize="@dimen/textsize" />


<com.schoolappniftysol.UI.OpenSenseTextView
android:id="@+id/roll_student"
style="@Style/fp_studenttitle"
fontPath="fonts/OpenSans-Regular.ttf"
android:layout_width="@dimen/_38sdp"
android:layout_height="@dimen/_38sdp"
android:layout_gravity="center"
android:background="@drawable/circle_textview"
android:padding="@dimen/_10sdp"
android:text="234"
android:gravity="center"
android:textSize="@dimen/_10sdp"
android:singleLine="true"
android:textAlignment="center"
android:textColor="@color/ColorWhite" />

<ImageButton
android:id="@+id/pp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/ic_dot"
android:visibility="invisible" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/_70sdp">

<TextView
android:id="@+id/email_student"
style="@Style/fp_studentdetail"
android:layout_width="match_parent"
android:layout_marginStart="@dimen/_20sdp"
android:singleLine="false"
android:text=""
android:textColor="@color/tt"
android:textSize="@dimen/textsize" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/_70sdp">

<TextView
android:id="@+id/Address"
style="@Style/fp_studentdetail"
android:layout_width="match_parent"
android:layout_marginStart="@dimen/_20sdp"
android:singleLine="false"
android:text=""
android:textColor="@color/tt"
android:textSize="@dimen/textsize" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/_70sdp">

</LinearLayout>
</LinearLayout>

</LinearLayout>

</LinearLayout>


PLEASE HELP !!!
 
You got a null pointer exception at line 76. Where is line 76 in your code?
Please use [code][/code] tags to enclose your code.

sorry , do not know how to use the tags but the Line 76 is the big font size below:

Code:
package com.schoolappniftysol.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.schoolappniftysol.Bean.StudentList;
import com.schoolappniftysol.Fragment.Fragment_student;
import com.schoolappniftysol.R;
import com.schoolappniftysol.SessionManager;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by admin on 3/8/2017.
 */

public class StudentList_adapter extends BaseAdapter {
    Context context;
    LayoutInflater inflater;
    ArrayList<StudentList> save;
    Fragment_student fragment;
    public StudentList_adapter(Context context, ArrayList<StudentList> st,Fragment_student fragment) {
        this.context=context;
        inflater=LayoutInflater.from(context);
        save=st;
        this.fragment=fragment;
    }
    [USER=1021285]@override[/USER]
    public int getCount() {
       // Log.d("SIZE",save.size()+"");
        return save.size();
    }
    [USER=1021285]@override[/USER]
    public long getItemId(int i) {
        return 0;
    }
    [USER=1021285]@override[/USER]
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder viewHolder;


        if(view==null)
        {
            view = inflater.inflate(R.layout.studentlist1, null);
            viewHolder=new ViewHolder();
            viewHolder.tv_Image=(CircleImageView) view.findViewById(R.id.image_student);
            //  viewHolder.progressBar=(ProgressBar)view.findViewById(R.id.progressBar);
            viewHolder.tv_name=(TextView)view.findViewById(R.id.name_student);
           // viewHolder.tv_class=(TextView)view.findViewById(R.id.class_student);
            viewHolder.tv_Roll=(TextView)view.findViewById(R.id.roll_student);
            viewHolder.tv_Email=(TextView)view.findViewById(R.id.email_student);
            viewHolder.tv_address=(TextView)view.findViewById(R.id.address);
           // viewHolder.tv_section=(TextView)view.findViewById(R.id.section_student);
            viewHolder.button = (ImageButton) view.findViewById(R.id.pp);

            view.setTag(viewHolder);
        }
        else {

            viewHolder=(ViewHolder)view.getTag();

        }

        viewHolder.tv_name.setText(save.get(i).getName());
     //   Log.d("Name ssdsds",save.get(i).getName());
      //  viewHolder.tv_class.setText(save.get(i).getJsonMemberClass()+" ");
        viewHolder.tv_Roll.setText(save.get(i).getRollno());
        viewHolder.tv_address.setText(save.get(i).getAddress());
        //viewHolder.tv_a.setText(save.get(i).getAddresses());
       // viewHolder.tv_section.setText(save.get(i).getSection());
//        viewHolder.progressBar.setVisibility(View.VISIBLE);
        viewHolder.tv_Image.setVisibility(View.VISIBLE);

        Picasso.with(context)
                .load(save.get(i).getImage())
                .placeholder(context.getResources().getDrawable(R.drawable.ic_school))
                .fit()
                .centerCrop()
                .into(viewHolder.tv_Image);

          //  if(a==new SessionManager(fragment.getActivity()).getUserDetails().get(SessionManager.KEY_ID)){}
            if (new SessionManager(fragment.getContext()).getUserDetails()
                    .get(SessionManager.KEY_EMAIL).equals(save.get(i).getEmail())) {
            viewHolder.button.setVisibility(View.VISIBLE);

        }
        else {
                viewHolder.button.setVisibility(View.INVISIBLE);
            }




        return view;
    }

    [USER=1021285]@override[/USER]
    public Object getItem(int i) {
        return save.get(i);
    }
    private static class  ViewHolder {
        CircleImageView tv_Image;
        TextView tv_Roll;
        TextView tv_name;
        TextView tv_Email;
        TextView tv_class;
        TextView tv_address;
        TextView tv_section;
        TextView tv_phone;
        ImageButton button;
        // ProgressBar progressBar ;
    }



}
 
Upvote 0
So in your XML layout you have

Code:
android:id="@+id/Address"

But in your code you have

Code:
viewHolder.tv_address=(TextView)view.findViewById(R.id.address);

Which I believe should be

Code:
viewHolder.tv_address=(TextView)view.findViewById(R.id.Address);

Spot the difference.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones