0

there is a nullpointerexception error. error line is if (!temp4.equals(null)) {. I want to remove null data in db when I convert into an array. please help me.

String[] celement4array=new String[cc.getCount()];
int b=0;
for (cc.moveToFirst(); !cc.isAfterLast(); cc.moveToNext()) {
    String temp4 = cc.getString(cc.getColumnIndex("element4"));
    if (!temp4.equals(null)) {
        celement4array[b]=temp4;
        b++;
    }
}
Adi Inbar
  • 12,097
  • 13
  • 56
  • 69

1 Answers1

1

Use temp4 != null. Calling .equals() tries to use the object, which is null.

The == operator, as well as the != operator, returns true or false depending on the memory position of the two elements that are being compared. As you want to know if the object's memory position is inexistent (null), using the != operator is the way to go.

  • Thank you. I solved the nullpointerexception error. But I also want to reduce total length of array after if statement remove null elements. How can I do that? please teach me. – user2733182 Aug 30 '13 at 23:47
  • Ick. "memory position" should not be used in Java. But true otherwise. – user2246674 Aug 30 '13 at 23:47
  • Java or not, when talking about programming every pice of data is somewhere in memory. So the Strings are in memory, in a certain position. == just compares if the memory position is the same. –  Aug 30 '13 at 23:49
  • @user2733182 take a look at this question http://stackoverflow.com/questions/4870188/delete-item-from-array-and-shrink-array –  Aug 30 '13 at 23:51
  • @zyngawow There is *no discussion of "memory addresses" in the JLS*. I prefer to consider `object == object` is an *object identity comparison*. That is, it evaluates to true *iff* 1) both sides are `null` (i.e. evaluate to "no object") or 2) both sides evaluate to the same object. If you *must* go into detail, then talk about *references*, which are (unfortunately, IMOHO) used in the JLS to describe Java semantics. – user2246674 Aug 30 '13 at 23:55
  • @user2246674 Java still has pointers in the background, as the JVM is written in C(++?). –  Aug 30 '13 at 23:59
  • @zyngawow [There are no shortage of JVM implementations in various languages](http://en.wikipedia.org/wiki/List_of_Java_virtual_machines) (even though C/C++ are predominate, they are not required). Everything executed on a computer is executed via ML at some point - but it's just baggage to surface it. Why not talk about gates? – user2246674 Aug 31 '13 at 00:00
  • @user2246674 Off-topic: How do you write a Virtual Machine for a language that needs a Virtual Machine in the language that need the Virtual Machine? Edit: http://stackoverflow.com/questions/2279229/how-can-a-jvm-be-written-in-java –  Aug 31 '13 at 00:05
  • @zyngawow Why not talk about machine language? Gates/transistors? Electrons? Because it's not relevant. – user2246674 Aug 31 '13 at 00:15