2

I try to select only the files with same module to be displayed on the list view from an sql table. If I'm using if (module == smodule) the app is ok but the if statement is jumped, using if (module.equals(smodule)) the app is crasing.

Thanks @Nambari problem is that I had a null for the value of module in the database.

Thanks @Buhake Sindi for the suggestion 'Instantiate it like String module = ""'

Java code.................:

private void getFiles() {                               //getting data from the SQL database
    String[] columns = {"id", "file", "module", "date", "note"};
    database = helper.getWritableDatabase();
    Cursor cursor = database.query("files", columns, null, null, null, null, null);
    cursor.moveToFirst();
    files.clear();
    while (!cursor.isAfterLast()) {
        String module = cursor.getString(2);
        String smodule = "Alllllll";
        Log.v("test", smodule+" "+module+" <-if m=sm getting sql data /not if");
        if (module.equals(smodule)){
             String file = cursor.getString(1);
             files.add(file);
             Log.v("test", file+" "+files+" <-if m=sm getting sql data/if");
        }
    cursor.moveToNext();
    }
    cursor.close();
    helper.close();

Logcat:

Alllllll null <-if m=sm getting sql data not/if
Alllllll null <-if m=sm getting sql data not/if
Alllllll null <-if m=sm getting sql data not/if
Alllllll null <-if m=sm getting sql data not/if
Alllllll Alllllll <-if m=sm getting sql data not/if
Alllllll Alllllll <-if m=sm getting sql data not/if
Alllllll Alllllll <-if m=sm getting sql data not/if

Error log after replacing == with .equals():

03-28 14:31:03.985: E/SpannableStringBuilder(25898): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-28 14:31:03.985: E/SpannableStringBuilder(25898): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-28 14:31:15.030: D/AbsListView(25898): Get MotionRecognitionManager
03-28 14:31:15.085: W/ResourceType(25898): Failure getting entry for 0x010802c1 (t=7 e=705) in package 0 (error -75)
03-28 14:31:15.090: W/ResourceType(25898): Failure getting entry for 0x010802c1 (t=7 e=705) in package 0 (error -75)
03-28 14:31:15.225: E/SpannableStringBuilder(25898): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-28 14:31:15.225: E/SpannableStringBuilder(25898): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-28 14:31:28.950: D/AbsListView(26523): Get MotionRecognitionManager
03-28 14:31:29.060: D/libEGL(26523): loaded /system/lib/egl/libEGL_mali.so
03-28 14:31:29.075: D/libEGL(26523): loaded /system/lib/egl/libGLESv1_CM_mali.so
03-28 14:31:29.080: D/libEGL(26523): loaded /system/lib/egl/libGLESv2_mali.so
03-28 14:31:29.090: D/(26523): Device driver API match
03-28 14:31:29.090: D/(26523): Device driver API version: 10
03-28 14:31:29.090: D/(26523): User space API version: 10 
03-28 14:31:29.090: D/(26523): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Thu Oct 25 08:43:05 KST 2012 
03-28 14:31:29.115: D/OpenGLRenderer(26523): Enabling debug mode 0
03-28 14:31:29.135: W/ResourceType(26523): Failure getting entry for 0x010802c1 (t=7 e=705) in package 0 (error -75)
03-28 14:31:29.145: W/ResourceType(26523): Failure getting entry for 0x010802c1 (t=7 e=705) in package 0 (error -75)
03-28 14:31:30.780: D/AbsListView(26523): Get MotionRecognitionManager
03-28 14:31:30.800: V/test(26523): Alllllll null <-if m=sm getting sql data /not if
03-28 14:31:30.805: D/AndroidRuntime(26523): Shutting down VM
03-28 14:31:30.805: W/dalvikvm(26523): threadid=1: thread exiting with uncaught exception (group=0x4189f2a0)
03-28 14:31:30.810: E/AndroidRuntime(26523): FATAL EXCEPTION: main
03-28 14:31:30.810: E/AndroidRuntime(26523): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cn/com.example.cn.ListView2}: java.lang.NullPointerException
03-28 14:31:30.810: E/AndroidRuntime(26523):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
03-28 14:31:30.810: E/AndroidRuntime(26523):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
03-28 14:31:30.810: E/AndroidRuntime(26523):    at android.app.ActivityThread.access$700(ActivityThread.java:140)

end

Alexander
  • 23
  • 3

5 Answers5

4
if (module == smodule){

should be

  if (module.equals(smodule)){

While comparing String/Objects. It is better to use equals() than ==

== checks for reference equality. equals() checks for object equality.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • It's not only *better*, it's the only (correct) way to compare equality (and not only for Strings) –  Mar 28 '13 at 14:12
  • @a_horse_with_no_name: agree. But reason why I have better there is, when you compare String literals, == also works. – kosa Mar 28 '13 at 14:14
  • if I'm using `if (module.equals(smodule)){` is crashing the app – Alexander Mar 28 '13 at 14:24
  • @MV: Without error message, can't predict what is going on. Update question with error stack. – kosa Mar 28 '13 at 14:26
  • `==` is **not** guaranteed to work with String literals. You should avoid using that. –  Mar 28 '13 at 14:33
  • `03-28 14:31:30.800: V/test(26523): Alllllll null <-if m=sm getting sql data /not if` `03-28 14:31:30.805: D/AndroidRuntime(26523): Shutting down VM` `03-28 14:31:30.805: W/dalvikvm(26523): threadid=1: thread exiting with uncaught exception (group=0x4189f2a0)` `03-28 14:31:30.810: E/AndroidRuntime(26523): FATAL EXCEPTION: main` `03-28 14:31:30.810: E/AndroidRuntime(26523): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cn/com.example.cn.ListView2}: java.lang.NullPointerException` – Alexander Mar 28 '13 at 14:37
  • @MV: It seems NullPointerException. Please edit question and update full stack trace over there. Make sure "module" is not null. – kosa Mar 28 '13 at 14:41
2

That's because you are not evaluating the value of the String, instead you are comparing its object reference.

For String value comparison, use the equals() method, like so:

if (module.equals(smodule)) {

Seeing that smodule is never null and that module can be null, to avoid NullPointerException rather do:

if (smodule.equals(module)) {
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
1

use module.equals(smodule),

== vs .equals

  • == -> is a reference comparison ie both objects point to the same memory location

  • equals() -> evaluates to the comparison of values in the objects

  • it's .equals(...) though (first letter is not capitalized).

  • equals will only compare what it is written to compare, no more, no less.

  • if equals is not overridden, it defaults to ==; that is, it returns true if both variables refer to the same object.

  • Always remember to override hashCode if you override equals so as not to "break the contract".

Talha
  • 12,673
  • 5
  • 49
  • 68
0

Please use the .equals method and not == in Java. Tons of discussion already here.

Community
  • 1
  • 1
karmanaut
  • 628
  • 1
  • 6
  • 17
0
if (module == smodule)

should be

if (module.equals(smodule))

== compares object references.

equals() compares object contents.

kosa
  • 65,990
  • 13
  • 130
  • 167
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39