Click here to Skip to main content
14,335,481 members
Rate this:
Please Sign up or sign in to vote.
See more:
help me please
Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference at Main3Activity.ReCount(Main3Activity.java:650)

My ListViewAdapter

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter{
    public ArrayList<HashMap<String, String>> list;
    Activity activity;
    public static final String LineID="LineID";
    public static final String ItemID="ItemID";
    public static final String LineM="LineM";
    public static final String ItemName="ItemName";
    public static final String TextQty="TextQty";
    public static final String TextSubQty="TextSubQty";
    public static final String TextPrice="TextPrice";
    public static final String Total_COLUMN="Total_COLUMN";
    public static final String SUintID="SUintID";
    public static final String UintTwoID="UintTwoID";
    public static final String UintTwoPrice="UintTwoPrice";
    public static final String Add_Units_Count="Add_Units_Count";
    public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list){
        super();
        this.activity=activity;
        this.list=list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    private class ViewHolder{
        TextView TextLineID;
        TextView TextItemID;
        TextView textLineM;
        TextView TextItemName;
        TextView TextTextQty;
        TextView TextSubQty;
        TextView TextPrice;
        TextView TextItemTotal;
        TextView TextUintID;
        TextView UintTwoID;
        TextView UintTwoPrice;
        TextView Add_Units_Count;
    }

    @Override
    public View getView(int position,View convertView, ViewGroup parent) {
        ViewHolder holder;
        LayoutInflater inflater = activity.getLayoutInflater();

        if(convertView == null){
            convertView=inflater.inflate(R.layout.datagridview, null);
            holder=new ViewHolder();
            holder.TextLineID = (TextView) convertView.findViewById(R.id.TextLineID);
            holder.TextItemID = (TextView) convertView.findViewById(R.id.TextItemID);
            holder.textLineM = (TextView) convertView.findViewById(R.id.TextLineM);
            holder.TextItemName = (TextView) convertView.findViewById(R.id.ItemName);
            holder.TextTextQty = (TextView) convertView.findViewById(R.id.TextQty);
            holder.TextSubQty = (TextView) convertView.findViewById(R.id.TextSubQty);
            holder.TextPrice = (TextView) convertView.findViewById(R.id.TextPrice);
            holder.TextItemTotal = (TextView) convertView.findViewById(R.id.TextItemTotal);
            holder.TextUintID = (TextView) convertView.findViewById(R.id.TextUintID);
            holder.UintTwoID = (TextView) convertView.findViewById(R.id.UintTwoID);
            holder.UintTwoPrice = (TextView) convertView.findViewById(R.id.UintTwoPrice);
            holder.Add_Units_Count = (TextView) convertView.findViewById(R.id.Add_Units_Count);
            convertView.setTag(holder);
            convertView.setId(position);
        }else{
          holder=(ViewHolder)
         convertView.getTag();
        convertView.setId(position);
        }

        HashMap<String, String> map=list.get(position);
        holder.TextLineID.setText(map.get(LineID));
        holder.TextItemID.setText(map.get(ItemID));
        holder.textLineM.setText(map.get(LineM));
        holder.TextItemName.setText(map.get(ItemName));
        holder.TextTextQty.setText(map.get(TextQty));
        holder.TextSubQty.setText(map.get(TextSubQty));
        holder.TextPrice.setText(map.get(TextPrice));
        holder.TextItemTotal.setText(map.get(Total_COLUMN));
        holder.TextUintID.setText(map.get(SUintID));
        holder.UintTwoID.setText(map.get(UintTwoID));
        holder.UintTwoPrice.setText(map.get(UintTwoPrice));
        holder.Add_Units_Count.setText(map.get(Add_Units_Count));
        return convertView;
    }
}


and my on create


final ArrayList<HashMap<String, String>> list = new  ArrayList<HashMap<String, String>>();
public static final String LineID = "LineID";
public static final String ItemID = "ItemID";
public static final String LineM = "LineM";
public static final String ItemName = "ItemName";
public static final String TextQty = "TextQty";
public static final String TextSubQty = "TextSubQty";
public static final String TextPrice = "TextPrice";
public static final String Total_COLUMN = "Total_COLUMN";
public static final String SUintID = "SUintID";
public static final String UintTwoID = "UintTwoID";
public static final String UintTwoPrice = "UintTwoPrice";
public static final String Add_Units_Count = "Add_Units_Count";

protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.clientinvoices);
 this.setTitle("فاتورة مبيعات");
 EditText InvID = (EditText) findViewById(R.id.InvoiceIDValue);
 spinnerClientName = (SearchableSpinner) findViewById(R.id.ClientNameValue);
 spinnerStoreName = (SearchableSpinner) findViewById(R.id.StoreNameValue);
 spinnerItemName = (SearchableSpinner) findViewById(R.id.ITemNameValue);
 ClientNameS = (TextView) findViewById(R.id.ClientNameLebal);
 final EditText ItemCodeValueA = (EditText) findViewById(R.id.ItemCodeValue);
 EditText ItemCodeS = (EditText) findViewById(R.id.ItemCodeValue);
 ItemCodeS.setFocusableInTouchMode(true);
 ItemCodeS.requestFocus();
 final ListView listView = (ListView) findViewById(R.id.datagridview1);
 HashMap<String, String> hashmap = new HashMap<String, String>();
 ListViewAdapter adapter = new ListViewAdapter(this, list);
 listView.setAdapter(adapter);
}


And my recount

private void ReCount() {
 ListView listView1 = findViewById(R.id.datagridview1);
 double GetTotalValue = 0;
 for (int i = 0; i < listView1.getCount(); i++) {
try {
  TextView LineID = (TextView) listView1.getChildAt(i).findViewById(R.id.TextLineID);
  TextView LineM = (TextView) listView1.getChildAt(i).findViewById(R.id.TextLineM);
  TextView ItemID = (TextView) listView1.getChildAt(i).findViewById(R.id.TextItemID);
  TextView ItemName = (TextView) listView1.getChildAt(i).findViewById(R.id.ItemName);
  TextView Qty = (TextView) listView1.getChildAt(i).findViewById(R.id.TextQty);
  TextView SubQty = (TextView) listView1.getChildAt(i).findViewById(R.id.TextSubQty);
  TextView Price = (TextView) listView1.getChildAt(i).findViewById(R.id.TextPrice);
  TextView ItemTotal = (TextView) listView1.getChildAt(i).findViewById(R.id.TextItemTotal);
  TextView TUintTwoID = (TextView) listView1.getChildAt(i).findViewById(R.id.UintTwoID);
  TextView UintTwoPriceV = (TextView) listView1.getChildAt(i).findViewById(R.id.UintTwoPrice);
  TextView Add_Units_CountASDV = (TextView) listView1.getChildAt(i).findViewById(R.id.Add_Units_Count);

  double ergebnis = 0;
  double a = 0;
  double b = 0 ;
   if (String.valueOf(Qty.getText().toString()) != null && !Qty.getText().equals("0") && !Qty.getText().equals("null")){
   if (String.valueOf(Price.getText().toString()) != null && !Price.getText().equals("0") && !Price.getText().equals("null")){
  a = Double.parseDouble(Qty.getText().toString());
  b =  Double.parseDouble(Price.getText().toString());
  }else{a = 0;b = 0;}
  }else{a = 0;b = 0;}

  ergebnis = a * b;
  double ergebnisTwo = 0;
  double Suba = 0 ;
  double Subb = 0 ;
   if (String.valueOf(SubQty.getText().toString()) != null && !SubQty.getText().equals("0") && !SubQty.getText().equals("null")){
   if (String.valueOf(SubQty.getText().toString()) != null && !SubQty.getText().equals("0") && !SubQty.getText().equals("null")){
    Suba = Double.parseDouble(SubQty.getText().toString());
    Subb = Double.parseDouble(SubQty.getText().toString());
   }else{Suba = 0;Subb = 0;}
  }else{Suba = 0;Subb = 0;}

  if (Double.parseDouble(TUintTwoID.getText().toString()) != 0) {
   ergebnisTwo = Suba * Subb;
  }else{ergebnisTwo = 0;}

  double ergebnisThree = ergebnis + ergebnisTwo;
  ItemTotal.setText(String.valueOf(ergebnisThree));
  GetTotalValue += ergebnisThree;
}catch (Exception ex){ ex.printStackTrace();}
}

}


What I have tried:

i have list view for sales item
and i'm always get error about null 
on row line number 9 until the end
before number 9 nothing and get no error
and i need to let you know i use the same item

i don't know how to fix this problem
Posted
Updated 5-Apr-19 23:49pm
Comments
Richard MacCutchan 5-Apr-19 11:21am
   
You did not tell us which one is line 65. You need to find out why you are getting a null reference and correct the code so it does not occur.
Mostafa Salama S 6-Apr-19 2:25am
   
the line 650 is
TextView LineID = (TextView) listView1.getChildAt(i).findViewById(R.id.TextLineID);  TextView LineM = (TextView) listView1.getChildAt(i).findViewById(R.id.TextLineM);  TextView ItemID = (TextView) listView1.getChildAt(i).findViewById(R.id.TextItemID);  TextView ItemName = (TextView) listView1.getChildAt(i).findViewById(R.id.ItemName);  TextView Qty = (TextView) listView1.getChildAt(i).findViewById(R.id.TextQty);  TextView SubQty = (TextView) listView1.getChildAt(i).findViewById(R.id.TextSubQty);  TextView Price = (TextView) listView1.getChildAt(i).findViewById(R.id.TextPrice);  TextView ItemTotal = (TextView) listView1.getChildAt(i).findViewById(R.id.TextItemTotal);  TextView TUintTwoID = (TextView) listView1.getChildAt(i).findViewById(R.id.UintTwoID);  TextView UintTwoPriceV = (TextView) listView1.getChildAt(i).findViewById(R.id.UintTwoPrice)

after i remove first line give me the same error on next line
David Crow 5-Apr-19 21:57pm
   
What does line #650 of Main3Activity.java look like? That's where your null object is being referenced.
Mostafa Salama S 6-Apr-19 2:26am
   
the line 650 is

TextView LineID = (TextView) listView1.getChildAt(i).findViewById(R.id.TextLineID); TextView LineM = (TextView) listView1.getChildAt(i).findViewById(R.id.TextLineM); TextView ItemID = (TextView) listView1.getChildAt(i).findViewById(R.id.TextItemID); TextView ItemName = (TextView) listView1.getChildAt(i).findViewById(R.id.ItemName); TextView Qty = (TextView) listView1.getChildAt(i).findViewById(R.id.TextQty); TextView SubQty = (TextView) listView1.getChildAt(i).findViewById(R.id.TextSubQty); TextView Price = (TextView) listView1.getChildAt(i).findViewById(R.id.TextPrice); TextView ItemTotal = (TextView) listView1.getChildAt(i).findViewById(R.id.TextItemTotal); TextView TUintTwoID = (TextView) listView1.getChildAt(i).findViewById(R.id.UintTwoID); TextView UintTwoPriceV = (TextView) listView1.getChildAt(i).findViewById(R.id.UintTwoPrice);
after i remove first line give me the same error on next line
David Crow 6-Apr-19 9:27am
   
That means that either listView1 is null or the return value from getChildAt(i) is null.
Richard MacCutchan 6-Apr-19 10:28am
   
I don’t understand what the second part of those statements is supposed to do.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 3

Create the New Project
Ex. listviewProject

activity_main.xml
Paste the Below Code

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:id="@+id/listview"
            android:visibility="gone"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
        <TextView
            android:id="@+id/NoData"
            android:text="No Data"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>


and Create the New XML File in android studio
right click on layout folder then click New then click Layout Resource File
Paste the below code

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/Clicked"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/b1"
            android:text="Loading..."
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout></per>

Then Creating the 4 class file in below
1) Tutorial
2) TutorialAdapter
3) TutorialData
4) TutorialHolder

1) Tutorial File Paste the Below Code
<pre>
   import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;

public class Tutorial{
    Context c;
    ListView listView;
    Activity a;
    TutorialAdapter chettingAdapter;
    TextView CNoData;

