title: Null Pointer exception in Realm
body: <p>I have some functions, that save data 'Dialog' and 'Message' in realm DB. </p>
  
  <pre><code>private void addMessageToDialog(ObservableDialog dialog, DialogMessage dialog_msg) {
          // if i try to use FINDDIALOG - i have NPE errors
          rDialog rDialog  = findDialog(dialog.getChatId());
          realm.beginTransaction();
          rMessage rMessage = constructObjectRMessage(dialog_msg);
          realm.copyToRealm(rMessage);            
          if(rDialog==null) rDialog = constructObjectRDialog(dialog);
          rDialog.getDialogMessages().add(rMessage);
          realm.copyToRealmOrUpdate(rDialog);
          realm.commitTransaction();
      }
  private rDialog constructObjectRDialog(ObservableDialog dialog) {
          try {
              rDialog rDialog = new rDialog();
              rDialog.setChatId(dialog.getChatId());
              rDialog.setCreator(dialog.isCreator());
              rDialog.setNickname(dialog.getNickname());
              rDialog.setAvatarUrl(dialog.getAvatarUrl());
              rDialog.setPartnerOkId(dialog.getPartnerId());
              rDialog.setBanned(dialog.isBanned());
  
              List&lt;ChatStatus&gt; statusHistory = dialog.getStatusHistory();
              RealmList&lt;rChatStatus&gt; statuses = new RealmList&lt;&gt;();
              for (ChatStatus s : statusHistory) {
                  rChatStatus st = constuctorRChatCtatus(s.name());
                  realm.copyToRealm(st);
                  statuses.add(st);
              }
              rDialog.setStatusHistory(statuses);
  
              List&lt;DialogMessage&gt; dialogMessages = dialog.getMessages();
              RealmList&lt;rMessage&gt; messages = new RealmList&lt;&gt;();
              for (DialogMessage d : dialogMessages) {
                  rMessage m = constructObjectRMessage(d);
                  realm.copyToRealm(m);
                  messages.add(m);
              }
              rDialog.setDialogMessages(messages);
              return rDialog;
          } catch(Exception e){
              Log.e(TAG,e.getMessage());
          }
          return null;
      }
  
  
  private rDialog findDialog(String chatId) {
          RealmResults&lt;rDialog&gt; d = realm.where(rDialog.class)
              .equalTo("chatId", chatId)
              .findAll();             
          if(d.size()==0) return null;
          return d.first();
      }
  </code></pre>
  
  <p>When i use this code, it works only if i DON't USE <code>findDialog</code> (and <code>realm.where</code> method, and only use <code>constructObjectRDialog</code>).
  But when i try to use <code>findDialog</code> (in  <code>addMessageToDialog</code>) I have NPE error in</p>
  
  <pre><code> rDialog.getDialogMessages().add(rMessage);
  </code></pre>
  
  <p>Why can be such erros?
  PS - <code>rMessage</code> and <code>rDialog</code> are not null (when i check it in debugger)</p>
  
  <p>LOG:</p>
  
  <pre><code>02-28 11:43:21.220    6432-6460/com.OkLove.OkLoveDroid E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-468
      Process: com.OkLove.OkLoveDroid, PID: 6432
      java.lang.NullPointerException
              at io.realm.RealmList.add(RealmList.java:120)
              at com.OkLove.OkLoveDroid.BackgroundTasksHandlers.DbHandler.addMessageToDialog(DbHandler.java:98)
              at com.OkLove.OkLoveDroid.BackgroundTasksHandlers.DbHandler.handleMessage(DbHandler.java:77)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:136)
              at com.OkLove.OkLoveDroid.BackgroundTasksHandlers.DbHandlerThread.run(DbHandlerThread.java:30)
  </code></pre>