Null pointer exception

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Null pointer exception

Postby janaomate » Wed Aug 06, 2008 8:29 am

hi to all,

i have one problem with my code. If I have two activities then the output of my contact details is shown n my 2nd activity. If i have three activities then the nullpointer exception will raise. what can i do....



thanks in advance....

This is my code...

PhContacts.java

package com.omate.android.PhoneContacts;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class PhContacts extends Activity
{
private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
Button AddContact=(Button)findViewById(R.id.contactadd);
AddContact.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent i=new Intent(PhContacts.this,AddContact.class);
startSubActivity(i,ACTIVITY_CREATE);
}
});
Button EditContact=(Button)findViewById(R.id.contactedit);
EditContact.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent i=new Intent(PhContacts.this,EditContact.class);
startSubActivity(i,ACTIVITY_EDIT);
}
});
}
}
AddContact.java

package com.omate.android.PhoneContacts;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class PhContacts extends Activity
{
private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
Button AddContact=(Button)findViewById(R.id.contactadd);
AddContact.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent i=new Intent(PhContacts.this,AddContact.class);
startSubActivity(i,ACTIVITY_CREATE);
}
});
Button EditContact=(Button)findViewById(R.id.contactedit);
EditContact.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent i=new Intent(PhContacts.this,EditContact.class);
startSubActivity(i,ACTIVITY_EDIT);
}
});
}
}

EditContact.java

package com.omate.android.PhoneContacts;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.EditText;


public class EditContact extends ListActivity
{
private final String MY_DATABASE_NAME = "Usercontact";
private final String MY_DATABASE_TABLE = "contacts";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.editcontact);
EditText et = new EditText(this);
et.setSelection(et.getText().length());
ArrayList<String> results = new ArrayList<String>();
SQLiteDatabase myDB = null;
try
{
//this.createDatabase(MY_DATABASE_NAME, 1, MODE_PRIVATE, null);
myDB = this.openDatabase(MY_DATABASE_NAME, null);
/*myDB.execSQL("CREATE TABLE IF NOT EXISTS "
+ MY_DATABASE_TABLE
+ " (name VARCHAR, age INT(3));");*/

Cursor c = myDB.query(true,MY_DATABASE_TABLE, new String[] {"name","mobileno"},"mobileno",null,null,null,null);

/* Get the indices of the Columns we will need */
int nameColumn = c.getColumnIndex("name");
int mobileColumn = c.getColumnIndex("mobileno");

/* Check if our result was valid. */
if (c != null)
{
/* Check if at least one Result was returned. */
if (c.first())
{
int i = 0;
/* Loop through all Results */
do
{
i++;
/* Retrieve the values of the Entry
* the Cursor is pointing to. */
String Name = c.getString(nameColumn);
long Mobileno = c.getLong(mobileColumn);
/* We can also receive the Name
* of a Column by its Index.
* Makes no sense, as we already
* know the Name, but just to shwo we can ;) */
String mobilenoColumName = c.getColumnName(mobileColumn);

/* Add current Entry to results. */
results.add("" + i + ": " + Name
+ "(" + mobilenoColumName+ ": " + Mobileno + ")");
} while (c.next());
}
}
}
catch(FileNotFoundException e)
{

}
finally {
if (myDB != null)
myDB.close();
}

this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
}
}
Always

thnks for every viewers
janaomate
Freshman
Freshman
 
Posts: 7
Joined: Wed Aug 06, 2008 6:30 am
Location: India

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 7 guests