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

Apps GridView application keeps crashing

sudheer

Lurker
I am new to android, my application keeps crashing with the following errors. I am using API Level 15.

01-26 19:50:45.820: E/AndroidRuntime(963): FATAL EXCEPTION: main
01-26 19:50:45.820: E/AndroidRuntime(963): java.lang.NullPointerException
01-26 19:50:45.820: E/AndroidRuntime(963): at my.grid.view.DataAdapter.getView(DataAdapter.java:62)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.widget.AbsListView.obtainView(AbsListView.java:2012)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.widget.GridView.makeAndAddView(GridView.java:1323)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.widget.GridView.makeRow(GridView.java:328)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.widget.GridView.moveSelection(GridView.java:885)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.widget.GridView.layoutChildren(GridView.java:1230)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.widget.GridView.setSelectionInt(GridView.java:1476)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.widget.GridView.arrowScroll(GridView.java:1729)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.widget.GridView.commonKey(GridView.java:1543)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.widget.GridView.onKeyDown(GridView.java:1494)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.view.KeyEvent.dispatch(KeyEvent.java:2551)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.view.View.dispatchKeyEvent(View.java:5500)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1242)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
01-26 19:50:45.820: E/AndroidRuntime(963): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1879)
01-26 19:50:45.820: E/AndroidRuntime(963): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1361)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.app.Activity.dispatchKeyEvent(Activity.java:2324)
01-26 19:50:45.820: E/AndroidRuntime(963): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1806)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3327)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.view.ViewRootImpl.handleFinishedEvent(ViewRootImpl.java:3300)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2460)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.os.Looper.loop(Looper.java:137)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-26 19:50:45.820: E/AndroidRuntime(963): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 19:50:45.820: E/AndroidRuntime(963): at java.lang.reflect.Method.invoke(Method.java:511)
01-26 19:50:45.820: E/AndroidRuntime(963): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-26 19:50:45.820: E/AndroidRuntime(963): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-26 19:50:45.820: E/AndroidRuntime(963): at dalvik.system.NativeStart.main(Native Method)
main.xml
--------
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:stretchMode="columnWidth"
android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="1"
android:clipChildren="true"
android:horizontalSpacing="3dip"
android:verticalSpacing="3dip" />


customgrid.xml
--------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TableLayout
android:id="@+id/TableLayout01"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TableRow
android:id="@+id/TableRow01"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:text="@string/hello"
android:layout_width="100dp"
android:layout_height="15dp"
android:textSize="12dp"
android:id="@+id/txtName" />
<TextView
android:text="@string/hello"
android:layout_width="35dp"
android:layout_height="15dp"
android:textSize="12dp"
android:id="@+id/txtId" />
<TextView
android:text="@string/hello"
android:layout_width="70dp"
android:layout_height="15dp"
android:textSize="12dp"
android:id="@+id/txtRate" />
</TableRow>
</TableLayout>
</LinearLayout>


GridViewDataActivity.java
-------------------------
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;

public class GridViewDataActivity extends Activity
{
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new DataAdapter(this));
}
}


DataAdapter.java
----------------
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class DataAdapter extends BaseAdapter
{
Context mContext;
private String[] name = { "Rohit", "Rahul", "Ravi", "Amit", "Arun", "Anil",
"Kashif", "Rohit1", "Rahul1", "Ravi1", "Amit1",
"Arun1", "Anil1", "Kashif1", "Rohit2", "Rahul2",
"Ravi2", "Amit2", "Arun2", "Anil2", "Kashif2" };
private String[] id = { "S001", "S002", "S003", "S004", "S005", "S006", "S007", "S0011", "S0021", "S0031", "S0041",
"S0051", "S0061", "S0071", "S0012", "S0022", "S0032", "S0042", "S0052", "S0062", "S0072" };
private String[] rate = { "2500.00", "3500.00", "1000.00", "500.00", "9000.00", "10000.00", "5000.00", "12500.00",
"13500.00", "11000.00", "1500.00", "19000.00", "110000.00", "15000.00", "22500.00", "23500.00",
"21000.00", "2500.00",
"29000.00", "210000.00", "25000.00" };
private LayoutInflater mInflater;

public DataAdapter( Context c )
{
mContext = c;
mInflater = LayoutInflater.from(c);
}

public int getCount()
{
return id.length;
}

public Object getItem( int position )
{
return position;
}

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

public View getView( int position, View convertView, ViewGroup parent )
{
ViewHolder holder = null;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.customgrid, parent, false);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.txtName);
holder.txtName.setGravity(Gravity.LEFT);
holder.txtId = (TextView) convertView.findViewById(R.id.txtId);
holder.txtId.setGravity(Gravity.LEFT);
holder.txtRate = (TextView) convertView.findViewById(R.id.txtRate);
holder.txtRate.setGravity(Gravity.RIGHT);
if (position == 0)
{
convertView.setTag(holder);
}
}
else
{
holder = (ViewHolder) convertView.getTag();
}
/******************************************/
/*** This line is giving the problem *****/
/******************************************/
holder.txtName.setText(name[position]);
holder.txtId.setText(id[position]);
holder.txtRate.setText(rate[position]);
return convertView;
}

static class ViewHolder
{
TextView txtName;
TextView txtId;
TextView txtRate;
}
}

Any idea what is causing the problem?
 
01-26 19:50:45.820: E/AndroidRuntime(963): java.lang.NullPointerException
01-26 19:50:45.820: E/AndroidRuntime(963): at my.grid.view.DataAdapter.getView(DataAdapter.java:62)

The first line "java.lang.NullPointerException" indicates that a null pointer was used.

The second line "my.grid.view.DataAdapter.getView" indicates the last method called.

We know that getView must return the view to the Grid, I believe you returned null.

Here is the google tutorial to the GridView: Grid View | Android Developers

If you can't find the bug, post the code of the method getView from the class DataAdapter.
 
The first line "java.lang.NullPointerException" indicates that a null pointer was used.

The second line "my.grid.view.DataAdapter.getView" indicates the last method called.

We know that getView must return the view to the Grid, I believe you returned null.

Here is the google tutorial to the GridView: Grid View | Android Developers

If you can't find the bug, post the code of the method getView from the class DataAdapter.



Hi,

Thanks for your reply. I have already posted the code of the method getView. It is under DataAdapter.java.

Regards.

Sudheer
 
Why you are just setting the tag if position is 0?

I believe that android is calling the getView for all itens, and you are setting the tag just in the first item, when android calls again getView for position 1, you try to get the tag from convertView, but you didn't called setTag for convertView 1, then getTag returns a null object, and you try to get the object txtName from a null object, and then the error occurs.
 
Why you are just setting the tag if position is 0?

I believe that android is calling the getView for all itens, and you are setting the tag just in the first item, when android calls again getView for position 1, you try to get the tag from convertView, but you didn't called setTag for convertView 1, then getTag returns a null object, and you try to get the object txtName from a null object, and then the error occurs.

Actually it is based on this example.

Display Data in GridView in Android - MindStick

The error shows only when the list starts scrolling and the highlight bar is moved up or down. If there are only few rows, i.e., scrolling is not required it works perfectly.
 
Yes, it is line 62, giving the error.


He means, what exactly is your code on that line (since we dont have your code with line numbers here on the forums)

When you look at line 62, it should be pretty clear what is null.

For example is you call a method on an object:

Code:
someObject.doSomething();
If code like this was on line 62, you would know that the problem is someObject is null


hth:)
 
Back
Top Bottom