    public Tutorial(Context c, ListView listView, Activity a, TextView CNoData) {
        this.c = c;
        this.listView = listView;
        this.a = a;
        this.CNoData = CNoData;
    }
    public void getUpdate(ArrayList<TutorialData> chettingDatas)
    {
        if(chettingDatas.size()>0)
        {
            CNoData.setVisibility(View.GONE);
            listView.setVisibility(View.VISIBLE);
            chettingAdapter = new TutorialAdapter(c,a,chettingDatas);
            listView.setAdapter((ListAdapter)chettingAdapter);
        }
        else
        {
            CNoData.setVisibility(View.VISIBLE);
            CNoData.setText("No Data");
            listView.setVisibility(View.GONE);
        }
    }
}

</per>
2) TutorialAdapter File Paste the Below Code
<pre>
  import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Toast;
import java.util.ArrayList;

public class TutorialAdapter extends BaseAdapter {
    Context c;
    Activity a;
    ArrayList<TutorialData> chettingDatas;
    LayoutInflater layoutInflater;

    public TutorialAdapter(Context c, Activity a, ArrayList<TutorialData> chettingDatas) {
        this.c = c;
        this.a = a;
        this.chettingDatas = chettingDatas;
    }

    @Override
    public int getCount() {
        return chettingDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return chettingDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (layoutInflater == null) {
            layoutInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        if (convertView == null)
        {
            convertView= layoutInflater.inflate(R.layout.layout_tutorial,parent,false);
        }
        TutorialHolder Holder = new TutorialHolder(convertView);
        Holder.b1.setText(chettingDatas.get(position).getData());
        Holder.b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(c, "Clicked : "+chettingDatas.get(position).getData(), Toast.LENGTH_SHORT).show();
            }
        });
        return convertView;
    }
}

3) TutorialData File Paste the Below Code
  public class TutorialData {
    private String data;

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}

4) TutorialHolder File Paste the Below Code
public class TutorialHolder {
    Button b1;
    TutorialHolder(View v)
    {
        b1 = (Button)v.findViewById(R.id.b1);
    }
}


Then the Last Code is MainActivity.java paste the below code
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listview = (ListView)findViewById(R.id.listview);
        TextView NoData = (TextView)findViewById(R.id.NoData);
        ArrayList<TutorialData> chettingDatas= new ArrayList<>();
        TutorialData T = new TutorialData();
        T.setData("Hello");
        chettingDatas.add(T);
        TutorialData T1 = new TutorialData();
        T1.setData("Bro");
        chettingDatas.add(T1);
        TutorialData T2 = new TutorialData();
        T2.setData("Hi");
        chettingDatas.add(T2);
        TutorialData T3 = new TutorialData();
        T3.setData("Buy");
        chettingDatas.add(T3);
        Tutorial Tu = new Tutorial(MainActivity.this,listview,MainActivity.this,NoData);
        Tu.getUpdate(chettingDatas);
    }
}


Below Comment Box to i am sending the tested apk file send you can check the working...
Good Buy...
   
Comments
Pratishkumar Kushwaha 6-Apr-19 5:53am
   
https://drive.google.com/open?id=1La8_XYJbodMJg1wq3QQfoSup6t5yMe0M

This link is Google Drive so you can download the apk file look the working

I hop to solve you problem
Pratishkumar Kushwaha 7-Apr-19 6:50am
   
if you problem solve bro
Mostafa Salama S 7-Apr-19 6:52am
   
no :(
Mostafa Salama S 7-Apr-19 6:53am
   
https://imgur.com/5pakGqb

nothing hipping until row number 9
Pratishkumar Kushwaha 7-Apr-19 13:26pm
   
Can Explain the your actual which data will be display of the listview bro?
Mostafa Salama S 8-Apr-19 2:14am
   
i will display 11 line

the problem happen from line row number 9 i don't know why give me error none nothing before

i'm try to make sales invoice :(
Pratishkumar Kushwaha 10-Apr-19 1:31am
   
Can you try the above code bro
here is the one class of the TutorialData is a get the any data will be storing the TutorialData to you can passing the listview adapter so you can try the above code
and i am above link send of the drive to download the app and install in check the working bro

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100