Click here to Skip to main content
15,889,874 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Now just trying to create an android application to login using eclipse. But when i run the code getting the above mentioned error.Whil lookin at logcat it shows the error in line 36 but no idea for me to how to recover it,

here is my code:

Java
package com.example.login;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
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;
import android.app.Activity;

public class MainActivity extends ActionBarActivity {
    EditText un,pw;
    TextView error;
    Button ok;
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pw=(EditText)findViewById(R.id.et_pw);
        ok=(Button)findViewById(R.id.btn_login);
        error=(TextView)findViewById(R.id.tv_error);

        ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
                postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));

                String response = null;
                try {
                    response = CustomHttpClient.executeHttpPost("<target page url>", postParameters);
                    String res=response.toString();
                    res= res.replaceAll("\\s+","");
                    if(res.equals("1"))
                        error.setText("Correct Username or Password");
                    else
                        error.setText("Sorry!! Incorrect Username or Password");
                } catch (Exception e) {
                    un.setText(e.toString());
                }

            }
        });
    }
}

and here is my logcat:
04-04 11:14:26.657: E/AndroidRuntime(382): FATAL EXCEPTION: main
04-04 11:14:26.657: E/AndroidRuntime(382): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.login/com.example.login.MainActivity}: java.lang.NullPointerException
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.app.ActivityThread.access$500(ActivityThread.java:122)
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.os.Looper.loop(Looper.java:132)
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.app.ActivityThread.main(ActivityThread.java:4123)
04-04 11:14:26.657: E/AndroidRuntime(382):  at java.lang.reflect.Method.invokeNative(Native Method)
04-04 11:14:26.657: E/AndroidRuntime(382):  at java.lang.reflect.Method.invoke(Method.java:491)
04-04 11:14:26.657: E/AndroidRuntime(382):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
04-04 11:14:26.657: E/AndroidRuntime(382):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
04-04 11:14:26.657: E/AndroidRuntime(382):  at dalvik.system.NativeStart.main(Native Method)
04-04 11:14:26.657: E/AndroidRuntime(382): Caused by: java.lang.NullPointerException
04-04 11:14:26.657: E/AndroidRuntime(382):  at com.example.login.MainActivity.onCreate(MainActivity.java:36)
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.app.Activity.performCreate(Activity.java:4397)
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
04-04 11:14:26.657: E/AndroidRuntime(382):  ... 11 more


Its appreciable if i get an solution to this issue.

Thanks in advance.

code blocks fixed
Posted
Updated 5-Apr-14 10:50am
v2

Might be a good idea to initialise un?


Java
// un declared
   EditText un,pw;
    TextView error;
    Button ok;
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Initialisation of un missing?
        pw=(EditText)findViewById(R.id.et_pw);
        ok=(Button)findViewById(R.id.btn_login);
        error=(TextView)findViewById(R.id.tv_error);
 
        ok.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                ArrayList<namevaluepair> postParameters = new ArrayList<namevaluepair>();
                // un referenced prior to initialisation
                postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
                postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));
 </namevaluepair></namevaluepair>
 
Share this answer
 
You should learn to read the logging and to debug your Android code.

Caused by: java.lang.NullPointerException
04-04 11:14:26.657: E/AndroidRuntime(382):  at com.example.login.MainActivity.onCreate(MainActivity.java:36)


I'd be taking a closer look at line 36.
And I wouldn't wonder if that is the username you're asking for and the object un, which never gets initalized, is null at that point.

Which IDE are your using?
Eclipse is prefered. it's free. GET IT!

http://developer.android.com/tools/debugging/debugging-projects.html[^]

Also please switch on line numbering in the eclipse-preferences (search there for "Text Editor" -> Show Line numbers)
 
Share this answer
 
Comments
[no name] 4-Apr-14 5:40am    
i'm using eclipse only....and this is the line " ok.setOnClickListener(new View.OnClickListener() {" which causing error but dont know how to change
TorstenH. 4-Apr-14 5:54am    
is the button "ok" initalized?

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