Java ignore NullPointerException in database? -


i new java coming c#. been trying figure out, can't right.

so let's have table following contents:

_id | body                       | partner_jid | bin_id ---------------------------------------------------------- 1   | hello                      | jid_2933219 | group_30 2   | null                       | jid_2933219 | group_30 3   | !cmd                       | jid_2933219 | group_30 4   |                         | jid_2933219 | group_30 5   | example                    | jid_2933219 | group_30 

okay, needs happen, row body gets scanned text starts "!cmd" (and if gets found, something). but, once comes down number 2, program quits , returns nullpointerexception.

i have this:

import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.statement;  public class main {      public static void main(string[] args) {         try {             class.forname("org.sqlite.jdbc");             system.out.println("load driver success");              connection connection = drivermanager.getconnection("jdbc:sqlite:data.db");              string query = "select * messagestable";             statement statement = connection.createstatement();             resultset result = statement.executequery(query);              while(result.next()) {                 int msgid = result.getint("_id");                 string msgbody = result.getstring("body");                 string usrid = result.getstring("partner_jid");                 string grpid = result.getstring("bin_id");                 if(grpid.equals("group_30")){                     system.out.println(msgid);                     system.out.println(msgbody.tolowercase().trim());                     system.out.println(usrid);                     if(msgbody.contains("!cmd")){                         system.out.println("command found!");                     }                 }             }             } catch (exception e) {             system.out.println("read file error");             e.printstacktrace();         }     } } 

log:

1 hello jid_2933219 read file error java.lang.nullpointerexception     @ dysanix.message.scanner.main.main(main.java:26) 

how make ignore nullpointerexception , continue next one?

if(msgbody.contains("!cmd")){    system.out.println("command found!"); } 

if remove line, doesn't give nullpointerexception , prints whole table should, need make when !cmd gets found, how go around doing this?

thanks in advance!!

use boolean , (&&) check null before checking if contains string. like

if (msgbody != null && msgbody.contains("!cmd")) {     // ... } 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -