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 7413751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:57:11+00:00 2026-05-29T06:57:11+00:00

I use a Java method that returns an object or null if a value

  • 0

I use a Java method that returns an object or null if a value was not found. So I need to check for null values:

val value = javaobject.findThing(xyz)
if(value != null) {
    value.doAnotherThing()
} else {
    warn("Value not found.")
}

Can I write this code shorter with the Box concept? I have read the Lift-Wiki-documentation about the Box concept, but I don’t understand how to use it with Java null values.

  • 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-29T06:57:11+00:00Added an answer on May 29, 2026 at 6:57 am

    @TimN is right, you could use Box(value) to create a Box from a possibly null value, but you’ll get a deprecation warning.

    scala> val v: Thing = null
    v: Thing = null
    
    scala> Box[Thing](v)
    <console>:25: warning: method apply in trait BoxTrait is deprecated: Use legacyNullTest
                  Box[Thing](v)
    

    While you could use Box.legacyNullTest, if this is what you’re doing, then I would just stick with the standard library and use Option.

    Option(javaobject.findThing(xyz)) match {
        case Some(thing) => thing.doAnotherThing()
        case _ => warn("Value not found.")
    }
    

    And if you needed a Box to pass around, Option will automagically convert to a Box:

    scala> val b: Box[Thing] = Option(v)
    b: net.liftweb.common.Box[Thing] = Empty
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm converting an application to use Java 1.5 and have found the following method:
Consider this Java code class A{ //@returns Class object this method is contained in
I heard that static methods should use only static variables in java. But, main
I recently wrote some data access methods (plain old Java) that use immutable objects
I am trying to use Java's useDelimiter method on it's Scanner class to do
I start to use Java annotations heavily. One example is taking method with annotations
Is there any tricky way to use Java reserved words as variable, method, class,
What does Java use as a default probing method for HashMap? Is it Linear?
In Java we use System.setProperty() method to set some system properties. According to this
when i use setDefaultLookAndFeelDecorated(true) method in Java why is the Frame appear FullScreen when

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.