Click here to Skip to main content
15,886,789 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
click(view v) is throwing a null pointer exception. have been trying to sort it for hours. but to no avail. I would appriciate if anyone could help me sort this issue.

Thanks for you time.

Java
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends ActionBarActivity
{
   ankita db1 = null;
	
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      if (savedInstanceState == null)
      {
         getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
			
         //add();
      }

      //TextView txt1 = (TextView)findViewById(R.id.userName);
      //TextView txt2 = (TextView)findViewById(R.id.password);
		
      db1 = new ankita(this);
      Log.e("error", "before db create");
      db1.CreateDb();
      Log.e("error","after db create");
      add();
      //db1.insertrecord();
   }

public void add()
{
   Log.e("hello", "entered db insert");
   // TODO Auto-generated method stub
   Button addb= (Button) findViewById(R.id.add);
   try
   {
      addb.setOnClickListener(new View.OnClickListener()
      {
         @Override
         public void onClick(View v)
         {
            Log.e("hello", "entered db click");
            // TODO Auto-generated method stub
            TextView txt1 = (TextView) findViewById(R.id.userName);
            EditText txt = (EditText) findViewById(R.id.userName);
			
            TextView txt2 = (TextView) findViewById(R.id.password);
            Log.e("hello", "entered db insert1");
            String uname = (String) txt1.getText();
            Log.e("hello", "entered db insert2");
            String pass = (String) txt2.getText();
            Log.e("hello", "entered db insert3");
            db1.insertrecord(uname,pass);
			
         }
      });}
   catch(Exception e)
   {
      System.out.println("helloji"+e);
   }
}


code block corrected and tabulation reduced
Posted
Updated 23-Apr-14 0:06am
v3
Comments
Herman<T>.Instance 22-Apr-14 2:56am    
did you debug? which line throw the NullRef?
Nelek 23-Apr-14 6:07am    
I am not sure if it is a copy-paste problem with the code snippet. But I would say you are missing a bracket.

1 solution

TextView txt1 = (TextView)	findViewById(R.id.userName);
			EditText txt = (EditText) findViewById(R.id.userName);

Both use the same id. Probably
EditText txt
is null. use the correct id to fix it.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900