Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5967665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:58:37+00:00 2026-05-22T19:58:37+00:00

How to handle null when using Pattern.compile ? I’m using the following line to

  • 0

How to handle null when using Pattern.compile? I’m using the following line to compare strings:

Pattern.compile(Pattern.quote(s2), Pattern.CASE_INSENSITIVE).matcher(s1).find()

There are some cases where s1 can be null and obviously it throws NullPointerException. I know this could be handled by another if condition to s1, but I would like to know is there’s an alternate solution.

EDIT

Iterator iter = sampleList().iterator();
while (iter.hasNext()) {
    SampleObj so = (SampleObj) iter.next();
    if (!s1.equalsIgnoreCase("")) {
        if (Pattern.compile(Pattern.quote(s1), Pattern.CASE_INSENSITIVE).matcher(so.getS1()).find())
            match = true;
        else
            match = false;
    }
    if (!s3.equalsIgnoreCase("")) {
        if (Pattern.compile(Pattern.quote(s3), Pattern.CASE_INSENSITIVE).matcher(so.getS3()).find())
            match = true;
        else
            match = false;
    }
}

s1 and s3 are inputs which are matched over iterator.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:58:38+00:00Added an answer on May 22, 2026 at 7:58 pm

    Pattern.matcher() will always throw a NullPointerException when you pass in null, so: no, there is no other way, you’ll have to check for null explicitly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're using the following pattern to handle caching of universal objects for our asp.net
How could we handle null for a datetime field (got from SQL Server) in
I'm writing a Linux module and getting: Unable to handle kernel NULL pointer dereference
How does one handle a DateTime with a NOT NULL ? I want to
I've written a simple SessionItem management class to handle all those pesky null checks
Currently I'm using singleton pattern for certain global objects in my application (Qt application
I ran across this pattern in the code of a library I'm using. It
I'm wondering about the cost of using a try/exception to handle nulls compared to
I've seen an example implementation of the CoR pattern using a HashMap as the
I'm currently using an Open Session in View pattern in an ASP.NET WebForms application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.