0
votes

I am trying to retrieve data from a database. It's showing an error for display1.setOnClickListener(this); Please anyone suggest a solution to me.

@Override

protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub

    super.onCreate(savedInstanceState);

    display1=(Button)findViewById(R.id.button1);

    inputid=(EditText)findViewById(R.id.display);

    display1.setOnClickListener(this);
}

public void onClick(View v) {

    // TODO Auto-generated method stub

    int id=Integer.parseInt(inputid.getText().toString());

    DBAdapter dbadapter=new DBAdapter(this);

    Cursor c=dbadapter.display(id);

    Toast.makeText(this, "Name: " + c.getString(1) , Toast.LENGTH_LONG).show();

}

LOGCAT is

12-26 16:27:26.438: D/AndroidRuntime(31283): Shutting down VM 12-26 16:27:26.438: W/dalvikvm(31283): threadid=1: thread exiting with uncaught exception (group=0x40015560) 12-26 16:27:26.448: E/AndroidRuntime(31283): FATAL EXCEPTION: main 12-26 16:27:26.448: E/AndroidRuntime(31283): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.database/com.android.database.Display}: java.lang.NullPointerException 12-26 16:27:26.448: E/AndroidRuntime(31283): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 12-26 16:27:26.448: E/AndroidRuntime(31283): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 12-26 16:27:26.448: E/AndroidRuntime(31283): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 12-26 16:27:26.448: E/AndroidRuntime(31283): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 12-26 16:27:26.448: E/AndroidRuntime(31283): at android.os.Handler.dispatchMessage(Handler.java:99) 12-26 16:27:26.448: E/AndroidRuntime(31283): at android.os.Looper.loop(Looper.java:123) 12-26 16:27:26.448: E/AndroidRuntime(31283): at android.app.ActivityThread.main(ActivityThread.java:3683) 12-26 16:27:26.448: E/AndroidRuntime(31283): at java.lang.reflect.Method.invokeNative(Native Method) 12-26 16:27:26.448: E/AndroidRuntime(31283): at java.lang.reflect.Method.invoke(Method.java:507) 12-26 16:27:26.448: E/AndroidRuntime(31283): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 12-26 16:27:26.448: E/AndroidRuntime(31283): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 12-26 16:27:26.448: E/AndroidRuntime(31283): at dalvik.system.NativeStart.main(Native Method) 12-26 16:27:26.448: E/AndroidRuntime(31283): Caused by: java.lang.NullPointerException 12-26 16:27:26.448: E/AndroidRuntime(31283): at com.android.database.Display.onCreate(Display.java:23)****error here 12-26 16:27:26.448: E/AndroidRuntime(31283): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 12-26 16:27:26.448: E/AndroidRuntime(31283): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 12-26 16:27:26.448: E/AndroidRuntime(31283): ... 11 more 12-26 16:27:28.448: I/Process(31283): Sending signal. PID: 31283 SIG: 9

3
paste the complete code with error logPratik
Yes Pratik is right, what is there in display method, how can we suggest you a solution without knowing full code with log?Paresh Mayani

3 Answers

1
votes

You haven't set the content view so display1 will be null. Your onCreate needs something like this:

setContentView(R.layout.main);

It needs to be called before you try and access views.

Hope this helps.

0
votes

you need to set the view context in DBAdapter instead of this or you want to set the this then use the ActivityName.this like this

DBAdapter dbadapter=new DBAdapter(ActivityName.this);

or

DBAdapter dbadapter=new DBAdapter(v.getContext);

same way into the Toast.makeText() first argument

0
votes

I have got the solution. i just forgot to put layout for my activity.