-1

While moving from one activity to another it is showing a null pointer exception but I couldn't find where the error is happening.

giving my logcat below

10-04 12:14:10.876: E/AndroidRuntime(1966): FATAL EXCEPTION: main

10-04 12:14:10.876: E/AndroidRuntime(1966): java.lang.RuntimeException: Unable to start 
activity ComponentInfo{com.neochat/com.neochat.Displayfriendlist}: 
java.lang.NullPointerException

10-04 12:14:10.876: E/AndroidRuntime(1966):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)

10-04 12:14:10.876: E/AndroidRuntime(1966):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

10-04 12:14:10.876: E/AndroidRuntime(1966):     at android.app.ActivityThread.access$600(ActivityThread.java:141)

10-04 12:14:10.876: E/AndroidRuntime(1966):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)

10-04 12:14:10.876: E/AndroidRuntime(1966):     at android.os.Handler.dispatchMessage(Handler.java:99)

10-04 12:14:10.876: E/AndroidRuntime(1966):     at android.os.Looper.loop(Looper.java:137)

10-04 12:14:10.876: E/AndroidRuntime(1966):     at android.app.ActivityThread.main(ActivityThread.java:5041)

10-04 12:14:10.876: E/AndroidRuntime(1966):     at java.lang.reflect.Method.invokeNative(Native Method)

10-04 12:14:10.876: E/AndroidRuntime(1966):     at java.lang.reflect.Method.invoke(Method.java:511)

10-04 12:14:10.876: E/AndroidRuntime(1966):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

10-04 12:14:10.876: E/AndroidRuntime(1966):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

10-04 12:14:10.876: E/AndroidRuntime(1966):     at dalvik.system.NativeStart.main(Native Method)

10-04 12:14:10.876: E/AndroidRuntime(1966): Caused by: java.lang.NullPointerException

10-04 12:14:10.876: E/AndroidRuntime(1966):     at com.neochat.LoginDataBaseAdapter.Displayfriend(LoginDataBaseAdapter.java:110)

10-04 12:14:10.876: E/AndroidRuntime(1966):     at com.neochat.Displayfriendlist.onCreate(Displayfriendlist.java:25)

10-04 12:14:10.876: E/AndroidRuntime(1966):     at android.app.Activity.performCreate(Activity.java:5104)

10-04 12:14:10.876: E/AndroidRuntime(1966):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)

10-04 12:14:10.876: E/AndroidRuntime(1966):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

10-04 12:14:10.876: E/AndroidRuntime(1966):     ... 11 more

giving my java class below

public class DisplayFriendList extends Activity {

    Context context = this;
    LoginDataBaseAdapter Logi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.displayfriend);

        Logi = new LoginDataBaseAdapter(this);

        Bundle data = getIntent().getExtras();
        String code = data.getString("EMPCode");

        String gotfriend = Logi.Displayfriend(code);

        TextView txt = (TextView) findViewById(R.id.getfriend);
        txt.setText(gotfriend);

        Button btnadd = (Button) findViewById(R.id.data_addfriend);
        btnadd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // Logi.insertFriendEntry(String Name, String Emp_Code);

                Toast.makeText(context, "Friend added successfully",
                        Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        return super.onCreateOptionsMenu(menu);
    }

}

giving the function Displayfriend ..in LoginDataBaseAdapter class

public String DisplayFriend(String Emp_Code) {
    String displayfriend = "SELECT NAME , EMPLOYEE_CODE FROM EMPLOYEES " +  
              "WHERE " + " EMPLOYEE_CODE " + "LIKE '" +Emp_Code;
    Cursor display = db.rawQuery(displayfriend, null);
    String data = display.getString(0);
    return data;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
user2833559
  • 1
  • 1
  • 4

4 Answers4

0

the exception is raised by this line: String gotfriend=Logi.Displayfriend(code); because Logi var is equals to null. Logi is null cause there is a problem with its initialization, you should check Logi=new LoginDataBaseAdapter(this); and the constructor of LoginDataBaseAdapter to see if there are any errors.

If you post the code of the adapter and the second activity i'll try to be more precise.

Note: you should follow at least the "java good practice" naming your methods and vars. Non static method should start with lower case letter. Non final var should start with lower case letter.

0

i think the problem is in DisplayFriend method

before retriving the data from cursor check is that cursor is null or not if it's null it throughts exception. i think that's the problem.

android_dev
  • 1,477
  • 12
  • 18
  • yes i guess ur not checking thet emp_code is null or not and there is no data on the database with that emp_code then cursor returns null here ur not checking cursor null or not if cursor not null then u have to return the string..! then ur issue will be fixed – android_dev Oct 05 '13 at 07:30
  • Welcome then make it as fixed – android_dev Oct 05 '13 at 09:38
0

Which line is #110? if it is the first one (String display ...) then check Emp_Code. If it's the third (String data...) then use this:

if (display==null) return null; //or ""
if (!display.moveToFirst()) return null;
//the rest of your code
denispyr
  • 1,403
  • 3
  • 21
  • 34
0

Easy ways of solving such errors would be to look at the log cat.

LoginDataBaseAdapter.Displayfriend(LoginDataBaseAdapter.java:110)

double click on this line and you will be redirected to the line shown, based on the code, determine what might cause a null pointer. Either the object is not instantiated, or else the database does not return any data.